image.codec\barcode\linear_barcodes.mbt

92.1% · 139 / 151 points · 581 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

  1. 1///|
  2. 2/// # 线性条码 barcode/linear_barcodes
  3. 3///
  4. 4/// 编解码工具模块
  5. 5/// 编解码工具模块 Codec utility module
  6. 6// 更多一维码支持(v11.3.0)
  7. 7// Code128 / UPC-A / Code39 解码
  8. 8
  9. 9///|
  10. 10/// Code128 字符集类型
  11. 11#derive(Eq, @debug.Debug)
  12. 12pub(all) enum Code128Charset {
  13. 13 CodeA
  14. 14 CodeB
  15. 15 CodeC
  16. 16}
  17. 17
  18. 18///|
  19. 19pub extend Code128Charset with Eq::{not_equal, equal}
  20. 20
  21. 21///|
  22. 22pub extend Code128Charset with @debug.Debug::{to_repr}
  23. 23
  24. 24///|
  25. 25/// Code128 编码表(107个码字)
  26. 26/// 每个码字对应 11 位的条空模式(3条3空,宽度1-4)
  27. 27let code128_patterns : Array[Array[Int32]] = [
  28. 28 [2, 1, 2, 2, 2, 2], // 0
  29. 29 [2, 2, 2, 1, 2, 2], // 1
  30. 30 [2, 2, 2, 2, 2, 1], // 2
  31. 31 [1, 2, 1, 2, 2, 3], // 3
  32. 32 [1, 2, 1, 3, 2, 2], // 4
  33. 33 [1, 3, 1, 2, 2, 2], // 5
  34. 34 [1, 2, 2, 2, 1, 3], // 6
  35. 35 [1, 2, 2, 3, 1, 2], // 7
  36. 36 [1, 3, 2, 2, 1, 2], // 8
  37. 37 [2, 2, 1, 2, 1, 3], // 9
  38. 38 [2, 2, 1, 3, 1, 2], // 10
  39. 39 [2, 3, 1, 2, 1, 2], // 11
  40. 40 [1, 1, 2, 2, 3, 2], // 12
  41. 41 [1, 2, 2, 1, 3, 2], // 13
  42. 42 [1, 2, 2, 2, 3, 1], // 14
  43. 43 [1, 1, 3, 2, 2, 2], // 15
  44. 44 [1, 2, 3, 1, 2, 2], // 16
  45. 45 [1, 2, 3, 2, 2, 1], // 17
  46. 46 [2, 2, 3, 2, 1, 1], // 18
  47. 47 [2, 2, 1, 1, 3, 2], // 19
  48. 48 [2, 2, 1, 2, 3, 1], // 20
  49. 49 [2, 1, 3, 2, 1, 2], // 21
  50. 50 [2, 2, 3, 1, 1, 2], // 22
  51. 51 [3, 1, 2, 1, 3, 1], // 23
  52. 52 [3, 1, 1, 2, 2, 2], // 24
  53. 53 [3, 2, 1, 1, 2, 2], // 25
  54. 54 [3, 2, 1, 2, 2, 1], // 26
  55. 55 [3, 1, 2, 2, 1, 2], // 27
  56. 56 [3, 2, 2, 1, 1, 2], // 28
  57. 57 [3, 2, 2, 2, 1, 1], // 29
  58. 58 [2, 1, 2, 1, 2, 3], // 30
  59. 59 [2, 1, 2, 3, 2, 1], // 31
  60. 60 [2, 3, 2, 1, 2, 1], // 32
  61. 61 [1, 1, 1, 3, 2, 3], // 33
  62. 62 [1, 3, 1, 1, 2, 3], // 34
  63. 63 [1, 3, 1, 3, 2, 1], // 35
  64. 64 [1, 1, 2, 3, 1, 3], // 36
  65. 65 [1, 3, 2, 1, 1, 3], // 37
  66. 66 [1, 3, 2, 3, 1, 1], // 38
  67. 67 [2, 1, 1, 3, 1, 3], // 39
  68. 68 [2, 3, 1, 1, 1, 3], // 40
  69. 69 [2, 3, 1, 3, 1, 1], // 41
  70. 70 [1, 1, 2, 1, 3, 3], // 42
  71. 71 [1, 1, 2, 3, 3, 1], // 43
  72. 72 [1, 3, 2, 1, 3, 1], // 44
  73. 73 [1, 1, 3, 1, 2, 3], // 45
  74. 74 [1, 1, 3, 3, 2, 1], // 46
  75. 75 [1, 3, 3, 1, 2, 1], // 47
  76. 76 [3, 1, 3, 1, 2, 1], // 48
  77. 77 [2, 1, 1, 3, 3, 1], // 49
  78. 78 [2, 3, 1, 1, 3, 1], // 50
  79. 79 [2, 1, 3, 1, 1, 3], // 51
  80. 80 [2, 1, 3, 3, 1, 1], // 52
  81. 81 [2, 1, 3, 1, 3, 1], // 53
  82. 82 [3, 1, 1, 1, 2, 3], // 54
  83. 83 [3, 1, 1, 3, 2, 1], // 55
  84. 84 [3, 3, 1, 1, 2, 1], // 56
  85. 85 [3, 1, 2, 1, 1, 3], // 57
  86. 86 [3, 1, 2, 3, 1, 1], // 58
  87. 87 [3, 3, 2, 1, 1, 1], // 59
  88. 88 [3, 1, 4, 1, 1, 1], // 60
  89. 89 [2, 2, 1, 4, 1, 1], // 61
  90. 90 [4, 3, 1, 1, 1, 1], // 62
  91. 91 [1, 1, 1, 2, 2, 4], // 63
  92. 92 [1, 1, 1, 4, 2, 2], // 64
  93. 93 [1, 2, 1, 1, 2, 4], // 65
  94. 94 [1, 2, 1, 4, 2, 1], // 66
  95. 95 [1, 4, 1, 1, 2, 2], // 67
  96. 96 [1, 4, 1, 2, 2, 1], // 68
  97. 97 [1, 1, 2, 2, 1, 4], // 69
  98. 98 [1, 1, 2, 4, 1, 2], // 70
  99. 99 [1, 2, 2, 1, 1, 4], // 71
  100. 100 [1, 2, 2, 4, 1, 1], // 72
  101. 101 [1, 4, 2, 1, 1, 2], // 73
  102. 102 [1, 4, 2, 2, 1, 1], // 74
  103. 103 [2, 4, 1, 2, 1, 1], // 75
  104. 104 [2, 2, 1, 1, 1, 4], // 76
  105. 105 [4, 1, 3, 1, 1, 1], // 77
  106. 106 [2, 4, 1, 1, 1, 2], // 78
  107. 107 [1, 3, 4, 1, 1, 1], // 79
  108. 108 [1, 1, 1, 2, 4, 2], // 80
  109. 109 [1, 2, 1, 1, 4, 2], // 81
  110. 110 [1, 2, 1, 2, 4, 1], // 82
  111. 111 [1, 1, 4, 2, 1, 2], // 83
  112. 112 [1, 2, 4, 1, 1, 2], // 84
  113. 113 [1, 2, 4, 2, 1, 1], // 85
  114. 114 [4, 1, 1, 2, 1, 2], // 86
  115. 115 [4, 2, 1, 1, 1, 2], // 87
  116. 116 [4, 2, 1, 2, 1, 1], // 88
  117. 117 [2, 1, 2, 1, 4, 1], // 89
  118. 118 [2, 1, 4, 1, 2, 1], // 90
  119. 119 [4, 1, 2, 1, 2, 1], // 91
  120. 120 [1, 1, 1, 1, 4, 3], // 92
  121. 121 [1, 1, 1, 3, 4, 1], // 93
  122. 122 [1, 3, 1, 1, 4, 1], // 94
  123. 123 [1, 1, 4, 1, 1, 3], // 95
  124. 124 [1, 1, 4, 3, 1, 1], // 96
  125. 125 [4, 1, 1, 1, 1, 3], // 97
  126. 126 [4, 1, 1, 3, 1, 1], // 98
  127. 127 [1, 1, 3, 1, 4, 1], // 99
  128. 128 [1, 1, 4, 1, 3, 1], // 100
  129. 129 [3, 1, 1, 1, 4, 1], // 101
  130. 130 [4, 1, 1, 1, 3, 1], // 102
  131. 131 [2, 1, 1, 4, 1, 2], // 103 (Start A)
  132. 132 [2, 1, 1, 2, 1, 4], // 104 (Start B)
  133. 133 [2, 1, 1, 2, 3, 2], // 105 (Start C)
  134. 134]
  135. 135
  136. 136///|
  137. 137/// Code128 Stop 符(码字 106)——7 段 13 模块,与数据符的 6 段 11 模块不同形,
  138. 138/// 因此不能塞进上面的 6 段表。旧实现曾用 [2,1,1,2,2,3](11 模块)冒充 Stop,
  139. 139/// 生成的条码对标准扫码器不可读(S22 修复)。
  140. 140let code128_stop_pattern : Array[Int32] = [2, 3, 3, 1, 1, 1, 2]
  141. 141
  142. 142///|
  143. 143/// Code128 字符集 A 的字符映射(0-95)
  144. 144fn code128_char_a(code : Int32) -> String {
  145. 145● if code >= 0 && code <= 95 {
  146. 146● if code < 64 {
  147. 147● (code + 32).to_char().map(c => c.to_string()).unwrap_or("")
  148. 148 } else {
  149. 149 // 控制字符
  150. 150● (code - 64).to_char().map(c => c.to_string()).unwrap_or("")
  151. 151 }
  152. 152 } else {
  153. 153○ "?"
  154. 154 }
  155. 155}
  156. 156
  157. 157///|
  158. 158/// Code128 字符集 B 的字符映射(0-95)
  159. 159fn code128_char_b(code : Int32) -> String {
  160. 160● if code >= 0 && code <= 95 {
  161. 161● (code + 32).to_char().map(c => c.to_string()).unwrap_or("")
  162. 162 } else {
  163. 163● "?"
  164. 164 }
  165. 165}
  166. 166
  167. 167///|
  168. 168/// 识别 Code128 条形码
  169. 169pub fn decode_code128(
  170. 170 img : @types.Image,
  171. 171) -> BarcodeResult? raise @types.LoadError {
  172. 172 // 输入验证
  173. 173● if img.width <= 0 || img.height <= 0 {
  174. 174● raise @types.LoadError::DecodeFailed(
  175. 175 "decode_code128: 图像尺寸必须大于0",
  176. 176 )
  177. 177 }
  178. 178 if img.channels < 1 || img.channels > 4 {
  179. 179● raise @types.LoadError::DecodeFailed(
  180. 180 "decode_code128: 通道数必须在1-4之间",
  181. 181 )
  182. 182 }
  183. 183 let expected_size = img.width * img.height * img.channels
  184. 184● if img.data.length() != expected_size {
  185. 185● raise @types.LoadError::DecodeFailed(
  186. 186 "decode_code128: 数据大小不匹配",
  187. 187 )
  188. 188 }
  189. 189 let w = img.width
  190. 190 let h = img.height
  191. 191 let data = img.data
  192. 192 // 在中间扫描线尝试解码
  193. 193 let y = h / 2
  194. 194 let scan_line : Array[Int32] = []
  195. 195 for x in 0..<w {
  196. 196● scan_line.push(if data[y * w + x].to_int() < 128 { 1 } else { 0 })
  197. 197 }
  198. 198 // 寻找起始符(Code A/B/C)
  199. 199 let start_patterns = [
  200. 200 (103, code128_patterns[103]), // Start A
  201. 201 (104, code128_patterns[104]), // Start B
  202. 202 (105, code128_patterns[105]), // Start C
  203. 203 ]
  204. 204 let mut start_idx = -1
  205. 205 let mut start_code = 0
  206. 206 for i in 0..<(w - 20) {
  207. 207● for _tup0 in start_patterns {
  208. 208● let (code, pattern) = _tup0
  209. 209● if match_bar_pattern(scan_line, i, pattern) {
  210. 210● start_idx = i
  211. 211 start_code = code
  212. 212 break
  213. 213 }
  214. 214 }
  215. 215 if start_idx >= 0 {
  216. 216● break
  217. 217 }
  218. 218 }
  219. 219 if start_idx < 0 {
  220. 220● return None
  221. 221 }
  222. 222 // 确定字符集
  223. 223● let charset = match start_code {
  224. 224● 103 => Code128Charset::CodeA
  225. 225● 104 => Code128Charset::CodeB
  226. 226● 105 => Code128Charset::CodeC
  227. 227○ _ => Code128Charset::CodeB
  228. 228 }
  229. 229 // 解码数据码字
  230. 230 let mut pos = start_idx + 11 // 起始符占 11 个模块
  231. 231 let codes : Array[Int32] = []
  232. 232 let mut checksum = start_code
  233. 233 let mut code_count = 0
  234. 234 while pos < w - 13 {
  235. 235 // Stop 是 7 段 13 模块,比数据符长,必须先匹配,否则会被当成数据符或直接漏掉
  236. 236● if match_bar_pattern(scan_line, pos, code128_stop_pattern) {
  237. 237● pos += 13
  238. 238 break
  239. 239 }
  240. 240 // 匹配一个码字(6 个条空,11 个模块)
  241. 241● let (matched_code, matched_width) = match_code128_symbol(scan_line, pos)
  242. 242 if matched_code < 0 {
  243. 243● break
  244. 244 }
  245. 245● codes.push(matched_code)
  246. 246 code_count += 1
  247. 247 checksum += matched_code * code_count
  248. 248 pos += matched_width
  249. 249 }
  250. 250● if codes.length() < 2 {
  251. 251● return None
  252. 252 }
  253. 253 // 验证校验位
  254. 254 let expected_checksum = checksum % 103
  255. 255● let actual_checksum = codes[codes.length() - 1]
  256. 256 let valid = expected_checksum == actual_checksum
  257. 257 // 解码数据
  258. 258 let mut result = ""
  259. 259 let mut current_charset = charset
  260. 260● for i in 0..<(codes.length() - 1) {
  261. 261● let code = codes[i]
  262. 262 // 处理字符集切换
  263. 263 if code == 101 { // Code A
  264. 264● current_charset = Code128Charset::CodeA
  265. 265 continue
  266. 266● } else if code == 100 { // Code B
  267. 267● current_charset = Code128Charset::CodeB
  268. 268 continue
  269. 269● } else if code == 99 { // Code C
  270. 270● current_charset = Code128Charset::CodeC
  271. 271 continue
  272. 272 }
  273. 273● match current_charset {
  274. 274● Code128Charset::CodeA => result += code128_char_a(code)
  275. 275● Code128Charset::CodeB => result += code128_char_b(code)
  276. 276● Code128Charset::CodeC => {
  277. 277 // Code C: 两个数字一个码字
  278. 278 let d1 = code / 10
  279. 279 let d2 = code % 10
  280. 280● result += d1.to_string()
  281. 281● result += d2.to_string()
  282. 282 }
  283. 283 }
  284. 284 }
  285. 285● let confidence = if valid { 0.9 } else { 0.5 }
  286. 286 Some(BarcodeResult::{
  287. 287 barcode_type: BarcodeType::Code128,
  288. 288 data: result,
  289. 289 confidence,
  290. 290 x: start_idx,
  291. 291 y,
  292. 292 width: pos - start_idx,
  293. 293 height: 1,
  294. 294 })
  295. 295}
  296. 296
  297. 297///|
  298. 298/// 匹配条空模式
  299. 299fn match_bar_pattern(
  300. 300 scan_line : Array[Int32],
  301. 301 start : Int32,
  302. 302 pattern : Array[Int32],
  303. 303) -> Bool {
  304. 304● let mut pos = start
  305. 305 let mut is_bar = true
  306. 306 for width in pattern {
  307. 307● for _ in 0..<width {
  308. 308● if pos >= scan_line.length() {
  309. 309○ return false
  310. 310 }
  311. 311● let expected = if is_bar { 1 } else { 0 }
  312. 312 if scan_line[pos] != expected {
  313. 313● return false
  314. 314 }
  315. 315 pos += 1
  316. 316 }
  317. 317 is_bar = !is_bar
  318. 318 }
  319. 319 true
  320. 320}
  321. 321
  322. 322///|
  323. 323/// 匹配 Code128 码字
  324. 324fn match_code128_symbol(
  325. 325 scan_line : Array[Int32],
  326. 326 start : Int32,
  327. 327) -> (Int32, Int32) {
  328. 328 // 尝试匹配 106 个数据/起始/切换码字(0–105);Stop 不在此表,见 code128_stop_pattern
  329. 329● for code in 0..<106 {
  330. 330● let pattern = code128_patterns[code]
  331. 331 let mut total_width = 0
  332. 332 for w in pattern {
  333. 333● total_width += w
  334. 334 }
  335. 335● if match_bar_pattern(scan_line, start, pattern) {
  336. 336● return (code, total_width)
  337. 337 }
  338. 338 }
  339. 339 (-1, 0)
  340. 340}
  341. 341
  342. 342///|
  343. 343/// 识别 UPC-A 条形码
  344. 344pub fn decode_upca(img : @types.Image) -> BarcodeResult? raise @types.LoadError {
  345. 345 // 输入验证
  346. 346● if img.width <= 0 || img.height <= 0 {
  347. 347● raise @types.LoadError::DecodeFailed(
  348. 348 "decode_upca: 图像尺寸必须大于0",
  349. 349 )
  350. 350 }
  351. 351 if img.channels < 1 || img.channels > 4 {
  352. 352● raise @types.LoadError::DecodeFailed(
  353. 353 "decode_upca: 通道数必须在1-4之间",
  354. 354 )
  355. 355 }
  356. 356 let expected_size = img.width * img.height * img.channels
  357. 357● if img.data.length() != expected_size {
  358. 358● raise @types.LoadError::DecodeFailed("decode_upca: 数据大小不匹配")
  359. 359 }
  360. 360 // UPC-A 与 EAN-13 兼容(UPC-A 是 EAN-13 第一位为 0 的特例)
  361. 361● let result = decode_ean13(img)
  362. 362● match result {
  363. 363 Some(r) =>
  364. 364● if r.data.length() == 13 && r.data[0:1].to_owned() == "0" {
  365. 365 // 去掉前导 0,转换为 UPC-A(用切片避免 String 索引的 Byte/Char 差异)
  366. 366● let upca = r.data[1:13].to_owned()
  367. 367 Some(BarcodeResult::{
  368. 368 barcode_type: BarcodeType::UpcA,
  369. 369 data: upca,
  370. 370 confidence: r.confidence,
  371. 371 x: r.x,
  372. 372 y: r.y,
  373. 373 width: r.width,
  374. 374 height: r.height,
  375. 375 })
  376. 376 } else {
  377. 377○ None
  378. 378 }
  379. 379● None => None
  380. 380 }
  381. 381}
  382. 382
  383. 383///|
  384. 384/// Code39 编码表
  385. 385let code39_chars : Array[String] = [
  386. 386 "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F",
  387. 387 "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
  388. 388 "W", "X", "Y", "Z", "-", ".", " ", "$", "/", "+", "%",
  389. 389]
  390. 390
  391. 391///|
  392. 392/// Code39 条空模式(9个元素:5条4空,3宽6窄)
  393. 393let code39_patterns : Array[Array[Int32]] = [
  394. 394 [1, 1, 1, 2, 2, 1, 2, 1, 1], // 0
  395. 395 [2, 1, 1, 2, 1, 1, 1, 1, 2], // 1
  396. 396 [1, 1, 2, 2, 1, 1, 1, 1, 2], // 2
  397. 397 [2, 1, 2, 2, 1, 1, 1, 1, 1], // 3
  398. 398 [1, 1, 1, 2, 2, 1, 1, 1, 2], // 4
  399. 399 [2, 1, 1, 2, 2, 1, 1, 1, 1], // 5
  400. 400 [1, 1, 2, 2, 2, 1, 1, 1, 1], // 6
  401. 401 [1, 1, 1, 2, 1, 1, 2, 1, 2], // 7
  402. 402 [2, 1, 1, 2, 1, 1, 2, 1, 1], // 8
  403. 403 [1, 1, 2, 2, 1, 1, 2, 1, 1], // 9
  404. 404 [2, 1, 1, 1, 1, 2, 1, 1, 2], // A
  405. 405 [1, 1, 2, 1, 1, 2, 1, 1, 2], // B
  406. 406 [2, 1, 2, 1, 1, 2, 1, 1, 1], // C
  407. 407 [1, 1, 1, 1, 2, 2, 1, 1, 2], // D
  408. 408 [2, 1, 1, 1, 2, 2, 1, 1, 1], // E
  409. 409 [1, 1, 2, 1, 2, 2, 1, 1, 1], // F
  410. 410 [1, 1, 1, 1, 1, 2, 2, 1, 2], // G
  411. 411 [2, 1, 1, 1, 1, 2, 2, 1, 1], // H
  412. 412 [1, 1, 2, 1, 1, 2, 2, 1, 1], // I
  413. 413 [1, 1, 1, 1, 2, 2, 2, 1, 1], // J
  414. 414 [2, 1, 1, 1, 1, 1, 1, 2, 2], // K
  415. 415 [1, 1, 2, 1, 1, 1, 1, 2, 2], // L
  416. 416 [2, 1, 2, 1, 1, 1, 1, 2, 1], // M
  417. 417 [1, 1, 1, 1, 2, 1, 1, 2, 2], // N
  418. 418 [2, 1, 1, 1, 2, 1, 1, 2, 1], // O
  419. 419 [1, 1, 2, 1, 2, 1, 1, 2, 1], // P
  420. 420 [1, 1, 1, 1, 1, 1, 2, 2, 2], // Q
  421. 421 [2, 1, 1, 1, 1, 1, 2, 2, 1], // R
  422. 422 [1, 1, 2, 1, 1, 1, 2, 2, 1], // S
  423. 423 [1, 1, 1, 1, 2, 1, 2, 2, 1], // T
  424. 424 [2, 2, 1, 1, 1, 1, 1, 1, 2], // U
  425. 425 [1, 2, 2, 1, 1, 1, 1, 1, 2], // V
  426. 426 [2, 2, 2, 1, 1, 1, 1, 1, 1], // W
  427. 427 [1, 2, 1, 1, 2, 1, 1, 1, 2], // X
  428. 428 [2, 2, 1, 1, 2, 1, 1, 1, 1], // Y
  429. 429 [1, 2, 2, 1, 2, 1, 1, 1, 1], // Z
  430. 430 [1, 2, 1, 1, 1, 1, 2, 1, 2], // -
  431. 431 [2, 2, 1, 1, 1, 1, 2, 1, 1], // .
  432. 432 [1, 2, 2, 1, 1, 1, 2, 1, 1], // (space)
  433. 433 [1, 2, 1, 2, 1, 2, 1, 1, 1], // $
  434. 434 [1, 2, 1, 2, 1, 1, 1, 2, 1], // /
  435. 435 [1, 2, 1, 1, 1, 2, 1, 2, 1], // +
  436. 436 [1, 1, 1, 2, 1, 2, 1, 2, 1], // %
  437. 437]
  438. 438
  439. 439///|
  440. 440/// Code39 起始/终止符模式(*)
  441. 441let code39_start_pattern : Array[Int32] = [1, 2, 1, 1, 2, 1, 2, 1, 1]
  442. 442
  443. 443///|
  444. 444/// 识别 Code39 条形码
  445. 445pub fn decode_code39(
  446. 446 img : @types.Image,
  447. 447) -> BarcodeResult? raise @types.LoadError {
  448. 448 // 输入验证
  449. 449● if img.width <= 0 || img.height <= 0 {
  450. 450● raise @types.LoadError::DecodeFailed(
  451. 451 "decode_code39: 图像尺寸必须大于0",
  452. 452 )
  453. 453 }
  454. 454 if img.channels < 1 || img.channels > 4 {
  455. 455● raise @types.LoadError::DecodeFailed(
  456. 456 "decode_code39: 通道数必须在1-4之间",
  457. 457 )
  458. 458 }
  459. 459 let expected_size = img.width * img.height * img.channels
  460. 460● if img.data.length() != expected_size {
  461. 461● raise @types.LoadError::DecodeFailed("decode_code39: 数据大小不匹配")
  462. 462 }
  463. 463 let w = img.width
  464. 464 let h = img.height
  465. 465 let data = img.data
  466. 466 let y = h / 2
  467. 467 let scan_line : Array[Int32] = []
  468. 468 for x in 0..<w {
  469. 469● scan_line.push(if data[y * w + x].to_int() < 128 { 1 } else { 0 })
  470. 470 }
  471. 471 // 寻找起始符 *
  472. 472 let mut start_idx = -1
  473. 473 for i in 0..<(w - 20) {
  474. 474● if match_bar_pattern(scan_line, i, code39_start_pattern) {
  475. 475● start_idx = i
  476. 476 break
  477. 477 }
  478. 478 }
  479. 479 if start_idx < 0 {
  480. 480● return None
  481. 481 }
  482. 482 // 解码数据字符
  483. 483 let mut pos = start_idx + 10 // 起始符 + 间隔
  484. 484 let result_chars : Array[String] = []
  485. 485 while pos < w - 15 {
  486. 486 // 跳过空白间隔
  487. 487● while pos < w && scan_line[pos] == 0 {
  488. 488● pos += 1
  489. 489 }
  490. 490 if pos >= w - 9 {
  491. 491● break
  492. 492 }
  493. 493 // 检查是否是终止符
  494. 494● if match_bar_pattern(scan_line, pos, code39_start_pattern) {
  495. 495○ break
  496. 496 }
  497. 497 // 匹配数据字符
  498. 498 let mut matched = false
  499. 499● for code in 0..<code39_patterns.length() {
  500. 500● if match_bar_pattern(scan_line, pos, code39_patterns[code]) {
  501. 501● result_chars.push(code39_chars[code])
  502. 502 let mut total_width = 0
  503. 503 for width in code39_patterns[code] {
  504. 504● total_width += width
  505. 505 }
  506. 506 pos += total_width + 1 // 字符 + 间隔
  507. 507 matched = true
  508. 508 break
  509. 509 }
  510. 510 }
  511. 511 if !matched {
  512. 512● pos += 1
  513. 513 }
  514. 514 }
  515. 515● if result_chars.length() == 0 {
  516. 516○ return None
  517. 517 }
  518. 518 let mut result = ""
  519. 519 for c in result_chars {
  520. 520● result += c
  521. 521 }
  522. 522 Some(BarcodeResult::{
  523. 523 barcode_type: BarcodeType::Code128, // 复用类型
  524. 524 data: result,
  525. 525 confidence: 0.8,
  526. 526 x: start_idx,
  527. 527 y,
  528. 528 width: pos - start_idx,
  529. 529 height: 1,
  530. 530 })
  531. 531}
  532. 532
  533. 533///|
  534. 534/// 通用一维码识别(自动尝试所有类型)
  535. 535pub fn decode_linear_barcode(
  536. 536 img : @types.Image,
  537. 537) -> Array[BarcodeResult] raise @types.LoadError {
  538. 538 // 输入验证
  539. 539● if img.width <= 0 || img.height <= 0 {
  540. 540● raise @types.LoadError::DecodeFailed(
  541. 541 "decode_linear_barcode: 图像尺寸必须大于0",
  542. 542 )
  543. 543 }
  544. 544 if img.channels < 1 || img.channels > 4 {
  545. 545● raise @types.LoadError::DecodeFailed(
  546. 546 "decode_linear_barcode: 通道数必须在1-4之间",
  547. 547 )
  548. 548 }
  549. 549 let expected_size = img.width * img.height * img.channels
  550. 550● if img.data.length() != expected_size {
  551. 551● raise @types.LoadError::DecodeFailed(
  552. 552 "decode_linear_barcode: 数据大小不匹配",
  553. 553 )
  554. 554 }
  555. 555 let results : Array[BarcodeResult] = []
  556. 556 // 尝试 EAN-13
  557. 557● let ean13 = decode_ean13(img)
  558. 558● match ean13 {
  559. 559○ Some(r) => results.push(r)
  560. 560● None => ignore(())
  561. 561 }
  562. 562 // 尝试 UPC-A
  563. 563● let upca = decode_upca(img)
  564. 564● match upca {
  565. 565○ Some(r) => results.push(r)
  566. 566● None => ignore(())
  567. 567 }
  568. 568 // 尝试 Code128
  569. 569● let code128 = decode_code128(img)
  570. 570● match code128 {
  571. 571○ Some(r) => results.push(r)
  572. 572● None => ignore(())
  573. 573 }
  574. 574 // 尝试 Code39
  575. 575● let code39 = decode_code39(img)
  576. 576● match code39 {
  577. 577● Some(r) => results.push(r)
  578. 578● None => ignore(())
  579. 579 }
  580. 580 results
  581. 581}