在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
一、39条码简介 39码是1974年发展出来的条码系统,是一种可供使用者双向扫瞄的分散式条码,也就是说相临两资料码之间,必须包含一个不具任何意义的空白(或细白,其逻辑值为0),且其具有支援文字的能力,故较一般一维条码应用广泛,由于CODE 39支持0~9、A~Z等,目前较主要用于,通常运用于资产管理、会员卡、店内码管理、产品卷标等。 标准的39码是由起始安全空间、起始码、资料码、可忽略不计的检查码、终止安全空间及终止码所构成。 二、39码编码
39码的每一个字元编码方式,都是由九条不同排列的线条编码而得。可区分成如表之四种类型:
表1 39码的字元编码方式 (1)英文字母部分 26个英文字母所对应的39码逻辑值如表所示。
(2)数字与特殊符号部分 39码也可表示数字0~9以及特殊符号,其对应的逻辑值如表所示。
(3)39码的检查码查询表
三、39码编码算法 (1)移除输入待编码内容Value to Code中的*,得到Value; (2)将Value逐位按“二”中的的检查码查询表计算检查码的和sum,计算检查码 checkcode=sum%43,再以checkcode在检查码中查询对应的编码字元checkChar (3)将Value与checkChar拼接后,前后加上'*'再逐位按“二”中的对应的编码+‘0’进行编码,将最后一个0去掉后得到Code; (4)按表1对Code进行绘图。 四、基于C#的算法实现 (1)UI设计
(2)算法代码 1 using System; 2 using System.Collections; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 using System.Drawing; 8 using System.Drawing.Drawing2D; 9 10 namespace BarCode 11 { 12 /// <summary> 13 /// 39 barcode 14 /// </summary> 15 public class BarCode 16 { 17 private string _rawData; 18 private bool _enableChecksum; 19 private int _height; 20 private int _width; 21 private bool _includeLabel; 22 private string _code; 23 private enum LabelPositions : int { TOPLEFT, TOPCENTER, TOPRIGHT, BOTTOMLEFT, BOTTOMCENTER, BOTTOMRIGHT }; 24 25 private LabelPositions _labelPosition; 26 private Color _ForeColor = Color.Black; 27 private Color _BackColor = Color.White; 28 private string Code 29 { 30 get 31 { 32 if (string.IsNullOrEmpty(this._code)) 33 { 34 this._code = Encode(); 35 } 36 return this._code; 37 } 38 } 39 private Image _encodedImage; 40 public Image EncodedImage 41 { 42 get{return _encodedImage;} 43 set{this._encodedImage=value;} 44 } 45 private Hashtable C39_Code = new Hashtable(); //is initialized by init_Code39() 46 private Hashtable ExtC39_Translation = new Hashtable(); 47 /// <summary> 48 /// Encodes with Code39. 49 /// </summary> 50 /// <param name="input">Data to encode.</param> 51 /// <param name="AllowExtended">Allow Extended Code 39 (Full Ascii mode).</param> 52 /// <param name="EnableChecksum">Whether to calculate the Mod 43 checksum and encode it into the barcode</param> 53 public BarCode(string input, bool enableChecksum, int height, int width,bool includeLabel) 54 { 55 _rawData = input; 56 _enableChecksum = enableChecksum; 57 _height = height; 58 _width = width; 59 _includeLabel=includeLabel; 60 _labelPosition=LabelPositions.BOTTOMCENTER; 61 } 62 63 /// <summary> 64 /// Encode the raw data using the Code 39 algorithm. 65 /// </summary> 66 private string Encode() 67 { 68 this.init_Code39(); 69 this.init_ExtendedCode39(); 70 71 string strNoAstr = this._rawData.Replace("*", ""); 72 string strFormattedData = "*" + strNoAstr + (this._enableChecksum ? getChecksumChar(strNoAstr).ToString() : String.Empty) + "*"; 73 74 string result = ""; 75 //foreach (char c in this.FormattedData) 76 foreach (char c in strFormattedData) 77 { 78 try 79 { 80 result += C39_Code[c].ToString(); 81 result += "0";//whitespace 82 }//try 83 catch 84 { 85 throw new Exception("EC39-1: Invalid data. (Try using Extended Code39)"); 86 }//catch 87 }//foreach 88 89 result = result.Substring(0, result.Length-1); 90 91 //clear the hashtable so it no longer takes up memory 92 this.C39_Code.Clear(); 93 94 return result; 95 }//Encode_Code39 96 private void init_Code39() 97 { 98 C39_Code.Clear(); 99 C39_Code.Add('0', "101001101101"); 100 C39_Code.Add('1', "110100101011"); 101 C39_Code.Add('2', "101100101011"); 102 C39_Code.Add('3', "110110010101"); 103 C39_Code.Add('4', "101001101011"); 104 C39_Code.Add('5', "110100110101"); 105 C39_Code.Add('6', "101100110101"); 106 C39_Code.Add('7', "101001011011"); 107 C39_Code.Add('8', "110100101101"); 108 C39_Code.Add('9', "101100101101"); 109 C39_Code.Add('A', "110101001011"); 110 C39_Code.Add('B', "101101001011"); 111 C39_Code.Add('C', "110110100101"); 112 C39_Code.Add('D', "101011001011"); 113 C39_Code.Add('E', "110101100101"); 114 C39_Code.Add('F', "101101100101"); 115 C39_Code.Add('G', "101010011011"); 116 C39_Code.Add('H', "110101001101"); 117 C39_Code.Add('I', "101101001101"); 118 C39_Code.Add('J', "101011001101"); 119 C39_Code.Add('K', "110101010011"); 120 C39_Code.Add('L', "101101010011"); 121 C39_Code.Add('M', "110110101001"); 122 C39_Code.Add('N', "101011010011"); 123 C39_Code.Add('O', "110101101001"); 124 C39_Code.Add('P', "101101101001"); 125 C39_Code.Add('Q', "101010110011"); 126 C39_Code.Add('R', "110101011001"); 127 C39_Code.Add('S', "101101011001"); 128 C39_Code.Add('T', "101011011001"); 129 C39_Code.Add('U', "110010101011"); 130 C39_Code.Add('V', "100110101011"); 131 C39_Code.Add('W', "110011010101"); 132 C39_Code.Add('X', "100101101011"); 133 C39_Code.Add('Y', "110010110101"); 134 C39_Code.Add('Z', "100110110101"); 135 C39_Code.Add('-', "100101011011"); 136 C39_Code.Add('.', "110010101101"); 137 C39_Code.Add(' ', "100110101101"); 138 C39_Code.Add('$', "100100100101"); 139 C39_Code.Add('/', "100100101001"); 140 C39_Code.Add('+', "100101001001"); 141 C39_Code.Add('%', "101001001001"); 142 C39_Code.Add('*', "100101101101"); 143 }//init_Code39 144 private void init_ExtendedCode39() 145 { 146 ExtC39_Translation.Clear(); 147 ExtC39_Translation.Add(Convert.ToChar(0).ToString(), "%U"); 148 ExtC39_Translation.Add(Convert.ToChar(1).ToString(), "$A"); 149 ExtC39_Translation.Add(Convert.ToChar(2).ToString(), "$B"); 150 ExtC39_Translation.Add(Convert.ToChar(3).ToString(), "$C"); 151 ExtC39_Translation.Add(Convert.ToChar(4).ToString(), "$D"); 152 ExtC39_Translation.Add(Convert.ToChar(5).ToString(), "$E"); 153 ExtC39_Translation.Add(Convert.ToChar(6).ToString(), "$F"); 154 ExtC39_Translation.Add(Convert.ToChar(7).ToString(), "$G"); 155 ExtC39_Translation.Add(Convert.ToChar(8).ToString(), "$H"); 156 ExtC39_Translation.Add(Convert.ToChar(9).ToString(), "$I"); 157 ExtC39_Translation.Add(Convert.ToChar(10).ToString(), "$J"); 158 ExtC39_Translation.Add(Convert.ToChar(11).ToString(), "$K"); 159 ExtC39_Translation.Add(Convert.ToChar(12).ToString(), "$L"); 160 ExtC39_Translation.Add(Convert.ToChar(13).ToString(), "$M"); 161 ExtC39_Translation.Add(Convert.ToChar(14).ToString(), "$N"); 162 ExtC39_Translation.Add(Convert.ToChar(15).ToString(), "$O"); 163 ExtC39_Translation.Add(Convert.ToChar(16).ToString(), "$P"); 164 ExtC39_Translation.Add(Convert.ToChar(17).ToString(), "$Q"); 165 ExtC39_Translation.Add(Convert.ToChar(18).ToString(), "$R"); 166 ExtC39_Translation.Add(Convert.ToChar(19).ToString(), "$S"); 167 ExtC39_Translation.Add(Convert.ToChar(20).ToString(), "$T"); 168 ExtC39_Translation.Add(Convert.ToChar(21).ToString(), "$U"); 169 ExtC39_Translation.Add(Convert.ToChar(22).ToString(), "$V"); 170 ExtC39_Translation.Add(Convert.ToChar(23).ToString(), "$W"); 171 ExtC39_Translation.Add(Convert.ToChar(24).ToString(), "$X"); 172 ExtC39_Translation.Add(Convert.ToChar(25).ToString(), "$Y"); 173 ExtC39_Translation.Add(Convert.ToChar(26).ToString(), "$Z"); 174 ExtC39_Translation.Add(Convert.ToChar(27).ToString(), "%A"); 175 ExtC39_Translation.Add(Convert.ToChar(28).ToString(), "%B"); 176 ExtC39_Translation.Add(Convert.ToChar(29).ToString(), "%C"); 177 ExtC39_Translation.Add(Convert.ToChar(30).ToString(), "%D"); 178 ExtC39_Translation.Add(Convert.ToChar(31).ToString(), "%E"); 179 ExtC39_Translation.Add("!", "/A"); 180 ExtC39_Translation.Add("\"", "/B"); 181 ExtC39_Translation.Add("#", "/C"); 182 ExtC39_Translation.Add("$", "/D"); 183 ExtC39_Translation.Add("%", "/E"); 184 ExtC39_Translation.Add("&", "/F"); 185 ExtC39_Translation.Add("'", "/G"); 186 ExtC39_Translation.Add("(", "/H"); 187 ExtC39_Translation.Add(")", "/I"); 188 ExtC39_Translation.Add("*", "/J"); 189 ExtC39_Translation.Add("+", "/K"); 190 ExtC39_Translation.Add(",", "/L"); 191 ExtC39_Translation.Add("/", " 全部评论
专题导读
上一篇:extern"C"与extern"C"{…}的差别发布时间:2022-07-13下一篇:[No0000FA]C# 接口(Interface)发布时间:2022-07-13热门推荐
热门话题
阅读排行榜
|
请发表评论