BarCodeToHTML.cs 13 KB

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