星空网 > 软件开发 > ASP.net

C#生成Code39(extend)条形码【非条形码字体】

Code39是条形码的一种。由于编制简单、能够对任意长度的数据进行编码、支持设备广泛等特性而被广泛采用。

  1. 能够对任意长度的数据进行编码。其局限在于印刷品的长度和条码阅读器的识别范围。
  2. 支持设备广泛。目前几乎所有的条形码阅读设备都能阅读Code39码,打印机也是同样情况。
  3. 编制简单。简单的开发技术就能快速生成相应的编码图像。
  4. 一般Code39码由5条线和分开它们的4条缝隙共9个元素构成。线和缝隙有宽窄之分,而且无论线还是缝隙仅有3个比其他的元素要宽一定比例。39码因此得名

Code39条形码规则

  1、 每五条线表示一个字符;
  2、 粗线表示1,细线表示0;
  3、 线条间的间隙宽的表示1,窄的表示0;
  4、 五条线加上它们之间的四条间隙就是九位二进制编码,而且这九位中必定有三位是1,所以称为39码;
  5、 条形码的首尾各一个 * 标识开始和结束。
 
Code 39只接受如下43个有效输入字符:
  1、26个大写字母(A - Z),
  2、十个数字(0 - 9),
  3、连接号(-),句号(.),空格,美圆符号($),斜扛(/),加号(+)以及百分号(%)。
    4、其余的输入将被忽略。
  5、code39通常情况下不需要校验码。但是对於精确度要求高的应用,需要在code39条形码後面增加一个校验码。
 
条形码的生成网上也有很多库文件,做的特别好的都是商业授权。一维码二维码在.net开源项目中的生成读取比较知名的有:Zxing.net(http://zxingnet.codeplex.com/)、BarcodeLib等
但有些时候开源软件所实现的并不满足当下的需求,所以还要做一些其他尝试。效果如下图:
C#生成Code39(extend)条形码【非条形码字体】
 
代码分享:
 1   /// <summary> 2   /// Code39一维码生成类 3   /// </summary> 4   public class CSharpCode39 5   { 6  7     #region 数据码 8     private byte[,] c39_bp = new byte[,] {  9       { 0x30, 0x30, 0x30, 0x30, 0x31, 0x31, 0x30, 0x31, 0x30, 0x30 }, { 0x31, 0x31, 0x30, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x31 }, { 0x32, 0x30, 0x30, 0x31, 0x31, 0x30, 0x30, 0x30, 0x30, 0x31 }, { 0x33, 0x31, 0x30, 0x31, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30 }, { 0x34, 0x30, 0x30, 0x30, 0x31, 0x31, 0x30, 0x30, 0x30, 0x31 }, { 0x35, 0x31, 0x30, 0x30, 0x31, 0x31, 0x30, 0x30, 0x30, 0x30 }, { 0x36, 0x30, 0x30, 0x31, 0x31, 0x31, 0x30, 0x30, 0x30, 0x30 }, { 0x37, 0x30, 0x30, 0x30, 0x31, 0x30, 0x30, 0x31, 0x30, 0x31 }, { 0x38, 0x31, 0x30, 0x30, 0x31, 0x30, 0x30, 0x31, 0x30, 0x30 }, { 0x39, 0x30, 0x30, 0x31, 0x31, 0x30, 0x30, 0x31, 0x30, 0x30 }, { 0x41, 0x31, 0x30, 0x30, 0x30, 0x30, 0x31, 0x30, 0x30, 0x31 }, { 0x42, 0x30, 0x30, 0x31, 0x30, 0x30, 0x31, 0x30, 0x30, 0x31 }, { 0x43, 0x31, 0x30, 0x31, 0x30, 0x30, 0x31, 0x30, 0x30, 0x30 }, { 0x44, 0x30, 0x30, 0x30, 0x30, 0x31, 0x31, 0x30, 0x30, 0x31 }, { 0x45, 0x31, 0x30, 0x30, 0x30, 0x31, 0x31, 0x30, 0x30, 0x30 }, { 70, 0x30, 0x30, 0x31, 0x30, 0x31, 0x31, 0x30, 0x30, 0x30 },  10       { 0x47, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31, 0x31, 0x30, 0x31 }, { 0x48, 0x31, 0x30, 0x30, 0x30, 0x30, 0x31, 0x31, 0x30, 0x30 }, { 0x49, 0x30, 0x30, 0x31, 0x30, 0x30, 0x31, 0x31, 0x30, 0x30 }, { 0x4a, 0x30, 0x30, 0x30, 0x30, 0x31, 0x31, 0x31, 0x30, 0x30 }, { 0x4b, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31, 0x31 }, { 0x4c, 0x30, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x31, 0x31 }, { 0x4d, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x31, 0x30 }, { 0x4e, 0x30, 0x30, 0x30, 0x30, 0x31, 0x30, 0x30, 0x31, 0x31 }, { 0x4f, 0x31, 0x30, 0x30, 0x30, 0x31, 0x30, 0x30, 0x31, 0x30 }, { 80, 0x30, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x31, 0x30 }, { 0x51, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31, 0x31, 0x31 }, { 0x52, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31, 0x31, 0x30 }, { 0x53, 0x30, 0x30, 0x31, 0x30, 0x30, 0x30, 0x31, 0x31, 0x30 }, { 0x54, 0x30, 0x30, 0x30, 0x30, 0x31, 0x30, 0x31, 0x31, 0x30 }, { 0x55, 0x31, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31 }, { 0x56, 0x30, 0x31, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31 },  11       { 0x57, 0x31, 0x31, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 }, { 0x58, 0x30, 0x31, 0x30, 0x30, 0x31, 0x30, 0x30, 0x30, 0x31 }, { 0x59, 0x31, 0x31, 0x30, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30 }, { 90, 0x30, 0x31, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30 }, { 0x2d, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x31, 0x30, 0x31 }, { 0x2e, 0x31, 0x31, 0x30, 0x30, 0x30, 0x30, 0x31, 0x30, 0x30 }, { 0x20, 0x30, 0x31, 0x31, 0x30, 0x30, 0x30, 0x31, 0x30, 0x30 }, { 0x2a, 0x30, 0x31, 0x30, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30 }, { 0x24, 0x30, 0x31, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30 }, { 0x2f, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x31, 0x30 }, { 0x2b, 0x30, 0x31, 0x30, 0x30, 0x30, 0x31, 0x30, 0x31, 0x30 }, { 0x25, 0x30, 0x30, 0x30, 0x31, 0x30, 0x31, 0x30, 0x31, 0x30 } 12      }; 13     //code39合法字符集 [0-9A-Z+-*/%. ] 共43个 14     private byte[] c39_cw = new byte[] {  15       0x30, 0x31, 50, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 70,  16       0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 80, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56,  17       0x57, 0x58, 0x59, 90, 0x2d, 0x2e, 0x20, 0x24, 0x2f, 0x2b, 0x25 18      }; 19     private byte[,] c39_ex = new byte[,] {  20       { 1, 0x24, 0x41 }, { 2, 0x24, 0x42 }, { 3, 0x24, 0x43 }, { 4, 0x24, 0x44 }, { 5, 0x24, 0x45 }, { 6, 0x24, 70 }, { 7, 0x24, 0x47 }, { 8, 0x24, 0x48 }, { 9, 0x24, 0x49 }, { 10, 0x24, 0x4a }, { 11, 0x24, 0x4b }, { 12, 0x24, 0x4c }, { 13, 0x24, 0x4d }, { 14, 0x24, 0x4e }, { 15, 0x24, 0x4f }, { 0x10, 0x24, 80 },  21       { 0x11, 0x24, 0x51 }, { 0x12, 0x24, 0x52 }, { 0x13, 0x24, 0x53 }, { 20, 0x24, 0x54 }, { 0x15, 0x24, 0x55 }, { 0x16, 0x24, 0x56 }, { 0x17, 0x24, 0x57 }, { 0x18, 0x24, 0x58 }, { 0x19, 0x24, 0x59 }, { 0x1a, 0x24, 90 }, { 0x1b, 0x25, 0x41 }, { 0x1c, 0x25, 0x42 }, { 0x1d, 0x25, 0x43 }, { 30, 0x25, 0x44 }, { 0x1f, 0x25, 0x45 }, { 0x3b, 0x25, 70 },  22       { 60, 0x25, 0x47 }, { 0x3d, 0x25, 0x48 }, { 0x3e, 0x25, 0x49 }, { 0x3f, 0x25, 0x4a }, { 0x5b, 0x25, 0x4b }, { 0x5c, 0x25, 0x4c }, { 0x5d, 0x25, 0x4d }, { 0x5e, 0x25, 0x4e }, { 0x5f, 0x25, 0x4f }, { 0x7b, 0x25, 80 }, { 0x7c, 0x25, 0x51 }, { 0x7d, 0x25, 0x52 }, { 0x7e, 0x25, 0x53 }, { 0x7f, 0x25, 0x54 }, { 0, 0x25, 0x55 }, { 0x40, 0x25, 0x56 },  23       { 0x60, 0x25, 0x57 }, { 0x21, 0x2f, 0x41 }, { 0x22, 0x2f, 0x42 }, { 0x23, 0x2f, 0x43 }, { 0x26, 0x2f, 70 }, { 0x27, 0x2f, 0x47 }, { 40, 0x2f, 0x48 }, { 0x29, 0x2f, 0x49 }, { 0x2a, 0x2f, 0x4a }, { 0x2c, 0x2f, 0x4c }, { 0x3a, 0x2f, 90 }, { 0x61, 0x2b, 0x41 }, { 0x62, 0x2b, 0x42 }, { 0x63, 0x2b, 0x43 }, { 100, 0x2b, 0x44 }, { 0x65, 0x2b, 0x45 },  24       { 0x66, 0x2b, 70 }, { 0x67, 0x2b, 0x47 }, { 0x68, 0x2b, 0x48 }, { 0x69, 0x2b, 0x49 }, { 0x6a, 0x2b, 0x4a }, { 0x6b, 0x2b, 0x4b }, { 0x6c, 0x2b, 0x4c }, { 0x6d, 0x2b, 0x4d }, { 110, 0x2b, 0x4e }, { 0x6f, 0x2b, 0x4f }, { 0x70, 0x2b, 80 }, { 0x71, 0x2b, 0x51 }, { 0x72, 0x2b, 0x52 }, { 0x73, 0x2b, 0x53 }, { 0x74, 0x2b, 0x54 }, { 0x75, 0x2b, 0x55 },  25       { 0x76, 0x2b, 0x56 }, { 0x77, 0x2b, 0x57 }, { 120, 0x2b, 0x58 }, { 0x79, 0x2b, 0x59 }, { 0x7a, 0x2b, 90 } 26      }; 27     #endregion 28     #region 字段和属性 29  30  31  32     private bool _checksum; 33     private string _dataToEncode; 34     private bool _humanReadable; 35     private string _humanReadableFont; 36     private float _humanReadableSize; 37     private float _marginX; 38     private float _marginY; 39     private float _moduleHeight; 40     private float _moduleWidth; 41     private float _ratio; 42     private float _reduction; 43     private Color _codeBarColor = Color.Black; 44     private bool _isDisplayCheckCode; 45     private string _checkData; 46     private bool _isDisplayStartStopSign; 47     /// <summary> 48     /// 是否检查效验 49     /// </summary> 50     public bool Checksum 51     { 52       get 53       { 54         return _checksum; 55       } 56       set 57       { 58         _checksum = value; 59       } 60     } 61  62  63     /// <summary> 64     /// 要进行编码的数据 65     /// </summary> 66     public string DataToEncode 67     { 68       get 69       { 70         return _dataToEncode; 71       } 72       set 73       { 74         _dataToEncode = value; 75       } 76     } 77  78  79  80     /// <summary> 81     /// 是否显示文本内容 82     /// </summary> 83     public bool HumanReadable 84     { 85       get 86       { 87         return _humanReadable; 88       } 89       set 90       { 91         _humanReadable = value; 92       } 93     } 94     /// <summary> 95     /// 用于显示文本内容的字体 96     /// </summary> 97     public string HumanReadableFont 98     { 99       get100       {101         return _humanReadableFont;102       }103       set104       {105         _humanReadableFont = value;106       }107     }108     /// <summary>109     /// 用于显示文本内容文字的代大小 110     /// </summary>111     public float HumanReadableSize112     {113       get114       {115         return _humanReadableSize;116       }117       set118       {119         _humanReadableSize = value;120       }121     }122 123     /// <summary>124     /// 水平方向边距mm125     /// 水平方向建议尽量留白126     /// 如果没有留白同时模块宽度较小可能会造成无法识别127     /// </summary>128     public float MarginX129     {130       get131       {132         return _marginX;133       }134       set135       {136         _marginX = value;137       }138     }139     /// <summary>140     /// 垂直方向边距mm141     /// </summary>142     public float MarginY143     {144       get145       {146         return _marginY;147       }148       set149       {150         _marginY = value;151       }152     }153 154     /// <summary>155     /// 模块高度156     /// </summary>157     public float ModuleHeight158     {159       get160       {161         return _moduleHeight;162       }163       set164       {165         _moduleHeight = value;166       }167     }168 169     /// <summary>170     /// 模块宽度mm171     /// 模块宽度不应低于0.2646f172     /// 模块宽度过低会造成数据丢失因而读不出数据或者会误读173     /// </summary>174     public float ModuleWidth175     {176       get177       {178         return _moduleWidth;179       }180       set181       {182         _moduleWidth = value;183       }184     }185 186 187     /// <summary>188     /// 放大比率 189     /// </summary>190     public float Ratio191     {192       get193       {194         return _ratio;195       }196       set197       {198         _ratio = value;199       }200     }201     /// <summary>202     /// 缩小203     /// </summary>204     public float Reduction205     {206       get207       {208         return _reduction;209       }210       set211       {212         _reduction = value;213       }214     }215     /// <summary>216     /// 设置条形码的颜色217     /// </summary>218     public Color CodeBarColor219     {220       get221       {222         return _codeBarColor;223       }224       set225       {226         _codeBarColor = value;227       }228     }229 230     /// <summary>231     /// 是否显示效验码232     /// 此属性要在Checksum属性为true的情况下有用233     /// </summary>234     public bool IsDisplayCheckCode235     {236       get { return this._isDisplayCheckCode; }237       set { this._isDisplayCheckCode = value; }238     }239     /// <summary>240     /// 供人识别是否显示起止符241     /// </summary>242     public bool IsDisplayStartStopSign243     {244       get { return this._isDisplayStartStopSign; }245       set { this._isDisplayStartStopSign = value; }246     }247     #endregion248     /// <summary>249     /// 默认构造函数250     /// 初始化251     /// </summary>252     public CSharpCode39()253     {254       this.initData();255       256     }257     public CSharpCode39(string dataToEncode)258     {259       this.initData();260       this._dataToEncode = dataToEncode;261     }262     /// <summary>263     /// 默认构造函数264     /// 初始化数据265     /// </summary>266     private void initData()267     {268       this._humanReadableFont = "Arial";269       this._humanReadableSize = 10f;270       this._codeBarColor = Color.Black;271       this._moduleHeight = 15f;272       this._moduleWidth = 0.35f;273       this._ratio = 3f;274       this._marginX =2;275       this._marginY =2;276       this._checksum = true;277       this._isDisplayCheckCode = false;278       this._isDisplayStartStopSign = false;279     }280     private char[] _bitpattern_c39(string rawdata, ref int finalLength)281     {282       //0x27 39 283       //0x50 80284       if ((rawdata.Length == 0) || (rawdata.Length > 0x50 /*0x27*/))285       {286         return null;287       }288       for (int i = 0; i < rawdata.Length; i++)289       {290         if ((rawdata[i] == '\0') || (rawdata[i] > '\x007f'))291         {292           return null;293         }294       }295       byte[] data = processTilde(rawdata);296       if (data.Length == 0)297       {298         return null;299       }300       byte[] buffer2 = this.processExtended(data);301       if ((buffer2.Length == 0) || (buffer2.Length > /*40*/80))302       {303         return null;304       }305       finalLength = this._checksum ? ((buffer2.Length + 2) + 1) : (buffer2.Length + 2);306       return this.getPattern_c39(buffer2);307     }308 309 310     /// <summary>311     /// 计算效验值312     /// </summary>313     /// <param name="data"></param>314     /// <param name="len"></param>315     /// <returns></returns>316     private byte _checksum_c39(byte[] data, int len)317     {318       //0x2b 43319       //字符值的总和除以合法字符集的个数43 取余数  余数在合法字符数组中对应的数值就是效验值320       int num2 = 0;321       for (int i = 1; i < len; i++)322       {323         num2 += this.valueFromCharacter(data[i]);324       }325       return this.c39_cw[num2 % 0x2b];326     }327 328     private char[] Code39_bitpattern(string dataToEncode)329     {330       int finalLength = 0;331       return this._bitpattern_c39(dataToEncode, ref finalLength);332     }333     /// <summary>334     /// 获得Code39条码图片335     /// </summary>336     /// <param name="resolution">为DPI</param>337     /// <returns></returns>338     public Bitmap getBitmapImage(float resolution)339     {340      return Code39_createCode(resolution);341     }342    343     private Bitmap Code39_createCode(float resolution)344     {345       int num6;346       int finalLength = 0;347       char[] chArray = this._bitpattern_c39(DataToEncode, ref finalLength);348       if (chArray == null)349       {350         return null;351       }352       float fontsize = this._humanReadable ? (0.3527778f * this._humanReadableSize) : 0f;353       // float num3 = (7f * ModuleWidth) + ((3f * Ratio) * ModuleWidth);354       float num3 = (7f * this._moduleWidth) + ((3f * this._ratio) * this._moduleWidth);355       float width = (finalLength * num3) + (2f * this._marginX);356       float height = (this._moduleHeight + (2f * this._marginY)) + fontsize;357       width *= resolution / 25.4f;358       height *= resolution / 25.4f;359       Bitmap image = new Bitmap((int)width, (int)height, PixelFormat.Format32bppPArgb);360       image.SetResolution(resolution, resolution);361       //image.SetResolution(300, 300);362       Graphics g = Graphics.FromImage(image);363       g.Clear(Color.White);364       g.PageUnit = GraphicsUnit.Millimeter; //以毫米为度量单位365       g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, /*(int)width*/image.Width, /*(int)height*/image.Height));366       //new Pen(Color.Black, 2f);367       //new SolidBrush(Color.White);368       SolidBrush brush = new SolidBrush(Color.Black);369       if (resolution < 300f)370       {371         //g.TextRenderingHint = TextRenderingHint.AntiAlias;372         //g.SmoothingMode = SmoothingMode.AntiAlias;373         g.CompositingQuality = CompositingQuality.HighQuality;374         //g.SmoothingMode = SmoothingMode.HighQuality;375         g.InterpolationMode = InterpolationMode.HighQualityBicubic;376         g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;377       }378       float num7 = 0f;379       for (num6 = 0; num6 < chArray.Length; num6++)380       {381         if (chArray[num6] == '0')382         {383           if ((num6 & 1) != 1)384           {385             RectangleF rect = new RectangleF(MarginX + num7, MarginY, ModuleWidth, ModuleHeight);386             MakeBar(g, rect, Reduction);387           }388           num7 += 1f * ModuleWidth;389         }390         else391         {392           if ((num6 & 1) != 1)393           {394             RectangleF ef2 = new RectangleF(MarginX + num7, MarginY, Ratio * ModuleWidth, ModuleHeight);395             MakeBar(g, ef2, Reduction);396           }397           num7 += Ratio * ModuleWidth;398         }399       }400 401       #region 供人识别内容402       if (this._humanReadable)403       {404         #region 保留405         /*byte[] buffer2 = processTilde(this._dataToEncode);406         int index = 0;407         List<byte> arr = new List<byte>();408         for (num6 = 0; num6 < buffer2.Length; num6++)409         {410           //0x20 32   0x7e 126411           if ((buffer2[num6] >= 0x20) && (buffer2[num6] <= 0x7e))412           {413             arr.Add(buffer2[num6]);414           }415         }416         byte[] bytes = new byte[arr.Count];417         for (int i = 0; i < arr.Count; i++)418         {419           bytes[i] = arr[i];420         }421         index = arr.Count;422         423         //string text = new ASCIIEncoding().GetString(bytes, 0, index);424         */ 425         #endregion426         string text = this._dataToEncode;427         if (this._isDisplayCheckCode&&!string.IsNullOrEmpty(this._checkData)) {428           text += this._checkData;429         }430         if (this._isDisplayStartStopSign)431         {432           text = "*" + text + "*";433         }434         Font font = new Font(this._humanReadableFont, this._humanReadableSize);435         //g.DrawString(text, font, brush, new PointF(MarginX, MarginY + ModuleHeight));436         //新增字符串格式437         var drawFormat = new StringFormat { Alignment = StringAlignment.Center };438         float inpix = image.Width / resolution;//根据DPi求出 英寸数439         float widthmm = inpix * 25.4f;  //有每英寸像素求出毫米440         //g.DrawString(text, font, Brushes.Black, width / 2, height - 14, drawFormat);441         g.DrawString(text, font, /*Brushes.Black*/brush, new PointF(/*MarginX*/(int)(widthmm / 2), MarginY + ModuleHeight + 1), drawFormat);442 443       }444 445       #endregion446 447 448 449       return image;450     }451 452     /// <summary>453     /// 进行图形填充454     /// </summary>455     /// <param name="g"></param>456     /// <param name="rect"></param>457     /// <param name="reduction"></param>458     private void MakeBar(Graphics g, RectangleF rect, float reduction)459     {460       MakeBar(g, rect, reduction,this._codeBarColor);461     }462 463     /// <summary>464     /// 进行图形填充465     /// </summary>466     /// <param name="g"></param>467     /// <param name="rect"></param>468     /// <param name="reduction"></param>469     /// <param name="brushColor"></param>470     private void MakeBar(Graphics g, RectangleF rect, float reduction, Color brushColor)471     {472       float num = rect.Width * (reduction / 200f);473       float num2 = rect.Width - (rect.Width * (reduction / 200f));474 475       RectangleF ef = new RectangleF476       {477         X = rect.X + num,478         Y = rect.Y,479         Width = num2,480         Height = rect.Height481       };482       SolidBrush brush = new SolidBrush(brushColor);483       g.FillRectangle(brush, ef);484 485     }486 487     private char[] getPattern_c39(byte[] data)488     {  //0x2a 42为*489       //int num = 0x27;490       int num = 80;491       byte[] buffer = new byte[num + 1];492       buffer[0] = 0x2a;493       int index = 1;494       for (int i = 0; i < data.Length; i++)495       {496         buffer[index] = data[i];497         index++;498       }499       if (Checksum)500       {501         buffer[index] = this._checksum_c39(buffer, index);502         if (_isDisplayCheckCode)503         {504           this._checkData = ((char)buffer[index]).ToString();505         }506         index++;507       }508       buffer[index] = 0x2a;509       index++;510       char[] chArray = new char[index * 10];511       int num5 = 0;512       for (int j = 0; j < index; j++)513       {514         byte c = buffer[j];515         int num9 = this.indexFromCharacter(c);516         for (int k = 0; k < 9; k++)517         {518           chArray[num5] = (char)this.c39_bp[num9, k + 1];519           num5++;520         }521         chArray[num5] = '0';522         num5++;523       }524       return chArray;525     }526 527     private int indexFromCharacter(byte c)528     {529       //0x2c==44530       for (int i = 0; i < 0x2c; i++)531       {532         if (this.c39_bp[i, 0] == c)533         {534           return i;535         }536       }537       return -1;538     }539 540 541     private byte[] processExtended(byte[] data)542     {543       //0x25 38544       //0x4F 79 0x4E 78545       //int num = 0x4F;546       int num = data.Length - 1;547       byte[] sourceArray = new byte[num + 1];548       int index = 0;549       for (int i = 0; i < data.Length; i++)550       {551         byte c = data[i];552         if (this.valueFromCharacter(c) != -1)553         {554           sourceArray[index] = c;555           index++;556         }557         else558         {559           byte num5 = 0;560           byte num6 = 0;561           if (this.valuesFromExtended(c, ref num5, ref num6))562           {563             sourceArray[index] = num5;564             sourceArray[index + 1] = num6;565             index += 2;566           }567         }568       }569       byte[] destinationArray = new byte[index];570       Array.Copy(sourceArray, destinationArray, index);571       return destinationArray;572     }573 574     /// <summary>575     /// 返回指定字符在code39合法字符数组中对应的索引576     /// </summary>577     /// <param name="c"></param>578     /// <returns></returns>579     private int valueFromCharacter(byte c)580     {581       //c39_cw为数组,保存的为合法的字符集信息[0-9A-Z+-*/%. ] 共43个582       //如果存在这个字符就返回c39_cw对应的索引583       for (int i = 0; i < /*0x2b*/this.c39_cw.Length; i++)584       {585         if (this.c39_cw[i] == c)586         {587           return i;588         }589       }590       return -1;591     }592 593     /// <summary>594     /// 判断字符集是否存在Extended595     /// </summary>596     /// <param name="c"></param>597     /// <param name="v1"></param>598     /// <param name="v2"></param>599     /// <returns></returns>600     private bool valuesFromExtended(byte c, ref byte v1, ref byte v2)601     {602       //0x55 85603       for (int i = 0; i < 0x55; i++)604       {605         if (this.c39_ex[i, 0] == c)606         {607           v1 = this.c39_ex[i, 1];608           v2 = this.c39_ex[i, 2];609           return true;610         }611       }612       return false;613     }614 615     private byte[] processTilde(string rawdata)616     {617       byte[] sourceArray = new byte[rawdata.Length];618       int index = 0;619       for (int i = 0; i < rawdata.Length; i++)620       {621         if (rawdata[i] != '~')622         {623           sourceArray[index] = (byte)rawdata[i];624           index++;625         }626         else if ((i + 3) < rawdata.Length)627         {628           string str = new string(new char[] { rawdata[i + 1], rawdata[i + 2], rawdata[i + 3] });629           int num3 = Convert.ToInt32(str, 10);630           if ((num3 > 0) && (num3 <= 0xff))631           {632             sourceArray[index] = (byte)num3;633             index++;634           }635           if (num3 == 0x3e7)636           {637             sourceArray[index] = 0x86;638             index++;639           }640           i += 3;641         }642         else643         {644           sourceArray[index] = (byte)rawdata[i];645           index++;646         }647       }648       byte[] destinationArray = new byte[index];649       Array.Copy(sourceArray, destinationArray, index);650       return destinationArray;651     }652   }

 这个方法是根据DPI以及数据模块的高宽来确定条码的实际高宽。模块的宽度以毫米(mm)来算。类中的getBitmapImage方法的一个参数getBitmapImage为DPI。安卓用户安装平时常用的扫码软件即可做识别测试。就识别率来说还是可以的,扫描枪绝对是可以扫的出的。同时使用过MessagingToolkit.Barcode进行解码测试,万张无一误读。

 

在线解码网站:http://cli.im/deqr/、http://www.fbqr.com/desQR#

 WinForm的例子下载:点击下载DEMO


 



原标题:C#生成Code39(extend)条形码【非条形码字体】

关键词:C#

C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流