GS1Helper.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using System.Collections.Generic;
  2. namespace FastReport.Barcode
  3. {
  4. internal static class GS1Helper
  5. {
  6. struct AI
  7. {
  8. public string numAI;
  9. public int numAILength;
  10. public int minDataLength;
  11. public int maxDataLength;
  12. public bool useFNC1;
  13. public AI(string numAI, int numAILength, int minDataLength, int maxDataLength, bool useFNC1)
  14. {
  15. this.numAI = numAI;
  16. this.numAILength = numAILength;
  17. this.minDataLength = minDataLength;
  18. this.maxDataLength = maxDataLength;
  19. this.useFNC1 = useFNC1;
  20. }
  21. }
  22. static List<AI> AICodes = new List<AI>()
  23. {
  24. new AI("00", 2, 18, 18, false),
  25. new AI("01", 2, 14, 14, false),
  26. new AI("02", 2, 14, 14, false),
  27. new AI("10", 2, 1, 20, true),
  28. new AI("11", 2, 6, 6, false),
  29. new AI("12", 2, 6, 6, false),
  30. new AI("13", 2, 6, 6, false),
  31. new AI("15", 2, 6, 6, false),
  32. new AI("16", 2, 6, 6, false),
  33. new AI("17", 2, 6, 6, false),
  34. new AI("20", 2, 2, 2, false),
  35. new AI("21", 2, 1, 20, true),
  36. new AI("22", 2, 1, 20, true),
  37. new AI("240", 3, 1, 30, true),
  38. new AI("241", 3, 1, 30, true),
  39. new AI("242", 3, 1, 6, true),
  40. new AI("243", 3, 1, 20, true),
  41. new AI("250", 3, 1, 30, true),
  42. new AI("251", 3, 1, 30, true),
  43. new AI("253", 3, 13, 30, true),
  44. new AI("254", 3, 1, 20, true),
  45. new AI("255", 3, 13, 25, true),
  46. new AI("30", 2, 1, 8, true),
  47. new AI("31XX", 4, 6, 6, false), // all 31xx
  48. new AI("32XX", 4, 6, 6, false), // all 32xx
  49. new AI("33XX", 4, 6, 6, false), // all 33xx
  50. new AI("34XX", 4, 6, 6, false), // all 34xx
  51. new AI("35XX", 4, 6, 6, false), // all 35xx
  52. new AI("36XX", 4, 6, 6, false), // all 36xx
  53. new AI("37", 2, 1, 8, true),
  54. new AI("390X", 4, 1, 15, true),
  55. new AI("391X", 4, 3, 18, true),
  56. new AI("392X", 4, 1, 15, true),
  57. new AI("393X", 4, 3, 18, true),
  58. new AI("394X", 4, 4, 4, true),
  59. new AI("400", 3, 1, 30, true),
  60. new AI("401", 3, 1, 30, true),
  61. new AI("402", 3, 17, 17, true),
  62. new AI("403", 3, 1, 30, true),
  63. new AI("41X", 3, 13, 13, false),
  64. new AI("420", 3, 1, 20, true),
  65. new AI("421", 3, 3, 12, true),
  66. new AI("422", 3, 3, 3, true),
  67. new AI("423", 3, 3, 15, true),
  68. new AI("424", 3, 3, 3, true),
  69. new AI("425", 3, 3, 15, true),
  70. new AI("426", 3, 3, 3, true),
  71. new AI("7001", 4, 13, 13, true),
  72. new AI("7002", 4, 1, 30, true),
  73. new AI("7003", 4, 10, 10, true),
  74. new AI("7004", 4, 1, 4, true),
  75. new AI("7005", 4, 1, 12, true),
  76. new AI("7006", 4, 6, 6, true),
  77. new AI("7007", 4, 6, 12, true),
  78. new AI("7008", 4, 1, 3, true),
  79. new AI("7009", 4, 1, 10, true),
  80. new AI("7010", 4, 1, 2, true),
  81. new AI("7020", 4, 1, 20, true),
  82. new AI("7021", 4, 1, 20, true),
  83. new AI("7022", 4, 1, 20, true),
  84. new AI("7023", 4, 1, 30, true),
  85. new AI("703X", 4, 3, 30, true),
  86. new AI("710", 3, 1, 20, true),
  87. new AI("711", 3, 1, 20, true),
  88. new AI("712", 3, 1, 20, true),
  89. new AI("713", 3, 1, 20, true),
  90. new AI("71X", 3, 1, 20, true),
  91. new AI("8001", 4, 14, 14, true),
  92. new AI("8002", 4, 1, 20, true),
  93. new AI("8003", 4, 14, 30, true),
  94. new AI("8004", 4, 1, 30, true),
  95. new AI("8005", 4, 6, 6, true),
  96. new AI("8006", 4, 18, 18, true),
  97. new AI("8007", 4, 1, 34, true),
  98. new AI("8008", 4, 8, 12, true),
  99. new AI("8010", 4, 1, 30, true),
  100. new AI("8011", 4, 1, 12, true),
  101. new AI("8012", 4, 1, 20, true),
  102. new AI("8013", 4, 1, 30, true),
  103. new AI("8017", 4, 18, 18, true),
  104. new AI("8018", 4, 18, 18, true),
  105. new AI("8019", 4, 1, 10, true),
  106. new AI("8020", 4, 1, 25, true),
  107. new AI("8110", 4, 1, 70, true),
  108. new AI("8111", 4, 4, 4, true),
  109. new AI("8112", 4, 1, 70, true),
  110. new AI("8200", 4, 1, 70, true),
  111. new AI("90", 2, 1, 30, true),
  112. new AI("9X", 2, 1, 90, true)
  113. };
  114. static int FindAIIndex(string code, int index)
  115. {
  116. int codeLen, maxLen, result;
  117. result = -1;
  118. if (index == -1)
  119. return -1;
  120. if (code[index] != '(')
  121. return result;
  122. codeLen = code.Length - index;
  123. if (codeLen < 3)
  124. return result;
  125. for (int i = 0; i < AICodes.Count; i++)
  126. {
  127. result = -1;
  128. maxLen = AICodes[i].numAI.Length;
  129. if (maxLen > codeLen)
  130. continue;
  131. for (int j = 0; j < maxLen; j++)
  132. {
  133. result = i;
  134. if ((AICodes[i].numAI[j] != code[index + j + 1]) && (AICodes[i].numAI[j] != 'X'))
  135. {
  136. result = -1;
  137. break;
  138. }
  139. }
  140. if (result != -1)
  141. break;
  142. }
  143. return result;
  144. }
  145. static string GetCode(string code, ref int index)
  146. {
  147. int foundAI, maxLen, codeLen, tempIndex;
  148. string result = "";
  149. foundAI = FindAIIndex(code, index);
  150. if (foundAI == -1)
  151. return "";
  152. index += AICodes[foundAI].numAI.Length + 1;
  153. if (code[index] != ')')
  154. return "";
  155. codeLen = code.Length - index - 1;
  156. index++;
  157. if (!AICodes[foundAI].useFNC1 && (codeLen >= AICodes[foundAI].maxDataLength))
  158. {
  159. result = code.Substring(index - AICodes[foundAI].numAILength - 1, AICodes[foundAI].numAILength) +
  160. code.Substring(index, AICodes[foundAI].maxDataLength);
  161. index += AICodes[foundAI].maxDataLength;
  162. }
  163. else if (AICodes[foundAI].useFNC1)
  164. {
  165. maxLen = codeLen;
  166. tempIndex = code.IndexOf('(', index);
  167. if (tempIndex != -1)
  168. maxLen = tempIndex - index;
  169. if (maxLen < 0)
  170. maxLen = codeLen;
  171. if ((maxLen >= AICodes[foundAI].minDataLength) && (maxLen <= AICodes[foundAI].maxDataLength))
  172. {
  173. result = code.Substring(index - AICodes[foundAI].numAILength - 1, AICodes[foundAI].numAILength) +
  174. code.Substring(index, maxLen);
  175. if (maxLen < codeLen)
  176. result += "&1;";
  177. index += maxLen;
  178. }
  179. }
  180. return result;
  181. }
  182. public static string ParseGS1(string code)
  183. {
  184. var result = "";
  185. if (code.Length < 3)
  186. return result;
  187. var str = "";
  188. var i = 0;
  189. result = "&1;";
  190. while (i < code.Length)
  191. {
  192. str = GetCode(code, ref i);
  193. if (str != "")
  194. result += str;
  195. else
  196. {
  197. result = "";
  198. return result;
  199. }
  200. }
  201. return result;
  202. }
  203. }
  204. }