现在的位置: 首页 > 综合 > 正文

条码生成代码(Code39码)

2014年01月09日 ⁄ 综合 ⁄ 共 5646字 ⁄ 字号 评论关闭
『Code 39条形码介绍』

 code39条码能表示字母、数字和其它一些符号共43个字符:A -Z, 0-9, -.$/ + %, space等,其主要特点如下:
  ◆条码的长度是可变化的
  ◆通常用“*”号作为起始、终止符
  ◆校验码不用
  ◆代码密度介于3 - 9.4个字符/每英寸       
  ◆空白区是窄条的10倍
  ◆用于工业、图书、以及票证自动化管理上
三九码是Intermec公司于1975年推出的一种条码,它可表示数字、英文字母以及“-”、“.”、“/”、“ ”、“%”、“$”、“ ”(空格)和“*”共44个符号,其中“*”仅作为启始符和终止符。
三九码且有编码规则简单、误码率低、所能表示字符个数多等特点,因此在各个领域有着极为广泛的应用。我国也制定了相应的国家标准(GB12908-91)。
三九码仅有两种单元宽度——分别为宽单元和窄单元。宽单元这宽度为窄单元的1到3倍,一般多选用2倍、2.5倍或3倍。三九码的每一个条码字符由九个单元组成,其中有三个宽单元,其余是窄单元,因此称为三九码。
我国有的图书馆使用的39码使用了特殊的校验字符,条码软件都可打印适用于图书馆的39码。
代码原创: LovlyPuppy
实现该功能分两个类,第一个类 DrawImageBord.cs
using System.Drawing;
using System.Drawing.Imaging;
public   abstract   class   DrawImageBord 
 

     
protected   virtual   string   BordRuleName 
      

         
get   {   return   string.Empty;   } 
      }
 

     
protected   virtual   System.Collections.Hashtable   Roles 
      

         
get   {   return   new   System.Collections.Hashtable();   } 
      }
 

     
string   drawString; 
     
int   width   =   800;   //画布的宽度(可计算) 
     int   height   =   36;//1CM 
     int   unitWidth   =   1;   // 

     
int   currentLocation   =   0

     
public   DrawImageBord(string   s) 
     

         drawString   
=   s; 
     }
 

     
public   virtual   void   Draw(System.IO.Stream   target) 
     

         Bitmap   bm   
=   new   Bitmap(width,   height); 
         Graphics   g   
=   Graphics.FromImage(bm); 
         g.SmoothingMode   
=   System.Drawing.Drawing2D.SmoothingMode.Default; 
         g.TextRenderingHint   
=   System.Drawing.Text.TextRenderingHint.AntiAlias; 

         
//画布和边的设定   
         g.Clear(Color.White); 

         g.DrawRectangle(Pens.White,   
0,   0,   width,   height); 
         
for(int   i   =   0;   i   <   drawString.Length;   i++
               

                   
this.DrawString(drawString[i].ToString(),   g); 
               }
 
        bm.Save(target,   ImageFormat.Jpeg); 
     }
 
    
protected   virtual   void   DrawString(string   s,   Graphics   g) 
    

          System.Collections.Hashtable   hash   
=   this.Roles; 
          
object   o   =   hash[s]; 
          
if   (o   ==   null)   return
          
char[]   chars   =   o.ToString().ToCharArray(); 
          
if   (chars.Length   >   9)   return
              SolidBrush   blackBrush   
=   new   SolidBrush(Color.Black); 
              SolidBrush   witeBrush   
=   new   SolidBrush(Color.White); 

           
for(int   i   =   0;   i   <   5;   i++
              

                 
//画第一个   0   黑条 
                  if   (chars[i]   ==   '0'
                  

                         Rectangle   re1   
=   new   Rectangle(currentLocation,   0,   unitWidth,   height); 
                         g.FillRectangle(blackBrush,   re1); 
                         currentLocation   
+=   unitWidth; 
                  }
 
                  
else 
                  

                         Rectangle   re1   
=   new   Rectangle(currentLocation,   0,   3   *   unitWidth,   height); 
                         g.FillRectangle(blackBrush,   re1); 
                         currentLocation   
+=   3   *   unitWidth; 
                  }
 
                 
//画第6个     5   白条 
                  if   ((i   +   5)   <   9
                  

                       
if   (chars[i+5==  '0'
                       

                             Rectangle   re1   
=   new   Rectangle(currentLocation,   0,   unitWidth,   height); 
                             g.FillRectangle(witeBrush,   re1); 
                             currentLocation   
+=   unitWidth; 
                       }
 
                       
else 
                       

                               Rectangle   re1   
=   new   Rectangle(currentLocation,   0,   3   *   unitWidth,   height); 
                               g.FillRectangle(witeBrush,   re1); 
                               currentLocation   
+=   3   *   unitWidth; 
                       }
 
                  }
 
               }
 
                 Rectangle   re2   
=   new   Rectangle(currentLocation,   0,   unitWidth,   height); 
                 g.FillRectangle(witeBrush,   re2); 
                currentLocation   
+=   unitWidth; 

    }
 
}
 
 
第二个类CODE39DrawImageBord.cs 继承DrawImageBord.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/**//// <summary>
/// CODE39DrawImageBord 的摘要说明
/// </summary>

public class CODE39DrawImageBord : DrawImageBord
...
{
    
private System.Collections.Hashtable hash = new System.Collections.Hashtable();
    
protected override string BordRuleName
    ...
{
        
get ...return "CODE39"; }
    }

    
public CODE39DrawImageBord(string s)
        : 
base(s)
    ...
{

    }


    
protected override System.Collections.Hashtable Roles
    ...
{
        
get
        ...
{
            
if (hash.Count > 0return hash;
            hash.Add(
"0""001100100");
            hash.Add(
"1""100010100");
            hash.Add(
"2""010010100");
            hash.Add(
"3""110000100");
            hash.Add(
"4""001010100");
            hash.Add(
"5""101000100");
            hash.Add(
"6""011000100");
            hash.Add(
"7""000110100");

            hash.Add(
"8""100100100");
            hash.Add(
"9""010100100");
            hash.Add(
"A""100010010");
            hash.Add(
"B""010010010");
            hash.Add(
"C""110000010");
            hash.Add(
"D""001010010");
            hash.Add(
"E""101000010");

            hash.Add(
"F""011000010");
            hash.Add(
"G""000110010");
            hash.Add(
"H""100100010");
            hash.Add(
"I""010100010");
            hash.Add(
"J"

抱歉!评论已关闭.