id-scanner-lib 1.6.7 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -284,18 +284,20 @@
284
284
  handle(entry) {
285
285
  const timestamp = new Date(entry.timestamp).toISOString();
286
286
  const prefix = `[${timestamp}] [${entry.level.toUpperCase()}] [${entry.tag}]`;
287
+ // 优先使用 error,其次使用 data
288
+ const extra = entry.error || entry.data || '';
287
289
  switch (entry.level) {
288
290
  case exports.LoggerLevel.DEBUG:
289
- console.debug(prefix, entry.message, entry.error || '');
291
+ console.debug(prefix, entry.message, extra);
290
292
  break;
291
293
  case exports.LoggerLevel.INFO:
292
- console.info(prefix, entry.message, entry.error || '');
294
+ console.info(prefix, entry.message, extra);
293
295
  break;
294
296
  case exports.LoggerLevel.WARN:
295
- console.warn(prefix, entry.message, entry.error || '');
297
+ console.warn(prefix, entry.message, extra);
296
298
  break;
297
299
  case exports.LoggerLevel.ERROR:
298
- console.error(prefix, entry.message, entry.error || '');
300
+ console.error(prefix, entry.message, extra);
299
301
  break;
300
302
  // 输出什么也不做
301
303
  }
@@ -369,10 +371,13 @@
369
371
  this.queue = [];
370
372
  /** 定时发送的计时器ID */
371
373
  this.timerId = null;
374
+ /** 当前连续失败计数 */
375
+ this.consecutiveFailures = 0;
372
376
  this.endpoint = endpoint;
373
377
  this.maxQueueSize = maxQueueSize;
374
378
  this.flushInterval = flushInterval;
375
379
  this.isBrowser = typeof window !== 'undefined' && typeof window.addEventListener === 'function';
380
+ this.maxConsecutiveFailures = 10;
376
381
  // 设置定时发送
377
382
  this.startTimer();
378
383
  // 页面卸载前尝试发送剩余日志
@@ -402,47 +407,55 @@
402
407
  flush() {
403
408
  if (this.queue.length === 0)
404
409
  return;
405
- const entriesToSend = [...this.queue];
406
- this.queue = [];
407
- // 防止在 fetch 失败时无限重试
408
- const sendCount = this._sendCount || 0;
409
- this._sendCount = sendCount + 1;
410
- // 如果发送次数过多,停止发送以防止无限循环
411
- if (sendCount > 10) {
412
- console.warn('RemoteLogHandler: Too many failed sends, stopping. Clear queue.');
410
+ // 如果连续失败次数过多,停止发送以防止无限循环
411
+ if (this.consecutiveFailures >= this.maxConsecutiveFailures) {
412
+ console.warn('RemoteLogHandler: Too many consecutive failures, stopping. Clear queue.');
413
413
  this.queue = [];
414
- this._sendCount = 0;
414
+ this.consecutiveFailures = 0;
415
415
  return;
416
416
  }
417
- try {
418
- fetch(this.endpoint, {
419
- method: 'POST',
420
- headers: {
421
- 'Content-Type': 'application/json'
422
- },
423
- body: JSON.stringify(entriesToSend),
424
- keepalive: true
425
- }).catch((err) => {
426
- console.error('Failed to send logs to remote server:', err);
427
- // 防止无限重试 - 如果失败次数过多,丢弃日志
428
- if (this._sendCount > 10) {
429
- console.warn('RemoteLogHandler: Max retry exceeded, discarding logs');
430
- this.queue = []; // 清空队列,避免内存泄漏
431
- this._sendCount = 0;
432
- return;
433
- }
434
- // 失败时把日志放回队列,但防止无限增长
435
- if (this.queue.length < this.maxQueueSize) {
436
- const maxReturn = Math.min(entriesToSend.length, this.maxQueueSize - this.queue.length);
437
- const returnedEntries = entriesToSend.slice(0, maxReturn);
438
- this.queue = [...returnedEntries, ...this.queue];
439
- }
440
- this._sendCount = 0;
441
- });
442
- }
443
- catch (error) {
444
- console.error('Error sending logs:', error);
445
- }
417
+ const entriesToSend = [...this.queue];
418
+ this.queue = [];
419
+ this.sendLogEntries(entriesToSend);
420
+ }
421
+ /**
422
+ * 发送日志条目到远程服务器
423
+ * @param entries 日志条目数组
424
+ */
425
+ sendLogEntries(entries) {
426
+ if (entries.length === 0)
427
+ return;
428
+ const controller = new AbortController();
429
+ const timeoutId = setTimeout(() => controller.abort(), 10000); // 10s 超时
430
+ fetch(this.endpoint, {
431
+ method: 'POST',
432
+ headers: {
433
+ 'Content-Type': 'application/json'
434
+ },
435
+ body: JSON.stringify(entries),
436
+ keepalive: true,
437
+ signal: controller.signal
438
+ }).then(() => {
439
+ clearTimeout(timeoutId);
440
+ this.consecutiveFailures = 0; // 发送成功,重置失败计数
441
+ }).catch((err) => {
442
+ clearTimeout(timeoutId);
443
+ console.error('Failed to send logs to remote server:', err);
444
+ this.consecutiveFailures++;
445
+ // 如果失败次数过多,丢弃日志防止内存泄漏
446
+ if (this.consecutiveFailures >= this.maxConsecutiveFailures) {
447
+ console.warn('RemoteLogHandler: Max consecutive failures exceeded, discarding logs');
448
+ this.queue = [];
449
+ this.consecutiveFailures = 0;
450
+ return;
451
+ }
452
+ // 失败时把日志放回队列,但防止无限增长
453
+ const maxReturn = Math.min(entries.length, this.maxQueueSize - this.queue.length);
454
+ if (maxReturn > 0) {
455
+ const returnedEntries = entries.slice(0, maxReturn);
456
+ this.queue = [...returnedEntries, ...this.queue];
457
+ }
458
+ });
446
459
  }
447
460
  /**
448
461
  * 开始定时发送
@@ -452,7 +465,7 @@
452
465
  return;
453
466
  if (this.timerId !== null)
454
467
  return;
455
- this.timerId = window.setInterval(() => {
468
+ this.timerId = setInterval(() => {
456
469
  this.flush();
457
470
  }, this.flushInterval);
458
471
  }
@@ -461,7 +474,7 @@
461
474
  */
462
475
  stopTimer() {
463
476
  if (this.timerId !== null) {
464
- window.clearInterval(this.timerId);
477
+ clearInterval(this.timerId);
465
478
  this.timerId = null;
466
479
  }
467
480
  }
@@ -538,37 +551,57 @@
538
551
  * 记录调试级别日志
539
552
  * @param tag 标签
540
553
  * @param message 消息
541
- * @param error 错误
554
+ * @param errorOrData 错误对象或结构化数据
542
555
  */
543
- debug(tag, message, error) {
544
- this.log(exports.LoggerLevel.DEBUG, tag, message, error);
556
+ debug(tag, message, errorOrData) {
557
+ if (errorOrData instanceof Error) {
558
+ this.log(exports.LoggerLevel.DEBUG, tag, message, errorOrData);
559
+ }
560
+ else {
561
+ this.logWithData(exports.LoggerLevel.DEBUG, tag, message, errorOrData);
562
+ }
545
563
  }
546
564
  /**
547
565
  * 记录信息级别日志
548
566
  * @param tag 标签
549
567
  * @param message 消息
550
- * @param error 错误
568
+ * @param errorOrData 错误对象或结构化数据
551
569
  */
552
- info(tag, message, error) {
553
- this.log(exports.LoggerLevel.INFO, tag, message, error);
570
+ info(tag, message, errorOrData) {
571
+ if (errorOrData instanceof Error) {
572
+ this.log(exports.LoggerLevel.INFO, tag, message, errorOrData);
573
+ }
574
+ else {
575
+ this.logWithData(exports.LoggerLevel.INFO, tag, message, errorOrData);
576
+ }
554
577
  }
555
578
  /**
556
579
  * 记录警告级别日志
557
580
  * @param tag 标签
558
581
  * @param message 消息
559
- * @param error 错误
582
+ * @param errorOrData 错误对象或结构化数据
560
583
  */
561
- warn(tag, message, error) {
562
- this.log(exports.LoggerLevel.WARN, tag, message, error);
584
+ warn(tag, message, errorOrData) {
585
+ if (errorOrData instanceof Error) {
586
+ this.log(exports.LoggerLevel.WARN, tag, message, errorOrData);
587
+ }
588
+ else {
589
+ this.logWithData(exports.LoggerLevel.WARN, tag, message, errorOrData);
590
+ }
563
591
  }
564
592
  /**
565
593
  * 记录错误级别日志
566
594
  * @param tag 标签
567
595
  * @param message 消息
568
- * @param error 错误
596
+ * @param errorOrData 错误对象或结构化数据
569
597
  */
570
- error(tag, message, error) {
571
- this.log(exports.LoggerLevel.ERROR, tag, message, error);
598
+ error(tag, message, errorOrData) {
599
+ if (errorOrData instanceof Error) {
600
+ this.log(exports.LoggerLevel.ERROR, tag, message, errorOrData);
601
+ }
602
+ else {
603
+ this.logWithData(exports.LoggerLevel.ERROR, tag, message, errorOrData);
604
+ }
572
605
  }
573
606
  /**
574
607
  * 创建标记了特定标签的日志记录器
@@ -613,6 +646,42 @@
613
646
  this.consoleOutput(entry);
614
647
  }
615
648
  }
649
+ /**
650
+ * 记录日志(支持结构化数据)
651
+ * @param level 日志级别
652
+ * @param tag 标签
653
+ * @param message 消息
654
+ * @param data 结构化数据
655
+ */
656
+ logWithData(level, tag, message, data) {
657
+ // 检查日志级别
658
+ const levelValue = this.getLevelValue(level);
659
+ const currentLevelValue = this.getLevelValue(this.logLevel);
660
+ if (levelValue < currentLevelValue) {
661
+ return;
662
+ }
663
+ // 创建日志条目
664
+ const entry = {
665
+ timestamp: Date.now(),
666
+ level: level,
667
+ tag: tag || this.defaultTag,
668
+ message,
669
+ data
670
+ };
671
+ // 分发到所有处理程序
672
+ for (const handler of this.handlers) {
673
+ try {
674
+ handler.handle(entry);
675
+ }
676
+ catch (handlerError) {
677
+ console.error(`[Logger] 处理程序错误:`, handlerError);
678
+ }
679
+ }
680
+ // 如果没有处理程序,使用控制台
681
+ if (this.handlers.length === 0) {
682
+ this.consoleOutput(entry);
683
+ }
684
+ }
616
685
  /**
617
686
  * 控制台输出
618
687
  * @param entry 日志条目
@@ -620,18 +689,20 @@
620
689
  consoleOutput(entry) {
621
690
  const timestamp = new Date(entry.timestamp).toISOString();
622
691
  const prefix = `[${timestamp}] [${entry.level.toUpperCase()}] [${entry.tag}]`;
692
+ // 构造日志内容:错误对象或数据对象
693
+ const extra = entry.error || entry.data || '';
623
694
  switch (entry.level) {
624
695
  case exports.LoggerLevel.DEBUG:
625
- console.debug(`${prefix} ${entry.message}`, entry.error || '');
696
+ console.debug(`${prefix} ${entry.message}`, extra);
626
697
  break;
627
698
  case exports.LoggerLevel.INFO:
628
- console.info(`${prefix} ${entry.message}`, entry.error || '');
699
+ console.info(`${prefix} ${entry.message}`, extra);
629
700
  break;
630
701
  case exports.LoggerLevel.WARN:
631
- console.warn(`${prefix} ${entry.message}`, entry.error || '');
702
+ console.warn(`${prefix} ${entry.message}`, extra);
632
703
  break;
633
704
  case exports.LoggerLevel.ERROR:
634
- console.error(`${prefix} ${entry.message}`, entry.error || '');
705
+ console.error(`${prefix} ${entry.message}`, extra);
635
706
  break;
636
707
  }
637
708
  }
@@ -1598,93 +1669,685 @@
1598
1669
  }
1599
1670
 
1600
1671
  /**
1601
- * @file 图像处理工具类
1602
- * @description 提供图像预处理功能,用于提高OCR识别率
1603
- * @module ImageProcessor
1604
- * @version 1.3.2
1672
+ * 格式化日期字符串为标准格式 (YYYY-MM-DD)
1605
1673
  */
1674
+ function formatDateString(dateStr) {
1675
+ const dateMatch = dateStr.match(/(\d{4})[-\.\u5e74\s]*(\d{1,2})[-\.\u6708\s]*(\d{1,2})[日]*/);
1676
+ if (dateMatch) {
1677
+ const year = dateMatch[1];
1678
+ const month = dateMatch[2].padStart(2, "0");
1679
+ const day = dateMatch[3].padStart(2, "0");
1680
+ return `${year}-${month}-${day}`;
1681
+ }
1682
+ if (/^\d{8}$/.test(dateStr)) {
1683
+ const year = dateStr.substring(0, 4);
1684
+ const month = dateStr.substring(4, 6);
1685
+ const day = dateStr.substring(6, 8);
1686
+ return `${year}-${month}-${day}`;
1687
+ }
1688
+ return dateStr;
1689
+ }
1606
1690
  /**
1607
- * 图像处理工具类
1691
+ * IDCardTextParser - 统一解析身份证OCR文本
1692
+ * 提取 ocr-processor.ts 和 ocr-worker.ts 中的解析逻辑
1693
+ */
1694
+ class IDCardTextParser {
1695
+ /**
1696
+ * 解析身份证文本
1697
+ * @param text OCR识别的原始文本
1698
+ * @returns 解析后的身份证信息
1699
+ */
1700
+ static parse(text) {
1701
+ const info = {};
1702
+ const processedText = text.replace(/\s+/g, " ").trim();
1703
+ const lines = processedText.split("\n").filter((line) => line.trim());
1704
+ // 1. 解析身份证号码
1705
+ const idNumberRegex = /(\d{17}[\dX])/;
1706
+ const idNumberWithPrefixRegex = /公民身份号码[\s\:]*(\d{17}[\dX])/;
1707
+ const basicMatch = processedText.match(idNumberRegex);
1708
+ const prefixMatch = processedText.match(idNumberWithPrefixRegex);
1709
+ if (prefixMatch && prefixMatch[1]) {
1710
+ info.idNumber = prefixMatch[1];
1711
+ }
1712
+ else if (basicMatch && basicMatch[1]) {
1713
+ info.idNumber = basicMatch[1];
1714
+ }
1715
+ // 2. 解析姓名
1716
+ const nameWithLabelRegex = /姓名[\s\:]*([一-龥]{2,4})/;
1717
+ const nameMatch = processedText.match(nameWithLabelRegex);
1718
+ if (nameMatch && nameMatch[1]) {
1719
+ info.name = nameMatch[1].trim();
1720
+ }
1721
+ else {
1722
+ for (const line of lines) {
1723
+ if (line.length >= 2 && line.length <= 5 && /^[一-龥]+$/.test(line) &&
1724
+ !/性别|民族|住址|公民|签发|有效/.test(line)) {
1725
+ info.name = line.trim();
1726
+ break;
1727
+ }
1728
+ }
1729
+ }
1730
+ // 3. 解析性别和民族
1731
+ const genderAndNationalityRegex = /性别[\s\:]*([男女])[\s ]*民族[\s\:]*([一-龥]+族)/;
1732
+ const genderOnlyRegex = /性别[\s\:]*([男女])/;
1733
+ const nationalityOnlyRegex = /民族[\s\:]*([一-龥]+族)/;
1734
+ const genderNationalityMatch = processedText.match(genderAndNationalityRegex);
1735
+ const genderOnlyMatch = processedText.match(genderOnlyRegex);
1736
+ const nationalityOnlyMatch = processedText.match(nationalityOnlyRegex);
1737
+ if (genderNationalityMatch) {
1738
+ info.gender = genderNationalityMatch[1];
1739
+ info.ethnicity = genderNationalityMatch[2];
1740
+ }
1741
+ else {
1742
+ if (genderOnlyMatch)
1743
+ info.gender = genderOnlyMatch[1];
1744
+ if (nationalityOnlyMatch)
1745
+ info.ethnicity = nationalityOnlyMatch[1];
1746
+ }
1747
+ // 4. 判断身份证类型
1748
+ if (processedText.includes('出生') || processedText.includes('公民身份号码')) {
1749
+ info.type = exports.IDCardType.FRONT;
1750
+ }
1751
+ else if (processedText.includes('签发机关') || processedText.includes('有效期')) {
1752
+ info.type = exports.IDCardType.BACK;
1753
+ }
1754
+ // 5. 解析出生日期
1755
+ const birthDateRegex1 = /出生[\s\:]*(\d{4})年(\d{1,2})月(\d{1,2})[日号]/;
1756
+ const birthDateRegex2 = /出生[\s\:]*(\d{4})[-\/\.](\d{1,2})[-\/\.](\d{1,2})/;
1757
+ const birthDateRegex3 = /出生日期[\s\:]*(\d{4})[-\/\.\u5e74](\d{1,2})[-\/\.\u6708](\d{1,2})[日号]?/;
1758
+ const birthDateMatch = processedText.match(birthDateRegex1) || processedText.match(birthDateRegex2) || processedText.match(birthDateRegex3);
1759
+ if (!birthDateMatch && info.idNumber && info.idNumber.length === 18) {
1760
+ const year = info.idNumber.substring(6, 10);
1761
+ const month = info.idNumber.substring(10, 12);
1762
+ const day = info.idNumber.substring(12, 14);
1763
+ info.birthDate = `${year}-${month}-${day}`;
1764
+ }
1765
+ else if (birthDateMatch) {
1766
+ const year = birthDateMatch[1];
1767
+ const month = birthDateMatch[2].padStart(2, "0");
1768
+ const day = birthDateMatch[3].padStart(2, "0");
1769
+ info.birthDate = `${year}-${month}-${day}`;
1770
+ }
1771
+ // 6. 解析地址
1772
+ const addressRegex1 = /住址[\s\:]*([\s\S]*?)(?=公民身份|出生|性别|签发)/;
1773
+ const addressRegex2 = /住址[\s\:]*([一-龥a-zA-Z0-9\s\.\-]+)/;
1774
+ const addressMatch = processedText.match(addressRegex1) || processedText.match(addressRegex2);
1775
+ if (addressMatch && addressMatch[1]) {
1776
+ info.address = addressMatch[1].replace(/\s+/g, "").replace(/\n/g, "").trim();
1777
+ if (info.address.length > 70)
1778
+ info.address = info.address.substring(0, 70);
1779
+ if (!/[一-龥]/.test(info.address))
1780
+ info.address = '';
1781
+ }
1782
+ // 7. 解析签发机关
1783
+ const authorityRegex1 = /签发机关[\s\:]*([\s\S]*?)(?=有效|公民|出生|\d{8}|$)/;
1784
+ const authorityRegex2 = /签发机关[\s\:]*([一-龥\s]+)/;
1785
+ const authorityMatch = processedText.match(authorityRegex1) || processedText.match(authorityRegex2);
1786
+ if (authorityMatch && authorityMatch[1]) {
1787
+ info.issueAuthority = authorityMatch[1].replace(/\s+/g, "").replace(/\n/g, "").trim();
1788
+ }
1789
+ // 8. 解析有效期限
1790
+ const validPeriodRegex1 = /有效期限[\s\:]*(\d{4}[-\.\u5e74\s]\d{1,2}[-\.\u6708\s]\d{1,2}[日\s]*)[-\s]*(至|-)[-\s]*(\d{4}[-\.\u5e74\s]\d{1,2}[-\.\u6708\s]\d{1,2}[日]*|[永久长期]*)/;
1791
+ const validPeriodRegex2 = /有效期限[\s\:]*(\d{8})[-\s]*(至|-)[-\s]*(\d{8}|[永久长期]*)/;
1792
+ const validPeriodMatch = processedText.match(validPeriodRegex1) || processedText.match(validPeriodRegex2);
1793
+ if (validPeriodMatch && validPeriodMatch[1] && validPeriodMatch[3]) {
1794
+ const startDate = formatDateString(validPeriodMatch[1]);
1795
+ const endDate = /\d/.test(validPeriodMatch[3]) ? formatDateString(validPeriodMatch[3]) : '长期有效';
1796
+ info.validFrom = startDate;
1797
+ info.validTo = endDate;
1798
+ info.validPeriod = `${startDate}-${endDate}`;
1799
+ }
1800
+ else if (validPeriodMatch) {
1801
+ info.validPeriod = validPeriodMatch[0].replace("有效期限", "").trim();
1802
+ }
1803
+ return info;
1804
+ }
1805
+ }
1806
+
1807
+ /**
1808
+ * @file Canvas 对象池
1809
+ * @description 提供 Canvas 元素的复用机制,减少内存分配和 GC 压力
1810
+ * @module utils/canvas-pool
1811
+ */
1812
+ /**
1813
+ * Canvas 对象池
1608
1814
  *
1609
- * 提供各种图像处理功能,用于优化识别效果
1815
+ * 复用 Canvas 元素,避免频繁创建和销毁导致的内存抖动
1816
+ *
1817
+ * @example
1818
+ * ```typescript
1819
+ * const pool = CanvasPool.getInstance();
1820
+ * const { canvas, context } = pool.acquire(100, 200);
1821
+ * // 使用 canvas 进行绘制...
1822
+ * pool.release(canvas);
1823
+ * ```
1610
1824
  */
1611
- class ImageProcessor {
1825
+ class CanvasPool {
1612
1826
  /**
1613
- * 将ImageData转换为Canvas元素
1614
- *
1615
- * @param {ImageData} imageData - 要转换的图像数据
1616
- * @returns {HTMLCanvasElement} 包含图像的Canvas元素
1827
+ * 获取单例实例
1617
1828
  */
1618
- static imageDataToCanvas(imageData) {
1619
- const canvas = document.createElement("canvas");
1620
- canvas.width = imageData.width;
1621
- canvas.height = imageData.height;
1622
- const ctx = canvas.getContext("2d");
1623
- if (ctx) {
1624
- ctx.putImageData(imageData, 0, 0);
1829
+ static getInstance() {
1830
+ if (!CanvasPool.instance) {
1831
+ CanvasPool.instance = new CanvasPool();
1625
1832
  }
1626
- return canvas;
1833
+ return CanvasPool.instance;
1627
1834
  }
1628
1835
  /**
1629
- * 将Canvas转换为ImageData
1630
- *
1631
- * @param {HTMLCanvasElement} canvas - 要转换的Canvas元素
1632
- * @returns {ImageData|null} Canvas的图像数据,如果获取失败则返回null
1836
+ * 重置单例实例(主要用于测试)
1633
1837
  */
1634
- static canvasToImageData(canvas) {
1635
- const ctx = canvas.getContext("2d");
1636
- return ctx ? ctx.getImageData(0, 0, canvas.width, canvas.height) : null;
1838
+ static resetInstance() {
1839
+ if (CanvasPool.instance) {
1840
+ CanvasPool.instance.dispose();
1841
+ CanvasPool.instance = null;
1842
+ }
1637
1843
  }
1638
1844
  /**
1639
- * 调整图像亮度和对比度
1640
- *
1641
- * @param imageData 原始图像数据
1642
- * @param brightness 亮度调整值 (-100到100)
1643
- * @param contrast 对比度调整值 (-100到100)
1644
- * @returns 处理后的图像数据
1845
+ * 私有构造函数
1645
1846
  */
1646
- static adjustBrightnessContrast(imageData, brightness = 0, contrast = 0) {
1647
- // 将亮度和对比度范围限制在 -100 到 100 之间
1648
- brightness = Math.max(-100, Math.min(100, brightness));
1649
- contrast = Math.max(-100, Math.min(100, contrast));
1650
- // 将范围转换为适合计算的值
1651
- const factor = (259 * (contrast + 255)) / (255 * (259 - contrast));
1652
- const briAdjust = (brightness / 100) * 255;
1653
- const data = imageData.data;
1654
- const length = data.length;
1655
- for (let i = 0; i < length; i += 4) {
1656
- // 分别处理 RGB 三个通道
1657
- for (let j = 0; j < 3; j++) {
1658
- // 应用亮度和对比度调整公式
1659
- const newValue = factor * (data[i + j] + briAdjust - 128) + 128;
1660
- data[i + j] = Math.max(0, Math.min(255, newValue));
1847
+ constructor() {
1848
+ /** Canvas 池存储 */
1849
+ this.pool = new Map();
1850
+ /** 已借出的 Canvas */
1851
+ this.borrowed = new Map();
1852
+ /** 最大池大小(每个尺寸) */
1853
+ this.maxPoolSize = 4;
1854
+ /** Canvas 尺寸容差(允许一定范围的尺寸复用) */
1855
+ this.sizeTolerance = 10;
1856
+ // 页面卸载前清理
1857
+ if (typeof window !== 'undefined') {
1858
+ window.addEventListener('beforeunload', () => this.dispose());
1859
+ }
1860
+ }
1861
+ /**
1862
+ * 生成尺寸键
1863
+ * @param width 宽度
1864
+ * @param height 高度
1865
+ */
1866
+ getSizeKey(width, height) {
1867
+ return `${width}x${height}`;
1868
+ }
1869
+ /**
1870
+ * 查找匹配的尺寸键(考虑容差)
1871
+ * @param width 宽度
1872
+ * @param height 高度
1873
+ */
1874
+ findMatchingSizeKey(width, height) {
1875
+ for (const [key, items] of this.pool.entries()) {
1876
+ const [w, h] = key.split('x').map(Number);
1877
+ if (Math.abs(w - width) <= this.sizeTolerance &&
1878
+ Math.abs(h - height) <= this.sizeTolerance) {
1879
+ // 找到可用的
1880
+ const available = items.filter(item => !item.inUse);
1881
+ if (available.length > 0) {
1882
+ return key;
1883
+ }
1661
1884
  }
1662
- // Alpha 通道保持不变
1663
1885
  }
1664
- return imageData;
1886
+ return null;
1665
1887
  }
1666
1888
  /**
1667
- * 将图像转换为灰度图
1889
+ * 从池中获取 Canvas
1668
1890
  *
1669
- * @param imageData 原始图像数据
1670
- * @returns 灰度图像数据
1891
+ * @param width 宽度
1892
+ * @param height 高度
1893
+ * @returns Canvas 和其上下文
1894
+ */
1895
+ acquire(width, height) {
1896
+ // 先尝试精确匹配
1897
+ let sizeKey = this.getSizeKey(width, height);
1898
+ let items = this.pool.get(sizeKey);
1899
+ // 如果没有精确匹配,尝试模糊匹配
1900
+ if (!items || items.every(item => item.inUse)) {
1901
+ const matchedKey = this.findMatchingSizeKey(width, height);
1902
+ if (matchedKey) {
1903
+ sizeKey = matchedKey;
1904
+ items = this.pool.get(sizeKey);
1905
+ }
1906
+ }
1907
+ // 如果没有可用的,创建一个新的
1908
+ if (!items || items.every(item => item.inUse)) {
1909
+ const canvas = document.createElement('canvas');
1910
+ canvas.width = width;
1911
+ canvas.height = height;
1912
+ const context = canvas.getContext('2d');
1913
+ const item = {
1914
+ canvas,
1915
+ context,
1916
+ inUse: true,
1917
+ lastUsed: Date.now(),
1918
+ sizeKey: this.getSizeKey(width, height)
1919
+ };
1920
+ // 如果池已满,移除最老的
1921
+ if (!items) {
1922
+ items = [];
1923
+ this.pool.set(sizeKey, items);
1924
+ }
1925
+ else if (items.length >= this.maxPoolSize) {
1926
+ // 找到最老的未使用项并移除
1927
+ let oldestIdx = 0;
1928
+ let oldestTime = Infinity;
1929
+ items.forEach((item, idx) => {
1930
+ if (!item.inUse && item.lastUsed < oldestTime) {
1931
+ oldestTime = item.lastUsed;
1932
+ oldestIdx = idx;
1933
+ }
1934
+ });
1935
+ const removed = items.splice(oldestIdx, 1)[0];
1936
+ this.borrowed.delete(removed.canvas);
1937
+ }
1938
+ items.push(item);
1939
+ this.borrowed.set(canvas, item);
1940
+ return { canvas, context };
1941
+ }
1942
+ // 找到一个空闲的
1943
+ const available = items.find(item => !item.inUse);
1944
+ available.inUse = true;
1945
+ available.lastUsed = Date.now();
1946
+ // 如果尺寸变化,更新 canvas
1947
+ if (available.canvas.width !== width || available.canvas.height !== height) {
1948
+ available.canvas.width = width;
1949
+ available.canvas.height = height;
1950
+ available.sizeKey = sizeKey;
1951
+ }
1952
+ // 清除之前的上下文状态
1953
+ available.context.setTransform(1, 0, 0, 1, 0, 0);
1954
+ available.context.clearRect(0, 0, width, height);
1955
+ this.borrowed.set(available.canvas, available);
1956
+ return { canvas: available.canvas, context: available.context };
1957
+ }
1958
+ /**
1959
+ * 释放 Canvas 回池中
1960
+ *
1961
+ * @param canvas 要释放的 Canvas
1671
1962
  */
1672
- static toGrayscale(imageData) {
1673
- const data = imageData.data;
1674
- const length = data.length;
1675
- for (let i = 0; i < length; i += 4) {
1676
- // 使用加权平均法将 RGB 转换为灰度值
1677
- const gray = data[i] * 0.3 + data[i + 1] * 0.59 + data[i + 2] * 0.11;
1678
- data[i] = data[i + 1] = data[i + 2] = gray;
1963
+ release(canvas) {
1964
+ const item = this.borrowed.get(canvas);
1965
+ if (!item) {
1966
+ // 不属于我们管理的 Canvas,忽略
1967
+ return;
1679
1968
  }
1680
- return imageData;
1969
+ item.inUse = false;
1970
+ item.lastUsed = Date.now();
1971
+ this.borrowed.delete(canvas);
1681
1972
  }
1682
1973
  /**
1683
- * 锐化图像
1684
- *
1685
- * @param imageData 原始图像数据
1686
- * @param amount 锐化程度,默认为2
1687
- * @returns 锐化后的图像数据
1974
+ * 批量释放所有借出的 Canvas
1975
+ */
1976
+ releaseAll() {
1977
+ for (const [, item] of this.borrowed) {
1978
+ item.inUse = false;
1979
+ item.lastUsed = Date.now();
1980
+ }
1981
+ this.borrowed.clear();
1982
+ }
1983
+ /**
1984
+ * 预热池(预创建指定尺寸的 Canvas)
1985
+ *
1986
+ * @param sizes 尺寸数组,每项为 [width, height]
1987
+ */
1988
+ warmup(sizes) {
1989
+ for (const [width, height] of sizes) {
1990
+ this.acquire(width, height);
1991
+ // 立即释放,让它们进入池中
1992
+ const sizeKey = this.getSizeKey(width, height);
1993
+ const items = this.pool.get(sizeKey);
1994
+ if (items && items.length > 0) {
1995
+ const item = items[items.length - 1];
1996
+ item.inUse = false;
1997
+ this.borrowed.delete(item.canvas);
1998
+ }
1999
+ }
2000
+ }
2001
+ /**
2002
+ * 获取池统计信息
2003
+ */
2004
+ getStats() {
2005
+ let totalItems = 0;
2006
+ let borrowedCount = 0;
2007
+ const poolSizes = {};
2008
+ for (const [key, items] of this.pool.entries()) {
2009
+ totalItems += items.length;
2010
+ borrowedCount += items.filter(i => i.inUse).length;
2011
+ poolSizes[key] = {
2012
+ total: items.length,
2013
+ available: items.filter(i => !i.inUse).length
2014
+ };
2015
+ }
2016
+ return { totalItems, borrowedCount, poolSizes };
2017
+ }
2018
+ /**
2019
+ * 清理并释放所有资源
2020
+ */
2021
+ dispose() {
2022
+ this.pool.clear();
2023
+ this.borrowed.clear();
2024
+ }
2025
+ }
2026
+ /** 单例实例 */
2027
+ CanvasPool.instance = null;
2028
+
2029
+ /**
2030
+ * @file 边缘检测器
2031
+ * @description 提供边缘检测算法(Sobel、Canny等)
2032
+ * @module utils/edge-detector
2033
+ */
2034
+ /**
2035
+ * 边缘检测器类
2036
+ * 提供各种边缘检测算法用于图像处理
2037
+ */
2038
+ class EdgeDetector {
2039
+ /**
2040
+ * 使用Sobel算子进行边缘检测
2041
+ * @param imageData 灰度图像数据
2042
+ * @param threshold 边缘阈值,默认为30
2043
+ * @returns 检测到边缘的图像数据
2044
+ */
2045
+ static detectEdges(imageData, threshold = 30) {
2046
+ const grayscaleImage = this.toGrayscale(new ImageData(new Uint8ClampedArray(imageData.data), imageData.width, imageData.height));
2047
+ const width = grayscaleImage.width;
2048
+ const height = grayscaleImage.height;
2049
+ const inputData = grayscaleImage.data;
2050
+ const outputData = new Uint8ClampedArray(inputData.length);
2051
+ const sobelX = [-1, 0, 1, -2, 0, 2, -1, 0, 1];
2052
+ const sobelY = [-1, -2, -1, 0, 0, 0, 1, 2, 1];
2053
+ for (let y = 1; y < height - 1; y++) {
2054
+ for (let x = 1; x < width - 1; x++) {
2055
+ let gx = 0, gy = 0;
2056
+ for (let ky = -1; ky <= 1; ky++) {
2057
+ for (let kx = -1; kx <= 1; kx++) {
2058
+ const pixelPos = ((y + ky) * width + (x + kx)) * 4;
2059
+ const pixelVal = inputData[pixelPos];
2060
+ const kernelIdx = (ky + 1) * 3 + (kx + 1);
2061
+ gx += pixelVal * sobelX[kernelIdx];
2062
+ gy += pixelVal * sobelY[kernelIdx];
2063
+ }
2064
+ }
2065
+ let magnitude = Math.sqrt(gx * gx + gy * gy);
2066
+ magnitude = magnitude > threshold ? 255 : 0;
2067
+ const pos = (y * width + x) * 4;
2068
+ outputData[pos] = outputData[pos + 1] = outputData[pos + 2] = magnitude;
2069
+ outputData[pos + 3] = 255;
2070
+ }
2071
+ }
2072
+ // 处理边缘
2073
+ for (let i = 0; i < width * 4; i++) {
2074
+ outputData[i] = 0;
2075
+ outputData[(height - 1) * width * 4 + i] = 0;
2076
+ }
2077
+ for (let i = 0; i < height; i++) {
2078
+ const leftPos = i * width * 4;
2079
+ const rightPos = (i * width + width - 1) * 4;
2080
+ for (let j = 0; j < 4; j++) {
2081
+ outputData[leftPos + j] = 0;
2082
+ outputData[rightPos + j] = 0;
2083
+ }
2084
+ }
2085
+ return new ImageData(outputData, width, height);
2086
+ }
2087
+ /**
2088
+ * 卡尼-德里奇边缘检测
2089
+ */
2090
+ static cannyEdgeDetection(imageData, lowThreshold = 20, highThreshold = 50) {
2091
+ const grayscaleImage = this.toGrayscale(new ImageData(new Uint8ClampedArray(imageData.data), imageData.width, imageData.height));
2092
+ const blurredImage = this.gaussianBlur(grayscaleImage, 1.5);
2093
+ const { gradientMagnitude, gradientDirection } = this.computeGradients(blurredImage);
2094
+ const nonMaxSuppressed = this.nonMaxSuppression(gradientMagnitude, gradientDirection, blurredImage.width, blurredImage.height);
2095
+ const thresholdResult = this.hysteresisThresholding(nonMaxSuppressed, blurredImage.width, blurredImage.height, lowThreshold, highThreshold);
2096
+ const outputData = new Uint8ClampedArray(imageData.data.length);
2097
+ for (let i = 0; i < thresholdResult.length; i++) {
2098
+ const pos = i * 4;
2099
+ const value = thresholdResult[i] ? 255 : 0;
2100
+ outputData[pos] = outputData[pos + 1] = outputData[pos + 2] = value;
2101
+ outputData[pos + 3] = 255;
2102
+ }
2103
+ return new ImageData(outputData, blurredImage.width, blurredImage.height);
2104
+ }
2105
+ static toGrayscale(imageData) {
2106
+ const srcData = imageData.data;
2107
+ const destData = new Uint8ClampedArray(srcData);
2108
+ for (let i = 0; i < srcData.length; i += 4) {
2109
+ const gray = srcData[i] * 0.3 + srcData[i + 1] * 0.59 + srcData[i + 2] * 0.11;
2110
+ destData[i] = destData[i + 1] = destData[i + 2] = gray;
2111
+ destData[i + 3] = srcData[i + 3];
2112
+ }
2113
+ return new ImageData(destData, imageData.width, imageData.height);
2114
+ }
2115
+ static gaussianBlur(imageData, sigma = 1.5) {
2116
+ const width = imageData.width, height = imageData.height;
2117
+ const inputData = imageData.data, outputData = new Uint8ClampedArray(inputData.length);
2118
+ const kernelSize = Math.max(3, Math.floor(sigma * 3) * 2 + 1);
2119
+ const halfKernel = Math.floor(kernelSize / 2);
2120
+ const kernel = this.generateGaussianKernel(kernelSize, sigma);
2121
+ for (let y = 0; y < height; y++) {
2122
+ for (let x = 0; x < width; x++) {
2123
+ let sum = 0, weightSum = 0;
2124
+ for (let ky = -halfKernel; ky <= halfKernel; ky++) {
2125
+ for (let kx = -halfKernel; kx <= halfKernel; kx++) {
2126
+ const pixelY = Math.min(Math.max(y + ky, 0), height - 1);
2127
+ const pixelX = Math.min(Math.max(x + kx, 0), width - 1);
2128
+ const pixelPos = (pixelY * width + pixelX) * 4;
2129
+ const kernelY = ky + halfKernel, kernelX = kx + halfKernel;
2130
+ const weight = kernel[kernelY * kernelSize + kernelX];
2131
+ sum += inputData[pixelPos] * weight;
2132
+ weightSum += weight;
2133
+ }
2134
+ }
2135
+ const pos = (y * width + x) * 4;
2136
+ const value = Math.round(sum / weightSum);
2137
+ outputData[pos] = outputData[pos + 1] = outputData[pos + 2] = value;
2138
+ outputData[pos + 3] = 255;
2139
+ }
2140
+ }
2141
+ return new ImageData(outputData, width, height);
2142
+ }
2143
+ static generateGaussianKernel(size, sigma) {
2144
+ const kernel = new Array(size * size);
2145
+ const center = Math.floor(size / 2);
2146
+ let sum = 0;
2147
+ for (let y = 0; y < size; y++) {
2148
+ for (let x = 0; x < size; x++) {
2149
+ const distance = Math.sqrt((x - center) ** 2 + (y - center) ** 2);
2150
+ kernel[y * size + x] = Math.exp(-(distance ** 2) / (2 * sigma ** 2));
2151
+ sum += kernel[y * size + x];
2152
+ }
2153
+ }
2154
+ for (let i = 0; i < kernel.length; i++)
2155
+ kernel[i] /= sum;
2156
+ return kernel;
2157
+ }
2158
+ static computeGradients(imageData) {
2159
+ const width = imageData.width, height = imageData.height;
2160
+ const inputData = imageData.data;
2161
+ const gradientMagnitude = new Array(width * height);
2162
+ const gradientDirection = new Array(width * height);
2163
+ const sobelX = [-1, 0, 1, -2, 0, 2, -1, 0, 1];
2164
+ const sobelY = [-1, -2, -1, 0, 0, 0, 1, 2, 1];
2165
+ for (let y = 1; y < height - 1; y++) {
2166
+ for (let x = 1; x < width - 1; x++) {
2167
+ let gx = 0, gy = 0;
2168
+ for (let ky = -1; ky <= 1; ky++) {
2169
+ for (let kx = -1; kx <= 1; kx++) {
2170
+ const pixelPos = ((y + ky) * width + (x + kx)) * 4;
2171
+ const pixelVal = inputData[pixelPos];
2172
+ const kernelIdx = (ky + 1) * 3 + (kx + 1);
2173
+ gx += pixelVal * sobelX[kernelIdx];
2174
+ gy += pixelVal * sobelY[kernelIdx];
2175
+ }
2176
+ }
2177
+ const idx = y * width + x;
2178
+ gradientMagnitude[idx] = Math.sqrt(gx * gx + gy * gy);
2179
+ gradientDirection[idx] = Math.atan2(gy, gx);
2180
+ }
2181
+ }
2182
+ return { gradientMagnitude, gradientDirection };
2183
+ }
2184
+ static nonMaxSuppression(gradientMagnitude, gradientDirection, width, height) {
2185
+ const result = new Array(width * height).fill(0);
2186
+ for (let y = 1; y < height - 1; y++) {
2187
+ for (let x = 1; x < width - 1; x++) {
2188
+ const idx = y * width + x;
2189
+ const magnitude = gradientMagnitude[idx];
2190
+ const direction = gradientDirection[idx];
2191
+ const degrees = (direction * 180 / Math.PI + 180) % 180;
2192
+ let neighbor1Idx, neighbor2Idx;
2193
+ if ((degrees >= 0 && degrees < 22.5) || (degrees >= 157.5 && degrees <= 180)) {
2194
+ neighbor1Idx = idx - 1;
2195
+ neighbor2Idx = idx + 1;
2196
+ }
2197
+ else if (degrees >= 22.5 && degrees < 67.5) {
2198
+ neighbor1Idx = (y - 1) * width + (x + 1);
2199
+ neighbor2Idx = (y + 1) * width + (x - 1);
2200
+ }
2201
+ else if (degrees >= 67.5 && degrees < 112.5) {
2202
+ neighbor1Idx = (y - 1) * width + x;
2203
+ neighbor2Idx = (y + 1) * width + x;
2204
+ }
2205
+ else {
2206
+ neighbor1Idx = (y - 1) * width + (x - 1);
2207
+ neighbor2Idx = (y + 1) * width + (x + 1);
2208
+ }
2209
+ if (magnitude >= gradientMagnitude[neighbor1Idx] && magnitude >= gradientMagnitude[neighbor2Idx]) {
2210
+ result[idx] = magnitude;
2211
+ }
2212
+ }
2213
+ }
2214
+ return result;
2215
+ }
2216
+ static hysteresisThresholding(nonMaxSuppressed, width, height, lowThreshold, highThreshold) {
2217
+ const result = new Array(width * height).fill(false);
2218
+ const visited = new Array(width * height).fill(false);
2219
+ const stack = [];
2220
+ for (let i = 0; i < nonMaxSuppressed.length; i++) {
2221
+ if (nonMaxSuppressed[i] >= highThreshold) {
2222
+ result[i] = true;
2223
+ stack.push(i);
2224
+ visited[i] = true;
2225
+ }
2226
+ }
2227
+ const dx = [-1, 0, 1, -1, 1, -1, 0, 1];
2228
+ const dy = [-1, -1, -1, 0, 0, 1, 1, 1];
2229
+ while (stack.length > 0) {
2230
+ const currentIdx = stack.pop();
2231
+ const currentX = currentIdx % width;
2232
+ const currentY = Math.floor(currentIdx / width);
2233
+ for (let i = 0; i < 8; i++) {
2234
+ const newX = currentX + dx[i];
2235
+ const newY = currentY + dy[i];
2236
+ if (newX >= 0 && newX < width && newY >= 0 && newY < height) {
2237
+ const newIdx = newY * width + newX;
2238
+ if (!visited[newIdx] && nonMaxSuppressed[newIdx] >= lowThreshold) {
2239
+ result[newIdx] = true;
2240
+ stack.push(newIdx);
2241
+ visited[newIdx] = true;
2242
+ }
2243
+ }
2244
+ }
2245
+ }
2246
+ return result;
2247
+ }
2248
+ }
2249
+
2250
+ /**
2251
+ * @file 图像处理工具类
2252
+ * @description 提供图像预处理功能,用于提高OCR识别率
2253
+ * @module ImageProcessor
2254
+ * @version 1.4.0
2255
+ */
2256
+ /**
2257
+ * 图像处理工具类
2258
+ *
2259
+ * 提供各种图像处理功能,用于优化识别效果
2260
+ */
2261
+ class ImageProcessor {
2262
+ /**
2263
+ * 将ImageData转换为Canvas元素
2264
+ *
2265
+ * @param {ImageData} imageData - 要转换的图像数据
2266
+ * @returns {HTMLCanvasElement} 包含图像的Canvas元素
2267
+ */
2268
+ static imageDataToCanvas(imageData, usePool = true) {
2269
+ let canvas;
2270
+ let context;
2271
+ if (usePool) {
2272
+ ({ canvas, context } = CanvasPool.getInstance().acquire(imageData.width, imageData.height));
2273
+ }
2274
+ else {
2275
+ canvas = document.createElement("canvas");
2276
+ canvas.width = imageData.width;
2277
+ canvas.height = imageData.height;
2278
+ context = canvas.getContext("2d");
2279
+ }
2280
+ context.putImageData(imageData, 0, 0);
2281
+ if (usePool) {
2282
+ // 立即释放回池中,用户保留 canvas 引用即可
2283
+ CanvasPool.getInstance().release(canvas);
2284
+ }
2285
+ return canvas;
2286
+ }
2287
+ /**
2288
+ * 将Canvas转换为ImageData
2289
+ *
2290
+ * @param {HTMLCanvasElement} canvas - 要转换的Canvas元素
2291
+ * @returns {ImageData|null} Canvas的图像数据,如果获取失败则返回null
2292
+ */
2293
+ static canvasToImageData(canvas) {
2294
+ const ctx = canvas.getContext("2d");
2295
+ return ctx ? ctx.getImageData(0, 0, canvas.width, canvas.height) : null;
2296
+ }
2297
+ /**
2298
+ * 调整图像亮度和对比度
2299
+ *
2300
+ * @param imageData 原始图像数据
2301
+ * @param brightness 亮度调整值 (-100到100)
2302
+ * @param contrast 对比度调整值 (-100到100)
2303
+ * @returns 处理后的图像数据
2304
+ */
2305
+ static adjustBrightnessContrast(imageData, brightness = 0, contrast = 0) {
2306
+ // 将亮度和对比度范围限制在 -100 到 100 之间
2307
+ brightness = Math.max(-100, Math.min(100, brightness));
2308
+ contrast = Math.max(-100, Math.min(100, contrast));
2309
+ // 将范围转换为适合计算的值
2310
+ const factor = (259 * (contrast + 255)) / (255 * (259 - contrast));
2311
+ const briAdjust = (brightness / 100) * 255;
2312
+ const data = imageData.data;
2313
+ const length = data.length;
2314
+ for (let i = 0; i < length; i += 4) {
2315
+ // 分别处理 RGB 三个通道
2316
+ for (let j = 0; j < 3; j++) {
2317
+ // 应用亮度和对比度调整公式
2318
+ const newValue = factor * (data[i + j] + briAdjust - 128) + 128;
2319
+ data[i + j] = Math.max(0, Math.min(255, newValue));
2320
+ }
2321
+ // Alpha 通道保持不变
2322
+ }
2323
+ return imageData;
2324
+ }
2325
+ /**
2326
+ * 将图像转换为灰度图(返回新 ImageData,不修改原图)
2327
+ *
2328
+ * @param imageData 原始图像数据
2329
+ * @returns 灰度图像数据(新对象)
2330
+ */
2331
+ static toGrayscale(imageData) {
2332
+ const srcData = imageData.data;
2333
+ const length = srcData.length;
2334
+ // 创建新数组,避免修改原图
2335
+ const destData = new Uint8ClampedArray(srcData);
2336
+ for (let i = 0; i < length; i += 4) {
2337
+ // 使用加权平均法将 RGB 转换为灰度值
2338
+ const gray = srcData[i] * 0.3 + srcData[i + 1] * 0.59 + srcData[i + 2] * 0.11;
2339
+ destData[i] = destData[i + 1] = destData[i + 2] = gray;
2340
+ // Alpha 通道保持不变
2341
+ destData[i + 3] = srcData[i + 3];
2342
+ }
2343
+ return new ImageData(destData, imageData.width, imageData.height);
2344
+ }
2345
+ /**
2346
+ * 锐化图像
2347
+ *
2348
+ * @param imageData 原始图像数据
2349
+ * @param amount 锐化程度,默认为2
2350
+ * @returns 锐化后的图像数据
1688
2351
  */
1689
2352
  static sharpen(imageData, amount = 2) {
1690
2353
  if (!imageData || !imageData.data)
@@ -1755,35 +2418,48 @@
1755
2418
  return new ImageData(outputData, width, height);
1756
2419
  }
1757
2420
  /**
1758
- * 对图像应用阈值操作,增强对比度
2421
+ * 对图像应用阈值操作,增强对比度(二值化)
1759
2422
  *
1760
2423
  * @param imageData 原始图像数据
1761
2424
  * @param threshold 阈值 (0-255)
1762
- * @returns 处理后的图像数据
2425
+ * @returns 处理后的图像数据(新对象,不修改原图)
1763
2426
  */
1764
2427
  static threshold(imageData, threshold = 128) {
1765
- // 先转换为灰度图(toGrayscale 内部已创建新 ImageData,无需外部拷贝)
2428
+ // 先转换为灰度图(返回新 ImageData,不修改原图)
1766
2429
  const grayscaleImage = this.toGrayscale(imageData);
1767
- const data = grayscaleImage.data;
1768
- for (let i = 0; i < data.length; i += 4) {
2430
+ const srcData = grayscaleImage.data;
2431
+ const length = srcData.length;
2432
+ // 创建新数组存储二值化结果
2433
+ const destData = new Uint8ClampedArray(length);
2434
+ for (let i = 0; i < length; i += 4) {
1769
2435
  // 二值化处理
1770
- const value = data[i] < threshold ? 0 : 255;
1771
- data[i] = data[i + 1] = data[i + 2] = value;
2436
+ const value = srcData[i] < threshold ? 0 : 255;
2437
+ destData[i] = destData[i + 1] = destData[i + 2] = value;
2438
+ destData[i + 3] = srcData[i + 3]; // 保持透明度
1772
2439
  }
1773
- return grayscaleImage;
2440
+ return new ImageData(destData, grayscaleImage.width, grayscaleImage.height);
1774
2441
  }
1775
2442
  /**
1776
- * 将图像转换为黑白图像(二值化)
2443
+ * 将图像转换为黑白图像(二值化,使用OTSU自动阈值)
1777
2444
  *
1778
2445
  * @param imageData 原始图像数据
1779
- * @returns 二值化后的图像数据
2446
+ * @returns 二值化后的图像数据(新对象,不修改原图)
1780
2447
  */
1781
2448
  static toBinaryImage(imageData) {
1782
- // 先转换为灰度图(toGrayscale 内部已创建新 ImageData,无需外部拷贝)
2449
+ // 先转换为灰度图(返回新 ImageData,不修改原图)
1783
2450
  const grayscaleImage = this.toGrayscale(imageData);
1784
2451
  // 使用OTSU算法自动确定阈值
1785
2452
  const threshold = this.getOtsuThreshold(grayscaleImage);
1786
- return this.threshold(grayscaleImage, threshold);
2453
+ // 直接对灰度图进行二值化,避免再次调用 toGrayscale
2454
+ const srcData = grayscaleImage.data;
2455
+ const length = srcData.length;
2456
+ const destData = new Uint8ClampedArray(length);
2457
+ for (let i = 0; i < length; i += 4) {
2458
+ const value = srcData[i] < threshold ? 0 : 255;
2459
+ destData[i] = destData[i + 1] = destData[i + 2] = value;
2460
+ destData[i + 3] = srcData[i + 3]; // 保持透明度
2461
+ }
2462
+ return new ImageData(destData, grayscaleImage.width, grayscaleImage.height);
1787
2463
  }
1788
2464
  /**
1789
2465
  * 使用OTSU算法计算最佳阈值
@@ -1901,24 +2577,20 @@
1901
2577
  const url = URL.createObjectURL(file);
1902
2578
  img.onload = () => {
1903
2579
  try {
1904
- // 创建canvas元素
1905
- const canvas = document.createElement("canvas");
1906
- const ctx = canvas.getContext("2d");
1907
- if (!ctx) {
1908
- reject(new Error("无法创建2D上下文"));
1909
- return;
1910
- }
1911
- canvas.width = img.width;
1912
- canvas.height = img.height;
2580
+ // 使用 Canvas 池获取 canvas
2581
+ const { canvas, context } = CanvasPool.getInstance().acquire(img.width, img.height);
1913
2582
  // 绘制图片到canvas
1914
- ctx.drawImage(img, 0, 0);
2583
+ context.drawImage(img, 0, 0);
1915
2584
  // 获取图像数据
1916
- const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
2585
+ const imageData = context.getImageData(0, 0, canvas.width, canvas.height);
2586
+ // 释放回池
2587
+ CanvasPool.getInstance().release(canvas);
1917
2588
  // 释放资源
1918
2589
  URL.revokeObjectURL(url);
1919
2590
  resolve(imageData);
1920
2591
  }
1921
2592
  catch (e) {
2593
+ URL.revokeObjectURL(url);
1922
2594
  reject(e);
1923
2595
  }
1924
2596
  };
@@ -1945,16 +2617,12 @@
1945
2617
  static async imageDataToFile(imageData, fileName = "image.jpg", fileType = "image/jpeg", quality = 0.8) {
1946
2618
  return new Promise((resolve, reject) => {
1947
2619
  try {
1948
- const canvas = document.createElement("canvas");
1949
- canvas.width = imageData.width;
1950
- canvas.height = imageData.height;
1951
- const ctx = canvas.getContext("2d");
1952
- if (!ctx) {
1953
- reject(new Error("无法创建2D上下文"));
1954
- return;
1955
- }
1956
- ctx.putImageData(imageData, 0, 0);
2620
+ // 使用 Canvas
2621
+ const { canvas, context } = CanvasPool.getInstance().acquire(imageData.width, imageData.height);
2622
+ context.putImageData(imageData, 0, 0);
1957
2623
  canvas.toBlob((blob) => {
2624
+ // 释放回池
2625
+ CanvasPool.getInstance().release(canvas);
1958
2626
  if (!blob) {
1959
2627
  reject(new Error("无法创建图片Blob"));
1960
2628
  return;
@@ -1988,327 +2656,67 @@
1988
2656
  let height;
1989
2657
  if (image instanceof ImageData) {
1990
2658
  width = image.width;
1991
- height = image.height;
1992
- }
1993
- else {
1994
- width = image.width;
1995
- height = image.height;
1996
- }
1997
- // 计算调整后的尺寸
1998
- let newWidth = width;
1999
- let newHeight = height;
2000
- if (keepAspectRatio) {
2001
- if (width > height) {
2002
- if (width > maxWidth) {
2003
- newHeight = Math.round(height * (maxWidth / width));
2004
- newWidth = maxWidth;
2005
- }
2006
- }
2007
- else {
2008
- if (height > maxHeight) {
2009
- newWidth = Math.round(width * (maxHeight / height));
2010
- newHeight = maxHeight;
2011
- }
2012
- }
2013
- }
2014
- else {
2015
- newWidth = Math.min(width, maxWidth);
2016
- newHeight = Math.min(height, maxHeight);
2017
- }
2018
- // 设置canvas尺寸
2019
- canvas.width = newWidth;
2020
- canvas.height = newHeight;
2021
- // 绘制调整后的图像
2022
- if (image instanceof ImageData) {
2023
- // 创建临时canvas存储ImageData
2024
- const tempCanvas = document.createElement('canvas');
2025
- const tempCtx = tempCanvas.getContext('2d');
2026
- if (!tempCtx) {
2027
- throw new Error('无法创建临时Canvas上下文');
2028
- }
2029
- tempCanvas.width = image.width;
2030
- tempCanvas.height = image.height;
2031
- tempCtx.putImageData(image, 0, 0);
2032
- // 绘制调整后的图像
2033
- ctx.drawImage(tempCanvas, 0, 0, width, height, 0, 0, newWidth, newHeight);
2034
- }
2035
- else {
2036
- ctx.drawImage(image, 0, 0, width, height, 0, 0, newWidth, newHeight);
2037
- }
2038
- // 返回调整后的ImageData
2039
- return ctx.getImageData(0, 0, newWidth, newHeight);
2040
- }
2041
- /**
2042
- * 边缘检测算法,用于识别图像中的边缘
2043
- * 基于Sobel算子实现
2044
- *
2045
- * @param imageData 原始图像数据,应已转为灰度图
2046
- * @param threshold 边缘阈值,默认为30
2047
- * @returns 检测到边缘的图像数据
2048
- */
2049
- static detectEdges(imageData, threshold = 30) {
2050
- // 确保输入图像是灰度图
2051
- const grayscaleImage = this.toGrayscale(new ImageData(new Uint8ClampedArray(imageData.data), imageData.width, imageData.height));
2052
- const width = grayscaleImage.width;
2053
- const height = grayscaleImage.height;
2054
- const inputData = grayscaleImage.data;
2055
- const outputData = new Uint8ClampedArray(inputData.length);
2056
- // Sobel算子 - 水平和垂直方向
2057
- const sobelX = [-1, 0, 1, -2, 0, 2, -1, 0, 1];
2058
- const sobelY = [-1, -2, -1, 0, 0, 0, 1, 2, 1];
2059
- // 对每个像素应用Sobel算子
2060
- for (let y = 1; y < height - 1; y++) {
2061
- for (let x = 1; x < width - 1; x++) {
2062
- let gx = 0;
2063
- let gy = 0;
2064
- // 应用卷积
2065
- for (let ky = -1; ky <= 1; ky++) {
2066
- for (let kx = -1; kx <= 1; kx++) {
2067
- const pixelPos = ((y + ky) * width + (x + kx)) * 4;
2068
- const pixelVal = inputData[pixelPos]; // 灰度值
2069
- const kernelIdx = (ky + 1) * 3 + (kx + 1);
2070
- gx += pixelVal * sobelX[kernelIdx];
2071
- gy += pixelVal * sobelY[kernelIdx];
2072
- }
2073
- }
2074
- // 计算梯度强度
2075
- let magnitude = Math.sqrt(gx * gx + gy * gy);
2076
- // 应用阈值
2077
- magnitude = magnitude > threshold ? 255 : 0;
2078
- // 设置输出像素
2079
- const pos = (y * width + x) * 4;
2080
- outputData[pos] = outputData[pos + 1] = outputData[pos + 2] = magnitude;
2081
- outputData[pos + 3] = 255; // 透明度保持完全不透明
2082
- }
2083
- }
2084
- // 处理边缘像素
2085
- for (let i = 0; i < width * 4; i++) {
2086
- // 顶部和底部行
2087
- outputData[i] = 0;
2088
- outputData[(height - 1) * width * 4 + i] = 0;
2089
- }
2090
- for (let i = 0; i < height; i++) {
2091
- // 左右两侧列
2092
- const leftPos = i * width * 4;
2093
- const rightPos = (i * width + width - 1) * 4;
2094
- for (let j = 0; j < 4; j++) {
2095
- outputData[leftPos + j] = 0;
2096
- outputData[rightPos + j] = 0;
2097
- }
2098
- }
2099
- return new ImageData(outputData, width, height);
2100
- }
2101
- /**
2102
- * 卡尼-德里奇边缘检测
2103
- * 相比Sobel更精确的边缘检测算法
2104
- *
2105
- * @param imageData 灰度图像数据
2106
- * @param lowThreshold 低阈值
2107
- * @param highThreshold 高阈值
2108
- * @returns 边缘检测结果
2109
- */
2110
- static cannyEdgeDetection(imageData, lowThreshold = 20, highThreshold = 50) {
2111
- const grayscaleImage = this.toGrayscale(new ImageData(new Uint8ClampedArray(imageData.data), imageData.width, imageData.height));
2112
- // 1. 高斯模糊
2113
- const blurredImage = this.gaussianBlur(grayscaleImage, 1.5);
2114
- // 2. 使用Sobel算子计算梯度
2115
- const { gradientMagnitude, gradientDirection } = this.computeGradients(blurredImage);
2116
- // 3. 非极大值抛弃
2117
- const nonMaxSuppressed = this.nonMaxSuppression(gradientMagnitude, gradientDirection, blurredImage.width, blurredImage.height);
2118
- // 4. 双阈值处理
2119
- const thresholdResult = this.hysteresisThresholding(nonMaxSuppressed, blurredImage.width, blurredImage.height, lowThreshold, highThreshold);
2120
- // 创建输出图像
2121
- const outputData = new Uint8ClampedArray(imageData.data.length);
2122
- // 将结果转换为ImageData
2123
- for (let i = 0; i < thresholdResult.length; i++) {
2124
- const pos = i * 4;
2125
- const value = thresholdResult[i] ? 255 : 0;
2126
- outputData[pos] = outputData[pos + 1] = outputData[pos + 2] = value;
2127
- outputData[pos + 3] = 255;
2128
- }
2129
- return new ImageData(outputData, blurredImage.width, blurredImage.height);
2130
- }
2131
- /**
2132
- * 高斯模糊
2133
- */
2134
- static gaussianBlur(imageData, sigma = 1.5) {
2135
- const width = imageData.width;
2136
- const height = imageData.height;
2137
- const inputData = imageData.data;
2138
- const outputData = new Uint8ClampedArray(inputData.length);
2139
- // 生成高斯核
2140
- const kernelSize = Math.max(3, Math.floor(sigma * 3) * 2 + 1);
2141
- const halfKernel = Math.floor(kernelSize / 2);
2142
- const kernel = this.generateGaussianKernel(kernelSize, sigma);
2143
- // 应用高斯核
2144
- for (let y = 0; y < height; y++) {
2145
- for (let x = 0; x < width; x++) {
2146
- let sum = 0;
2147
- let weightSum = 0;
2148
- for (let ky = -halfKernel; ky <= halfKernel; ky++) {
2149
- for (let kx = -halfKernel; kx <= halfKernel; kx++) {
2150
- const pixelY = Math.min(Math.max(y + ky, 0), height - 1);
2151
- const pixelX = Math.min(Math.max(x + kx, 0), width - 1);
2152
- const pixelPos = (pixelY * width + pixelX) * 4;
2153
- const kernelY = ky + halfKernel;
2154
- const kernelX = kx + halfKernel;
2155
- const weight = kernel[kernelY * kernelSize + kernelX];
2156
- sum += inputData[pixelPos] * weight;
2157
- weightSum += weight;
2158
- }
2159
- }
2160
- const pos = (y * width + x) * 4;
2161
- const value = Math.round(sum / weightSum);
2162
- outputData[pos] = outputData[pos + 1] = outputData[pos + 2] = value;
2163
- outputData[pos + 3] = 255;
2164
- }
2165
- }
2166
- return new ImageData(outputData, width, height);
2167
- }
2168
- /**
2169
- * 生成高斯核
2170
- */
2171
- static generateGaussianKernel(size, sigma) {
2172
- const kernel = new Array(size * size);
2173
- const center = Math.floor(size / 2);
2174
- let sum = 0;
2175
- for (let y = 0; y < size; y++) {
2176
- for (let x = 0; x < size; x++) {
2177
- const distance = Math.sqrt((x - center) ** 2 + (y - center) ** 2);
2178
- const value = Math.exp(-(distance ** 2) / (2 * sigma ** 2));
2179
- kernel[y * size + x] = value;
2180
- sum += value;
2181
- }
2659
+ height = image.height;
2182
2660
  }
2183
- // 归一化
2184
- for (let i = 0; i < kernel.length; i++) {
2185
- kernel[i] /= sum;
2661
+ else {
2662
+ width = image.width;
2663
+ height = image.height;
2186
2664
  }
2187
- return kernel;
2188
- }
2189
- /**
2190
- * 计算梯度强度和方向
2191
- */
2192
- static computeGradients(imageData) {
2193
- const width = imageData.width;
2194
- const height = imageData.height;
2195
- const inputData = imageData.data;
2196
- const gradientMagnitude = new Array(width * height);
2197
- const gradientDirection = new Array(width * height);
2198
- // Sobel算子
2199
- const sobelX = [-1, 0, 1, -2, 0, 2, -1, 0, 1];
2200
- const sobelY = [-1, -2, -1, 0, 0, 0, 1, 2, 1];
2201
- for (let y = 1; y < height - 1; y++) {
2202
- for (let x = 1; x < width - 1; x++) {
2203
- let gx = 0;
2204
- let gy = 0;
2205
- for (let ky = -1; ky <= 1; ky++) {
2206
- for (let kx = -1; kx <= 1; kx++) {
2207
- const pixelPos = ((y + ky) * width + (x + kx)) * 4;
2208
- const pixelVal = inputData[pixelPos];
2209
- const kernelIdx = (ky + 1) * 3 + (kx + 1);
2210
- gx += pixelVal * sobelX[kernelIdx];
2211
- gy += pixelVal * sobelY[kernelIdx];
2212
- }
2665
+ // 计算调整后的尺寸
2666
+ let newWidth = width;
2667
+ let newHeight = height;
2668
+ if (keepAspectRatio) {
2669
+ if (width > height) {
2670
+ if (width > maxWidth) {
2671
+ newHeight = Math.round(height * (maxWidth / width));
2672
+ newWidth = maxWidth;
2213
2673
  }
2214
- const idx = y * width + x;
2215
- gradientMagnitude[idx] = Math.sqrt(gx * gx + gy * gy);
2216
- gradientDirection[idx] = Math.atan2(gy, gx);
2217
2674
  }
2218
- }
2219
- // 处理边界
2220
- for (let y = 0; y < height; y++) {
2221
- for (let x = 0; x < width; x++) {
2222
- if (y === 0 || y === height - 1 || x === 0 || x === width - 1) {
2223
- const idx = y * width + x;
2224
- gradientMagnitude[idx] = 0;
2225
- gradientDirection[idx] = 0;
2675
+ else {
2676
+ if (height > maxHeight) {
2677
+ newWidth = Math.round(width * (maxHeight / height));
2678
+ newHeight = maxHeight;
2226
2679
  }
2227
2680
  }
2228
2681
  }
2229
- return { gradientMagnitude, gradientDirection };
2682
+ else {
2683
+ newWidth = Math.min(width, maxWidth);
2684
+ newHeight = Math.min(height, maxHeight);
2685
+ }
2686
+ // 设置canvas尺寸
2687
+ canvas.width = newWidth;
2688
+ canvas.height = newHeight;
2689
+ // 绘制调整后的图像
2690
+ if (image instanceof ImageData) {
2691
+ // 创建临时canvas存储ImageData
2692
+ const tempCanvas = document.createElement('canvas');
2693
+ const tempCtx = tempCanvas.getContext('2d');
2694
+ if (!tempCtx) {
2695
+ throw new Error('无法创建临时Canvas上下文');
2696
+ }
2697
+ tempCanvas.width = image.width;
2698
+ tempCanvas.height = image.height;
2699
+ tempCtx.putImageData(image, 0, 0);
2700
+ // 绘制调整后的图像
2701
+ ctx.drawImage(tempCanvas, 0, 0, width, height, 0, 0, newWidth, newHeight);
2702
+ }
2703
+ else {
2704
+ ctx.drawImage(image, 0, 0, width, height, 0, 0, newWidth, newHeight);
2705
+ }
2706
+ // 返回调整后的ImageData
2707
+ return ctx.getImageData(0, 0, newWidth, newHeight);
2230
2708
  }
2231
2709
  /**
2232
- * 非极大值抛弃
2710
+ * @deprecated 请使用 EdgeDetector.detectEdges()
2233
2711
  */
2234
- static nonMaxSuppression(gradientMagnitude, gradientDirection, width, height) {
2235
- const result = new Array(width * height).fill(0);
2236
- for (let y = 1; y < height - 1; y++) {
2237
- for (let x = 1; x < width - 1; x++) {
2238
- const idx = y * width + x;
2239
- const magnitude = gradientMagnitude[idx];
2240
- const direction = gradientDirection[idx];
2241
- // 将方向转化为角度
2242
- const degrees = (direction * 180 / Math.PI + 180) % 180;
2243
- // 获取相邻像素索引
2244
- let neighbor1Idx, neighbor2Idx;
2245
- // 将方向量化为四个方向: 0°, 45°, 90°, 135°
2246
- if ((degrees >= 0 && degrees < 22.5) || (degrees >= 157.5 && degrees <= 180)) {
2247
- // 水平方向
2248
- neighbor1Idx = idx - 1;
2249
- neighbor2Idx = idx + 1;
2250
- }
2251
- else if (degrees >= 22.5 && degrees < 67.5) {
2252
- // 45度方向
2253
- neighbor1Idx = (y - 1) * width + (x + 1);
2254
- neighbor2Idx = (y + 1) * width + (x - 1);
2255
- }
2256
- else if (degrees >= 67.5 && degrees < 112.5) {
2257
- // 垂直方向
2258
- neighbor1Idx = (y - 1) * width + x;
2259
- neighbor2Idx = (y + 1) * width + x;
2260
- }
2261
- else {
2262
- // 135度方向
2263
- neighbor1Idx = (y - 1) * width + (x - 1);
2264
- neighbor2Idx = (y + 1) * width + (x + 1);
2265
- }
2266
- // 检查当前像素是否是最大值
2267
- if (magnitude >= gradientMagnitude[neighbor1Idx] &&
2268
- magnitude >= gradientMagnitude[neighbor2Idx]) {
2269
- result[idx] = magnitude;
2270
- }
2271
- }
2272
- }
2273
- return result;
2712
+ static detectEdges(imageData, threshold = 30) {
2713
+ return EdgeDetector.detectEdges(imageData, threshold);
2274
2714
  }
2275
2715
  /**
2276
- * 双阈值处理
2716
+ * @deprecated 请使用 EdgeDetector.cannyEdgeDetection()
2277
2717
  */
2278
- static hysteresisThresholding(nonMaxSuppressed, width, height, lowThreshold, highThreshold) {
2279
- const result = new Array(width * height).fill(false);
2280
- const visited = new Array(width * height).fill(false);
2281
- const stack = [];
2282
- // 标记强边缘点
2283
- for (let i = 0; i < nonMaxSuppressed.length; i++) {
2284
- if (nonMaxSuppressed[i] >= highThreshold) {
2285
- result[i] = true;
2286
- stack.push(i);
2287
- visited[i] = true;
2288
- }
2289
- }
2290
- // 使用深度优先搜索连接弱边缘
2291
- const dx = [-1, 0, 1, -1, 1, -1, 0, 1];
2292
- const dy = [-1, -1, -1, 0, 0, 1, 1, 1];
2293
- while (stack.length > 0) {
2294
- const currentIdx = stack.pop();
2295
- const currentX = currentIdx % width;
2296
- const currentY = Math.floor(currentIdx / width);
2297
- // 检查88个相邻方向
2298
- for (let i = 0; i < 8; i++) {
2299
- const newX = currentX + dx[i];
2300
- const newY = currentY + dy[i];
2301
- if (newX >= 0 && newX < width && newY >= 0 && newY < height) {
2302
- const newIdx = newY * width + newX;
2303
- if (!visited[newIdx] && nonMaxSuppressed[newIdx] >= lowThreshold) {
2304
- result[newIdx] = true;
2305
- stack.push(newIdx);
2306
- visited[newIdx] = true;
2307
- }
2308
- }
2309
- }
2310
- }
2311
- return result;
2718
+ static cannyEdgeDetection(imageData, lowThreshold = 20, highThreshold = 50) {
2719
+ return EdgeDetector.cannyEdgeDetection(imageData, lowThreshold, highThreshold);
2312
2720
  }
2313
2721
  }
2314
2722
 
@@ -2598,7 +3006,7 @@
2598
3006
  // 识别图像
2599
3007
  const { data } = await worker.recognize(input.imageBase64);
2600
3008
  // 解析身份证信息
2601
- const idCardInfo = parseIDCardText(data.text);
3009
+ const idCardInfo = IDCardTextParser.parse(data.text);
2602
3010
  // 释放Worker资源
2603
3011
  await worker.terminate();
2604
3012
  const processingTime = performance.now() - startTime;
@@ -2612,160 +3020,6 @@
2612
3020
  };
2613
3021
  }
2614
3022
  }
2615
- /**
2616
- * 解析身份证文本
2617
- * @param text OCR识别的文本
2618
- * @returns 解析后的身份证信息
2619
- */
2620
- function parseIDCardText(text) {
2621
- const info = {};
2622
- // 预处理文本,清除多余空白
2623
- const processedText = text.replace(/\s+/g, ' ').trim();
2624
- // 解析身份证号码
2625
- const idNumberRegex = /(\d{17}[\dX])/;
2626
- const idNumberWithPrefixRegex = /公民身份号码[\s\:]*(\d{17}[\dX])/;
2627
- const basicMatch = processedText.match(idNumberRegex);
2628
- const prefixMatch = processedText.match(idNumberWithPrefixRegex);
2629
- if (prefixMatch && prefixMatch[1]) {
2630
- info.idNumber = prefixMatch[1];
2631
- }
2632
- else if (basicMatch && basicMatch[1]) {
2633
- info.idNumber = basicMatch[1];
2634
- }
2635
- // 解析姓名
2636
- const nameWithLabelRegex = /姓名[\s\:]*([一-龥]{2,4})/;
2637
- const nameMatch = processedText.match(nameWithLabelRegex);
2638
- if (nameMatch && nameMatch[1]) {
2639
- info.name = nameMatch[1].trim();
2640
- }
2641
- else {
2642
- // 备用方案:查找短行且内容全是汉字
2643
- const lines = processedText.split('\n').filter(line => line.trim());
2644
- for (const line of lines) {
2645
- if (line.length >= 2 &&
2646
- line.length <= 5 &&
2647
- /^[一-龥]+$/.test(line) &&
2648
- !/性别|民族|住址|公民|签发|有效/.test(line)) {
2649
- info.name = line.trim();
2650
- break;
2651
- }
2652
- }
2653
- }
2654
- // 解析性别和民族
2655
- const genderAndNationalityRegex = /性别[\s\:]*([男女])[\s ]*民族[\s\:]*([一-龥]+族)/;
2656
- const genderOnlyRegex = /性别[\s\:]*([男女])/;
2657
- const nationalityOnlyRegex = /民族[\s\:]*([一-龥]+族)/;
2658
- const genderNationalityMatch = processedText.match(genderAndNationalityRegex);
2659
- const genderOnlyMatch = processedText.match(genderOnlyRegex);
2660
- const nationalityOnlyMatch = processedText.match(nationalityOnlyRegex);
2661
- if (genderNationalityMatch) {
2662
- info.gender = genderNationalityMatch[1];
2663
- info.ethnicity = genderNationalityMatch[2];
2664
- }
2665
- else {
2666
- if (genderOnlyMatch)
2667
- info.gender = genderOnlyMatch[1];
2668
- if (nationalityOnlyMatch)
2669
- info.ethnicity = nationalityOnlyMatch[1];
2670
- }
2671
- // 根据内容判断身份证类型
2672
- if (processedText.includes('出生') || processedText.includes('公民身份号码')) {
2673
- info.type = exports.IDCardType.FRONT; // 确保类型为枚举值而不是字符串
2674
- }
2675
- else if (processedText.includes('签发机关') || processedText.includes('有效期')) {
2676
- info.type = exports.IDCardType.BACK; // 确保类型为枚举值而不是字符串
2677
- }
2678
- // 解析出生日期
2679
- const birthDateRegex1 = /出生[\s\:]*(\d{4})年(\d{1,2})月(\d{1,2})[日号]/;
2680
- const birthDateRegex2 = /出生[\s\:]*(\d{4})[-\/\.](\d{1,2})[-\/\.](\d{1,2})/;
2681
- const birthDateRegex3 = /出生日期[\s\:]*(\d{4})[-\/\.\u5e74](\d{1,2})[-\/\.\u6708](\d{1,2})[日号]?/;
2682
- const birthDateMatch = processedText.match(birthDateRegex1) ||
2683
- processedText.match(birthDateRegex2) ||
2684
- processedText.match(birthDateRegex3);
2685
- if (!birthDateMatch && info.idNumber && info.idNumber.length === 18) {
2686
- const year = info.idNumber.substring(6, 10);
2687
- const month = info.idNumber.substring(10, 12);
2688
- const day = info.idNumber.substring(12, 14);
2689
- info.birthDate = `${year}-${month}-${day}`;
2690
- }
2691
- else if (birthDateMatch) {
2692
- const year = birthDateMatch[1];
2693
- const month = birthDateMatch[2].padStart(2, '0');
2694
- const day = birthDateMatch[3].padStart(2, '0');
2695
- info.birthDate = `${year}-${month}-${day}`;
2696
- }
2697
- // 解析地址
2698
- const addressRegex1 = /住址[\s\:]*([\s\S]*?)(?=公民身份|出生|性别|签发)/;
2699
- const addressRegex2 = /住址[\s\:]*([一-龥a-zA-Z0-9\s\.\-]+)/;
2700
- const addressMatch = processedText.match(addressRegex1) || processedText.match(addressRegex2);
2701
- if (addressMatch && addressMatch[1]) {
2702
- info.address = addressMatch[1]
2703
- .replace(/\s+/g, '')
2704
- .replace(/\n/g, '')
2705
- .trim();
2706
- if (info.address.length > 70) {
2707
- info.address = info.address.substring(0, 70);
2708
- }
2709
- if (!/[一-龥]/.test(info.address)) {
2710
- info.address = '';
2711
- }
2712
- }
2713
- // 解析签发机关
2714
- const authorityRegex1 = /签发机关[\s\:]*([\s\S]*?)(?=有效|公民|出生|\d{8}|$)/;
2715
- const authorityRegex2 = /签发机关[\s\:]*([一-龥\s]+)/;
2716
- const authorityMatch = processedText.match(authorityRegex1) ||
2717
- processedText.match(authorityRegex2);
2718
- if (authorityMatch && authorityMatch[1]) {
2719
- info.issueAuthority = authorityMatch[1]
2720
- .replace(/\s+/g, '')
2721
- .replace(/\n/g, '')
2722
- .trim();
2723
- }
2724
- // 解析有效期限
2725
- const validPeriodRegex1 = /有效期限[\s\:]*(\d{4}[-\.\u5e74\s]\d{1,2}[-\.\u6708\s]\d{1,2}[日\s]*)[-\s]*(至|-)[-\s]*(\d{4}[-\.\u5e74\s]\d{1,2}[-\.\u6708\s]\d{1,2}[日]*|[永久长期]*)/;
2726
- const validPeriodRegex2 = /有效期限[\s\:]*(\d{8})[-\s]*(至|-)[-\s]*(\d{8}|[永久长期]*)/;
2727
- const validPeriodMatch = processedText.match(validPeriodRegex1) ||
2728
- processedText.match(validPeriodRegex2);
2729
- if (validPeriodMatch) {
2730
- if (validPeriodMatch[1] && validPeriodMatch[3]) {
2731
- const startDate = formatDateString(validPeriodMatch[1]);
2732
- const endDate = /\d/.test(validPeriodMatch[3])
2733
- ? formatDateString(validPeriodMatch[3])
2734
- : '长期有效';
2735
- info.validFrom = startDate;
2736
- info.validTo = endDate;
2737
- info.validPeriod = `${startDate}-${endDate}`;
2738
- }
2739
- else {
2740
- info.validPeriod = validPeriodMatch[0].replace('有效期限', '').trim();
2741
- }
2742
- }
2743
- return info;
2744
- }
2745
- /**
2746
- * 格式化日期字符串
2747
- * @param dateStr 原始日期字符串
2748
- * @returns 格式化后的日期字符串
2749
- */
2750
- function formatDateString(dateStr) {
2751
- // 提取年月日
2752
- const dateMatch = dateStr.match(/(\d{4})[-\.\u5e74\s]*(\d{1,2})[-\.\u6708\s]*(\d{1,2})[日]*/);
2753
- if (dateMatch) {
2754
- const year = dateMatch[1];
2755
- const month = dateMatch[2].padStart(2, '0');
2756
- const day = dateMatch[3].padStart(2, '0');
2757
- return `${year}-${month}-${day}`;
2758
- }
2759
- // 纯数字格式如 20220101
2760
- if (/^\d{8}$/.test(dateStr)) {
2761
- const year = dateStr.substring(0, 4);
2762
- const month = dateStr.substring(4, 6);
2763
- const day = dateStr.substring(6, 8);
2764
- return `${year}-${month}-${day}`;
2765
- }
2766
- // 无法格式化,返回原始字符串
2767
- return dateStr;
2768
- }
2769
3023
 
2770
3024
  /**
2771
3025
  * @file OCR处理器
@@ -2817,7 +3071,7 @@
2817
3071
  /**
2818
3072
  * 初始化OCR引擎
2819
3073
  *
2820
- * 加载Tesseract OCR引擎和中文简体语言包,并设置适合身份证识别的参数
3074
+ * 加载Tesseract OCR引擎和中文简体语言包,并设置适合身份证识别的参数
2821
3075
  *
2822
3076
  * @returns {Promise<void>} 初始化完成的Promise
2823
3077
  */
@@ -2839,11 +3093,11 @@
2839
3093
  await this.worker.loadLanguage("chi_sim");
2840
3094
  await this.worker.initialize("chi_sim");
2841
3095
  await this.worker.setParameters({
2842
- tessedit_char_whitelist: "0123456789X年月日壹贰叁肆伍陆柒捌玖拾民族汉满回维吾尔藏苗彝壮朝鲜侗瑶白土家哈尼哈萨克傣黎傈僳佤高山拉祜水东乡纳西景颇柯尔克孜达斡尔仫佬羌布朗撒拉毛南仡佬锡伯阿昌普米塔吉克怒乌孜别克俄罗斯鄂温克德昂保安裕固京塔塔尔独龙鄂伦春赫哲门巴珞巴基诺男女住址出生公民身份号码签发机关有效期省市区县乡镇街道号楼单元室ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", // 优化字符白名单,增加常见地址字符,移除部分不常用汉字
3096
+ tessedit_char_whitelist: "0123456789X年月日壹贰叁肆伍陆柒捌玖拾民族汉满回维吾尔藏苗彝壮朝鲜侗瑶白土家哈尼哈萨克傣黎傈僳佤高山拉祜水东乡纳西景颇柯尔克孜达斡尔仫佬羌布朗撒拉毛南仡佬锡伯阿昌普米塔吉克怒乌孜别克俄罗斯鄂温克德昂保安裕固京塔塔尔独龙鄂伦春赫哲门巴珞巴基诺男女住址出生公民身份号码签发机关有效期省市区县乡镇街道号楼单元室ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", // 优化字符白名单,增加常见地址字符,移除部分不常用汉字
2843
3097
  });
2844
- // 增加一些针对性的参数,提高识别率
3098
+ // 增加一些针对性的参数,提高识别率
2845
3099
  await this.worker.setParameters({
2846
- tessedit_pageseg_mode: 7, // PSM_SINGLE_LINE,使用数字而不是字符串
3100
+ tessedit_pageseg_mode: 7, // PSM_SINGLE_LINE,使用数字而不是字符串
2847
3101
  preserve_interword_spaces: "1", // 保留单词间的空格
2848
3102
  });
2849
3103
  this.initialized = true;
@@ -2859,7 +3113,7 @@
2859
3113
  if (!this.initialized) {
2860
3114
  await this.initialize();
2861
3115
  }
2862
- // 计算图像指纹,用于缓存查找
3116
+ // 计算图像指纹,用于缓存查找
2863
3117
  if (this.options.enableCache) {
2864
3118
  const fingerprint = calculateImageFingerprint(imageData);
2865
3119
  // 检查缓存中是否有结果
@@ -2876,7 +3130,7 @@
2876
3130
  const enhancedImage = ImageProcessor.batchProcess(downsampledImage, {
2877
3131
  brightness: this.options.brightness !== undefined ? this.options.brightness : 10, // 调整默认亮度
2878
3132
  contrast: this.options.contrast !== undefined ? this.options.contrast : 20, // 调整默认对比度
2879
- sharpen: true, // 默认启用锐化,通常对OCR有益
3133
+ sharpen: true, // 默认启用锐化,通常对OCR有益
2880
3134
  });
2881
3135
  // 转换为base64供Tesseract处理
2882
3136
  // 创建一个canvas元素
@@ -2898,11 +3152,11 @@
2898
3152
  // 使用Worker线程处理
2899
3153
  const result = await this.ocrWorker.postMessage({
2900
3154
  imageBase64: base64Image,
2901
- // 不传递函数对象,避免DataCloneError
3155
+ // 不传递函数对象,避免DataCloneError
2902
3156
  tessWorkerOptions: {},
2903
3157
  });
2904
3158
  idCardInfo = result.idCardInfo;
2905
- this.options.logger?.(`OCR处理完成,用时: ${result.processingTime.toFixed(2)}ms`);
3159
+ this.options.logger?.(`OCR处理完成,用时: ${result.processingTime.toFixed(2)}ms`);
2906
3160
  }
2907
3161
  else {
2908
3162
  // 使用主线程处理
@@ -2915,9 +3169,9 @@
2915
3169
  }
2916
3170
  const { data } = (await this.worker.recognize(canvas));
2917
3171
  // 解析身份证信息
2918
- idCardInfo = this.parseIDCardText(data.text);
3172
+ idCardInfo = IDCardTextParser.parse(data.text);
2919
3173
  const processingTime = performance.now() - startTime;
2920
- this.options.logger?.(`OCR处理完成,用时: ${processingTime.toFixed(2)}ms`);
3174
+ this.options.logger?.(`OCR处理完成,用时: ${processingTime.toFixed(2)}ms`);
2921
3175
  }
2922
3176
  // 缓存结果
2923
3177
  if (this.options.enableCache) {
@@ -2934,7 +3188,7 @@
2934
3188
  ? JSON.stringify(error)
2935
3189
  : String(error);
2936
3190
  this.options.logger?.(`OCR识别错误: ${errorMessage}`);
2937
- // 返回 null,让调用方知道识别失败
3191
+ // 返回 null,让调用方知道识别失败
2938
3192
  return null;
2939
3193
  }
2940
3194
  }
@@ -2947,198 +3201,6 @@
2947
3201
  * @param {string} text - OCR识别到的文本
2948
3202
  * @returns {IDCardInfo} 提取到的身份证信息对象
2949
3203
  */
2950
- /**
2951
- * 格式化日期字符串为标准格式 (YYYY-MM-DD)
2952
- * @param dateStr 原始日期字符串
2953
- * @returns 格式化后的日期字符串
2954
- */
2955
- formatDateString(dateStr) {
2956
- // 先尝试提取年月日
2957
- const dateMatch = dateStr.match(/(\d{4})[-\.\u5e74\s]*(\d{1,2})[-\.\u6708\s]*(\d{1,2})[日]*/);
2958
- if (dateMatch) {
2959
- const year = dateMatch[1];
2960
- const month = dateMatch[2].padStart(2, "0");
2961
- const day = dateMatch[3].padStart(2, "0");
2962
- return `${year}-${month}-${day}`;
2963
- }
2964
- // 如果是纯数字格式如 20220101
2965
- if (/^\d{8}$/.test(dateStr)) {
2966
- const year = dateStr.substring(0, 4);
2967
- const month = dateStr.substring(4, 6);
2968
- const day = dateStr.substring(6, 8);
2969
- return `${year}-${month}-${day}`;
2970
- }
2971
- // 如果无法格式化,返回原始字符串
2972
- return dateStr;
2973
- }
2974
- /**
2975
- * 验证身份证号是否符合规则
2976
- * @param idNumber 身份证号
2977
- * @returns 是否有效
2978
- */
2979
- validateIDNumber(idNumber) {
2980
- // 基本验证,校验位有效性和长度
2981
- if (!idNumber || idNumber.length !== 18) {
2982
- return false;
2983
- }
2984
- // 检查格式,前17位必须为数字,最后一位可以是数字或'X'
2985
- const pattern = /^\d{17}[\dX]$/;
2986
- if (!pattern.test(idNumber)) {
2987
- return false;
2988
- }
2989
- // 检查日期部分
2990
- parseInt(idNumber.substr(6, 4));
2991
- const month = parseInt(idNumber.substr(10, 2));
2992
- const day = parseInt(idNumber.substr(12, 2));
2993
- if (month < 1 || month > 12 || day < 1 || day > 31) {
2994
- return false;
2995
- }
2996
- // 更详细的检查可以添加校验位的验证等逻辑...
2997
- return true;
2998
- }
2999
- parseIDCardText(text) {
3000
- const info = {};
3001
- // 预处理文本,清除多余空白
3002
- const processedText = text.replace(/\s+/g, " ").trim();
3003
- // 拆分为行,并过滤空行
3004
- const lines = processedText.split("\n").filter((line) => line.trim());
3005
- // 解析身份证号码 - 多种模式匹配
3006
- // 1. 普通18位身份证号模式
3007
- const idNumberRegex = /(\d{17}[\dX])/;
3008
- // 2. 带前缀的模式
3009
- const idNumberWithPrefixRegex = /公民身份号码[\s\:]*(\d{17}[\dX])/;
3010
- // 尝试所有模式
3011
- let idNumber = null;
3012
- const basicMatch = processedText.match(idNumberRegex);
3013
- const prefixMatch = processedText.match(idNumberWithPrefixRegex);
3014
- if (prefixMatch && prefixMatch[1]) {
3015
- idNumber = prefixMatch[1]; // 首选带前缀的匹配,因为最可靠
3016
- }
3017
- else if (basicMatch && basicMatch[1]) {
3018
- idNumber = basicMatch[1]; // 其次是常规匹配
3019
- }
3020
- if (idNumber) {
3021
- info.idNumber = idNumber;
3022
- }
3023
- // 解析姓名 - 使用多种策略
3024
- // 1. 直接匹配姓名标签近的内容
3025
- const nameWithLabelRegex = /姓名[\s\:]*([一-龥]{2,4})/;
3026
- const nameMatch = processedText.match(nameWithLabelRegex);
3027
- // 2. 分析行文本寻找姓名
3028
- if (nameMatch && nameMatch[1]) {
3029
- info.name = nameMatch[1].trim();
3030
- }
3031
- else {
3032
- // 备用方案:查找短行且内容全是汉字
3033
- for (const line of lines) {
3034
- if (line.length >= 2 &&
3035
- line.length <= 5 &&
3036
- /^[一-龥]+$/.test(line) &&
3037
- !/性别|民族|住址|公民|签发|有效/.test(line)) {
3038
- info.name = line.trim();
3039
- break;
3040
- }
3041
- }
3042
- }
3043
- // 解析性别和民族 - 多种模式匹配
3044
- // 1. 标准格式匹配
3045
- const genderAndNationalityRegex = /性别[\s\:]*([男女])[\s ]*民族[\s\:]*([一-龥]+族)/;
3046
- const genderNationalityMatch = processedText.match(genderAndNationalityRegex);
3047
- // 2. 只匹配性别
3048
- const genderOnlyRegex = /性别[\s\:]*([男女])/;
3049
- const genderOnlyMatch = processedText.match(genderOnlyRegex);
3050
- // 3. 只匹配民族
3051
- const nationalityOnlyRegex = /民族[\s\:]*([一-龥]+族)/;
3052
- const nationalityOnlyMatch = processedText.match(nationalityOnlyRegex);
3053
- if (genderNationalityMatch) {
3054
- info.gender = genderNationalityMatch[1];
3055
- info.nationality = genderNationalityMatch[2];
3056
- }
3057
- else {
3058
- // 分开获取
3059
- if (genderOnlyMatch)
3060
- info.gender = genderOnlyMatch[1];
3061
- if (nationalityOnlyMatch)
3062
- info.nationality = nationalityOnlyMatch[1];
3063
- }
3064
- // 解析出生日期 - 支持多种格式
3065
- // 1. 标准格式:YYYY年MM月DD日
3066
- const birthDateRegex1 = /出生[\s\:]*(\d{4})年(\d{1,2})月(\d{1,2})[日号]/;
3067
- // 2. 美式日期格式:YYYY-MM-DD或YYYY/MM/DD
3068
- const birthDateRegex2 = /出生[\s\:]*(\d{4})[-\/\.](\d{1,2})[-\/\.](\d{1,2})/;
3069
- // 3. 带前缀的格式
3070
- const birthDateRegex3 = /出生日期[\s\:]*(\d{4})[-\/\.\u5e74](\d{1,2})[-\/\.\u6708](\d{1,2})[日号]?/;
3071
- let birthDateMatch = processedText.match(birthDateRegex1) ||
3072
- processedText.match(birthDateRegex2) ||
3073
- processedText.match(birthDateRegex3);
3074
- // 4. 从身份证号码中提取出生日期(如果上述方法失败)
3075
- if (!birthDateMatch && info.idNumber && info.idNumber.length === 18) {
3076
- const year = info.idNumber.substring(6, 10);
3077
- const month = info.idNumber.substring(10, 12);
3078
- const day = info.idNumber.substring(12, 14);
3079
- info.birthDate = `${year}-${month}-${day}`;
3080
- }
3081
- else if (birthDateMatch) {
3082
- // 确保月份和日期是两位数
3083
- const year = birthDateMatch[1];
3084
- const month = birthDateMatch[2].padStart(2, "0");
3085
- const day = birthDateMatch[3].padStart(2, "0");
3086
- info.birthDate = `${year}-${month}-${day}`;
3087
- }
3088
- // 解析地址 - 改进的正则匹配
3089
- // 1. 常规模式
3090
- const addressRegex1 = /住址[\s\:]*([\s\S]*?)(?=公民身份|出生|性别|签发)/;
3091
- // 2. 更宽松的模式
3092
- const addressRegex2 = /住址[\s\:]*([一-龥a-zA-Z0-9\s\.\-]+)/;
3093
- const addressMatch = processedText.match(addressRegex1) || processedText.match(addressRegex2);
3094
- if (addressMatch && addressMatch[1]) {
3095
- // 清理地址中的常见错误和多余空格
3096
- info.address = addressMatch[1]
3097
- .replace(/\s+/g, "")
3098
- .replace(/\n/g, "")
3099
- .trim();
3100
- // 限制地址长度并判断地址合理性
3101
- if (info.address.length > 70) {
3102
- info.address = info.address.substring(0, 70);
3103
- }
3104
- // 确保地址是合理的(不仅仅包含符号或数字)
3105
- if (!/[一-龥]/.test(info.address)) {
3106
- info.address = ""; // 如果没有中文字符,可能不是有效地址
3107
- }
3108
- }
3109
- // 解析签发机关
3110
- const authorityRegex1 = /签发机关[\s\:]*([\s\S]*?)(?=有效|公民|出生|\d{8}|$)/;
3111
- const authorityRegex2 = /签发机关[\s\:]*([一-龥\s]+)/;
3112
- const authorityMatch = processedText.match(authorityRegex1) ||
3113
- processedText.match(authorityRegex2);
3114
- if (authorityMatch && authorityMatch[1]) {
3115
- info.issuingAuthority = authorityMatch[1]
3116
- .replace(/\s+/g, "")
3117
- .replace(/\n/g, "")
3118
- .trim();
3119
- }
3120
- // 解析有效期限 - 支持多种格式
3121
- // 1. 常规格式:YYYY.MM.DD-YYYY.MM.DD
3122
- const validPeriodRegex1 = /有效期限[\s\:]*(\d{4}[-\.\u5e74\s]\d{1,2}[-\.\u6708\s]\d{1,2}[日\s]*)[-\s]*(至|-)[-\s]*(\d{4}[-\.\u5e74\s]\d{1,2}[-\.\u6708\s]\d{1,2}[日]*|[永久长期]*)/;
3123
- // 2. 简化格式:YYYYMMDD-YYYYMMDD
3124
- const validPeriodRegex2 = /有效期限[\s\:]*(\d{8})[-\s]*(至|-)[-\s]*(\d{8}|[永久长期]*)/;
3125
- const validPeriodMatch = processedText.match(validPeriodRegex1) ||
3126
- processedText.match(validPeriodRegex2);
3127
- if (validPeriodMatch) {
3128
- // 格式化为统一的有效期限形式
3129
- if (validPeriodMatch[1] && validPeriodMatch[3]) {
3130
- const startDate = this.formatDateString(validPeriodMatch[1]);
3131
- const endDate = /\d/.test(validPeriodMatch[3])
3132
- ? this.formatDateString(validPeriodMatch[3])
3133
- : "长期有效";
3134
- info.validPeriod = `${startDate}-${endDate}`;
3135
- }
3136
- else {
3137
- info.validPeriod = validPeriodMatch[0].replace("有效期限", "").trim();
3138
- }
3139
- }
3140
- return info;
3141
- }
3142
3204
  /**
3143
3205
  * 清除结果缓存
3144
3206
  */