BarCodeToHTML.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. using System.Collections;
  2. using System.Text.RegularExpressions;
  3. namespace Ant.Service.Utilities
  4. {
  5. public class BarCodeToHTML
  6. {
  7. public static string get39(string s, int width, int height)
  8. {
  9. Hashtable ht = new Hashtable();
  10. #region 39码 12位
  11. ht.Add('A', "110101001011");
  12. ht.Add('B', "101101001011");
  13. ht.Add('C', "110110100101");
  14. ht.Add('D', "101011001011");
  15. ht.Add('E', "110101100101");
  16. ht.Add('F', "101101100101");
  17. ht.Add('G', "101010011011");
  18. ht.Add('H', "110101001101");
  19. ht.Add('I', "101101001101");
  20. ht.Add('J', "101011001101");
  21. ht.Add('K', "110101010011");
  22. ht.Add('L', "101101010011");
  23. ht.Add('M', "110110101001");
  24. ht.Add('N', "101011010011");
  25. ht.Add('O', "110101101001");
  26. ht.Add('P', "101101101001");
  27. ht.Add('Q', "101010110011");
  28. ht.Add('R', "110101011001");
  29. ht.Add('S', "101101011001");
  30. ht.Add('T', "101011011001");
  31. ht.Add('U', "110010101011");
  32. ht.Add('V', "100110101011");
  33. ht.Add('W', "110011010101");
  34. ht.Add('X', "100101101011");
  35. ht.Add('Y', "110010110101");
  36. ht.Add('Z', "100110110101");
  37. ht.Add('0', "101001101101");
  38. ht.Add('1', "110100101011");
  39. ht.Add('2', "101100101011");
  40. ht.Add('3', "110110010101");
  41. ht.Add('4', "101001101011");
  42. ht.Add('5', "110100110101");
  43. ht.Add('6', "101100110101");
  44. ht.Add('7', "101001011011");
  45. ht.Add('8', "110100101101");
  46. ht.Add('9', "101100101101");
  47. ht.Add('+', "100101001001");
  48. ht.Add('-', "100101011011");
  49. ht.Add('*', "100101101101");
  50. ht.Add('/', "100100101001");
  51. ht.Add('%', "101001001001");
  52. ht.Add('$', "100100100101");
  53. ht.Add('.', "110010101101");
  54. ht.Add(' ', "100110101101");
  55. #endregion
  56. #region 39码 9位
  57. //ht.Add('0', "000110100");
  58. //ht.Add('1', "100100001");
  59. //ht.Add('2', "001100001");
  60. //ht.Add('3', "101100000");
  61. //ht.Add('4', "000110001");
  62. //ht.Add('5', "100110000");
  63. //ht.Add('6', "001110000");
  64. //ht.Add('7', "000100101");
  65. //ht.Add('8', "100100100");
  66. //ht.Add('9', "001100100");
  67. //ht.Add('A', "100001001");
  68. //ht.Add('B', "001001001");
  69. //ht.Add('C', "101001000");
  70. //ht.Add('D', "000011001");
  71. //ht.Add('E', "100011000");
  72. //ht.Add('F', "001011000");
  73. //ht.Add('G', "000001101");
  74. //ht.Add('H', "100001100");
  75. //ht.Add('I', "001001100");
  76. //ht.Add('J', "000011100");
  77. //ht.Add('K', "100000011");
  78. //ht.Add('L', "001000011");
  79. //ht.Add('M', "101000010");
  80. //ht.Add('N', "000010011");
  81. //ht.Add('O', "100010010");
  82. //ht.Add('P', "001010010");
  83. //ht.Add('Q', "000000111");
  84. //ht.Add('R', "100000110");
  85. //ht.Add('S', "001000110");
  86. //ht.Add('T', "000010110");
  87. //ht.Add('U', "110000001");
  88. //ht.Add('V', "011000001");
  89. //ht.Add('W', "111000000");
  90. //ht.Add('X', "010010001");
  91. //ht.Add('Y', "110010000");
  92. //ht.Add('Z', "011010000");
  93. //ht.Add('-', "010000101");
  94. //ht.Add('.', "110000100");
  95. //ht.Add(' ', "011000100");
  96. //ht.Add('*', "010010100");
  97. //ht.Add('$', "010101000");
  98. //ht.Add('/', "010100010");
  99. //ht.Add('+', "010001010");
  100. //ht.Add('%', "000101010");
  101. #endregion
  102. s = "*" + s.ToUpper() + "*";
  103. string result_bin = "";//二进制串
  104. try
  105. {
  106. foreach (char ch in s)
  107. {
  108. result_bin += ht[ch].ToString();
  109. result_bin += "0";//间隔,与一个单位的线条宽度相等
  110. }
  111. }
  112. catch { return "存在不允许的字符!"; }
  113. string result_html = ""; //HTML代码
  114. string color = ""; //颜色
  115. foreach (char c in result_bin)
  116. {
  117. color = c == '0' ? "#FFFFFF" : "#000000";
  118. result_html += "<div style=\"width:" + width + "px;height:" + height + "px;float:left;background:" + color + ";\"></div>";
  119. }
  120. result_html += "<div style=\"clear:both\"></div>";
  121. int len = ht['*'].ToString().Length;
  122. foreach (char c in s)
  123. {
  124. result_html += "<div style=\"width:" + (width * (len + 1)) + "px;float:left;color:#000000;text-align:center;\">" + c + "</div>";
  125. }
  126. result_html += "<div style=\"clear:both\"></div>";
  127. return "<div style=\"background:#FFFFFF;padding:5px;font-size:" + (width * 10) + "px;font-family:'楷体';\">" + result_html + "</div>";
  128. }
  129. public static string getEAN13(string s, int width, int height)
  130. {
  131. int checkcode_input = -1;//输入的校验码
  132. if (!Regex.IsMatch(s, @"^\d{12}$"))
  133. {
  134. if (!Regex.IsMatch(s, @"^\d{13}$"))
  135. {
  136. return "存在不允许的字符!";
  137. }
  138. else
  139. {
  140. checkcode_input = int.Parse(s[12].ToString());
  141. s = s.Substring(0, 12);
  142. }
  143. }
  144. int sum_even = 0;//偶数位之和
  145. int sum_odd = 0; //奇数位之和
  146. for (int i = 0; i < 12; i++)
  147. {
  148. if (i % 2 == 0)
  149. {
  150. sum_odd += int.Parse(s[i].ToString());
  151. }
  152. else
  153. {
  154. sum_even += int.Parse(s[i].ToString());
  155. }
  156. }
  157. int checkcode = (10 - (sum_even * 3 + sum_odd) % 10) % 10;//校验码
  158. if (checkcode_input > 0 && checkcode_input != checkcode)
  159. {
  160. return "输入的校验码错误!";
  161. }
  162. s += checkcode;//变成13位
  163. // 000000000101左侧42个01010右侧35个校验7个101000000000
  164. // 6 101左侧6位 01010右侧5位校验1位101000000000
  165. string result_bin = "";//二进制串
  166. result_bin += "000000000101";
  167. string type = ean13type(s[0]);
  168. for (int i = 1; i < 7; i++)
  169. {
  170. result_bin += ean13(s[i], type[i - 1]);
  171. }
  172. result_bin += "01010";
  173. for (int i = 7; i < 13; i++)
  174. {
  175. result_bin += ean13(s[i], 'C');
  176. }
  177. result_bin += "101000000000";
  178. string result_html = ""; //HTML代码
  179. string color = ""; //颜色
  180. int height_bottom = width * 5;
  181. foreach (char c in result_bin)
  182. {
  183. color = c == '0' ? "#FFFFFF" : "#000000";
  184. result_html += "<div style=\"width:" + width + "px;height:" + height + "px;float:left;background:" + color + ";\"></div>";
  185. }
  186. result_html += "<div style=\"clear:both\"></div>";
  187. result_html += "<div style=\"float:left;color:#000000;width:" + (width * 9) + "px;text-align:center;\">" + s[0] + "</div>";
  188. result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#000000;\"></div>";
  189. result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;\"></div>";
  190. result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#000000;\"></div>";
  191. for (int i = 1; i < 7; i++)
  192. {
  193. result_html += "<div style=\"float:left;width:" + (width * 7) + "px;color:#000000;text-align:center;\">" + s[i] + "</div>";
  194. }
  195. result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;\"></div>";
  196. result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#000000;\"></div>";
  197. result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;\"></div>";
  198. result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#000000;\"></div>";
  199. result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;\"></div>";
  200. for (int i = 7; i < 13; i++)
  201. {
  202. result_html += "<div style=\"float:left;width:" + (width * 7) + "px;color:#000000;text-align:center;\">" + s[i] + "</div>";
  203. }
  204. result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#000000;\"></div>";
  205. result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;\"></div>";
  206. result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#000000;\"></div>";
  207. result_html += "<div style=\"float:left;color:#000000;width:" + (width * 9) + "px;\"></div>";
  208. result_html += "<div style=\"clear:both\"></div>";
  209. return "<div style=\"background:#FFFFFF;padding:0px;font-size:" + (width * 10) + "px;font-family:'楷体';\">" + result_html + "</div>";
  210. }
  211. private static string ean13(char c, char type)
  212. {
  213. switch (type)
  214. {
  215. case 'A':
  216. {
  217. switch (c)
  218. {
  219. case '0': return "0001101";
  220. case '1': return "0011001";
  221. case '2': return "0010011";
  222. case '3': return "0111101";//011101
  223. case '4': return "0100011";
  224. case '5': return "0110001";
  225. case '6': return "0101111";
  226. case '7': return "0111011";
  227. case '8': return "0110111";
  228. case '9': return "0001011";
  229. default: return "Error!";
  230. }
  231. }
  232. case 'B':
  233. {
  234. switch (c)
  235. {
  236. case '0': return "0100111";
  237. case '1': return "0110011";
  238. case '2': return "0011011";
  239. case '3': return "0100001";
  240. case '4': return "0011101";
  241. case '5': return "0111001";
  242. case '6': return "0000101";//000101
  243. case '7': return "0010001";
  244. case '8': return "0001001";
  245. case '9': return "0010111";
  246. default: return "Error!";
  247. }
  248. }
  249. case 'C':
  250. {
  251. switch (c)
  252. {
  253. case '0': return "1110010";
  254. case '1': return "1100110";
  255. case '2': return "1101100";
  256. case '3': return "1000010";
  257. case '4': return "1011100";
  258. case '5': return "1001110";
  259. case '6': return "1010000";
  260. case '7': return "1000100";
  261. case '8': return "1001000";
  262. case '9': return "1110100";
  263. default: return "Error!";
  264. }
  265. }
  266. default: return "Error!";
  267. }
  268. }
  269. private static string ean13type(char c)
  270. {
  271. switch (c)
  272. {
  273. case '0': return "AAAAAA";
  274. case '1': return "AABABB";
  275. case '2': return "AABBAB";
  276. case '3': return "AABBBA";
  277. case '4': return "ABAABB";
  278. case '5': return "ABBAAB";
  279. case '6': return "ABBBAA";//中国
  280. case '7': return "ABABAB";
  281. case '8': return "ABABBA";
  282. case '9': return "ABBABA";
  283. default: return "Error!";
  284. }
  285. }
  286. }
  287. }