id-scanner-lib 1.0.0 → 1.2.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.
Files changed (62) hide show
  1. package/README.md +173 -0
  2. package/dist/core.d.ts +77 -0
  3. package/dist/id-recognition/data-extractor.d.ts +31 -0
  4. package/dist/id-recognition/id-detector.d.ts +25 -1
  5. package/dist/id-scanner-core.esm.js +10870 -0
  6. package/dist/id-scanner-core.esm.js.map +1 -0
  7. package/dist/id-scanner-core.js +10882 -0
  8. package/dist/id-scanner-core.js.map +1 -0
  9. package/dist/id-scanner-core.min.js +9 -0
  10. package/dist/id-scanner-core.min.js.map +1 -0
  11. package/dist/id-scanner-ocr.esm.js +1625 -0
  12. package/dist/id-scanner-ocr.esm.js.map +1 -0
  13. package/dist/id-scanner-ocr.js +1634 -0
  14. package/dist/id-scanner-ocr.js.map +1 -0
  15. package/dist/id-scanner-ocr.min.js +9 -0
  16. package/dist/id-scanner-ocr.min.js.map +1 -0
  17. package/dist/id-scanner-qr.esm.js +773 -0
  18. package/dist/id-scanner-qr.esm.js.map +1 -0
  19. package/dist/id-scanner-qr.js +782 -0
  20. package/dist/id-scanner-qr.js.map +1 -0
  21. package/dist/id-scanner-qr.min.js +9 -0
  22. package/dist/id-scanner-qr.min.js.map +1 -0
  23. package/dist/id-scanner.js +1954 -94656
  24. package/dist/id-scanner.js.map +1 -1
  25. package/dist/id-scanner.min.js +7 -7
  26. package/dist/id-scanner.min.js.map +1 -1
  27. package/dist/index-umd.d.ts +96 -0
  28. package/dist/index.d.ts +23 -88
  29. package/dist/ocr-module.d.ts +67 -0
  30. package/dist/qr-module.d.ts +68 -0
  31. package/dist/types/core.d.ts +77 -0
  32. package/dist/types/demo/demo.d.ts +14 -0
  33. package/dist/types/id-recognition/data-extractor.d.ts +105 -0
  34. package/dist/types/id-recognition/id-detector.d.ts +100 -0
  35. package/dist/types/id-recognition/ocr-processor.d.ts +64 -0
  36. package/dist/types/index-umd.d.ts +96 -0
  37. package/dist/types/index.d.ts +78 -0
  38. package/dist/types/ocr-module.d.ts +67 -0
  39. package/dist/types/qr-module.d.ts +68 -0
  40. package/dist/types/scanner/barcode-scanner.d.ts +90 -0
  41. package/dist/types/scanner/qr-scanner.d.ts +80 -0
  42. package/dist/types/utils/camera.d.ts +81 -0
  43. package/dist/types/utils/image-processing.d.ts +75 -0
  44. package/dist/types/utils/types.d.ts +65 -0
  45. package/dist/utils/camera.d.ts +18 -13
  46. package/dist/utils/types.d.ts +6 -6
  47. package/package.json +25 -4
  48. package/src/core.ts +138 -0
  49. package/src/id-recognition/data-extractor.ts +97 -0
  50. package/src/id-recognition/id-detector.ts +230 -67
  51. package/src/id-recognition/ocr-processor.ts +145 -27
  52. package/src/id-recognition/ocr-worker.ts +146 -0
  53. package/src/index-umd.ts +240 -0
  54. package/src/index.ts +125 -139
  55. package/src/ocr-module.ts +139 -0
  56. package/src/qr-module.ts +129 -0
  57. package/src/utils/camera.ts +61 -36
  58. package/src/utils/image-processing.ts +204 -0
  59. package/src/utils/performance.ts +208 -0
  60. package/src/utils/resource-manager.ts +198 -0
  61. package/src/utils/types.ts +23 -6
  62. package/src/utils/worker.ts +173 -0
@@ -0,0 +1,96 @@
1
+ /**
2
+ * @file ID扫描识别库UMD格式入口文件
3
+ * @description 专门为UMD格式构建的入口,使用静态导入而非动态导入
4
+ * @module IDScannerLib
5
+ * @version 1.1.0
6
+ * @license MIT
7
+ */
8
+ import { CameraOptions } from './utils/camera';
9
+ import { IDCardInfo } from './utils/types';
10
+ import type { QRScannerOptions } from './scanner/qr-scanner';
11
+ import type { BarcodeScannerOptions } from './scanner/barcode-scanner';
12
+ import { QRScanner } from './scanner/qr-scanner';
13
+ import { BarcodeScanner } from './scanner/barcode-scanner';
14
+ import { IDCardDetector } from './id-recognition/id-detector';
15
+ import { OCRProcessor } from './id-recognition/ocr-processor';
16
+ import { DataExtractor } from './id-recognition/data-extractor';
17
+ import { ImageProcessor } from './utils/image-processing';
18
+ /**
19
+ * IDScanner配置选项接口
20
+ */
21
+ export interface IDScannerOptions {
22
+ cameraOptions?: CameraOptions;
23
+ qrScannerOptions?: QRScannerOptions;
24
+ barcodeScannerOptions?: BarcodeScannerOptions;
25
+ onQRCodeScanned?: (result: string) => void;
26
+ onBarcodeScanned?: (result: string) => void;
27
+ onIDCardScanned?: (info: IDCardInfo) => void;
28
+ onError?: (error: Error) => void;
29
+ }
30
+ /**
31
+ * IDScanner 主类
32
+ * UMD版本使用静态导入实现
33
+ */
34
+ export declare class IDScanner {
35
+ private options;
36
+ private camera;
37
+ private qrScanner;
38
+ private barcodeScanner;
39
+ private idDetector;
40
+ private ocrProcessor;
41
+ private dataExtractor;
42
+ private scanMode;
43
+ private videoElement;
44
+ /**
45
+ * 构造函数
46
+ * @param options 配置选项
47
+ */
48
+ constructor(options?: IDScannerOptions);
49
+ /**
50
+ * 初始化模块
51
+ * 根据需要初始化OCR引擎
52
+ */
53
+ initialize(): Promise<void>;
54
+ /**
55
+ * 启动二维码扫描
56
+ * @param videoElement HTML视频元素
57
+ */
58
+ startQRScanner(videoElement: HTMLVideoElement): Promise<void>;
59
+ /**
60
+ * 启动条形码扫描
61
+ * @param videoElement HTML视频元素
62
+ */
63
+ startBarcodeScanner(videoElement: HTMLVideoElement): Promise<void>;
64
+ /**
65
+ * 启动身份证扫描
66
+ * @param videoElement HTML视频元素
67
+ */
68
+ startIDCardScanner(videoElement: HTMLVideoElement): Promise<void>;
69
+ /**
70
+ * 停止扫描
71
+ */
72
+ stop(): void;
73
+ /**
74
+ * 处理二维码扫描结果
75
+ */
76
+ private handleQRScan;
77
+ /**
78
+ * 处理条形码扫描结果
79
+ */
80
+ private handleBarcodeScan;
81
+ /**
82
+ * 处理身份证检测结果
83
+ */
84
+ private handleIDDetection;
85
+ /**
86
+ * 处理错误
87
+ */
88
+ private handleError;
89
+ /**
90
+ * 释放资源
91
+ */
92
+ terminate(): Promise<void>;
93
+ }
94
+ export { IDCardInfo } from './utils/types';
95
+ export { CameraOptions } from './utils/camera';
96
+ export { QRScanner, BarcodeScanner, IDCardDetector, OCRProcessor, DataExtractor, ImageProcessor };
package/dist/index.d.ts CHANGED
@@ -5,24 +5,12 @@
5
5
  * @version 1.0.0
6
6
  * @license MIT
7
7
  */
8
- import { QRScanner, QRScannerOptions } from './scanner/qr-scanner';
9
- import { BarcodeScanner, BarcodeScannerOptions } from './scanner/barcode-scanner';
10
- import { IDCardDetector } from './id-recognition/id-detector';
11
- import { OCRProcessor } from './id-recognition/ocr-processor';
12
- import { DataExtractor } from './id-recognition/data-extractor';
13
- import { Camera, CameraOptions } from './utils/camera';
14
- import { ImageProcessor } from './utils/image-processing';
15
- import { IDCardInfo, DetectionResult } from './utils/types';
8
+ import { CameraOptions } from './utils/camera';
9
+ import { IDCardInfo } from './utils/types';
10
+ import type { QRScannerOptions } from './scanner/qr-scanner';
11
+ import type { BarcodeScannerOptions } from './scanner/barcode-scanner';
16
12
  /**
17
13
  * IDScanner配置选项接口
18
- * @interface IDScannerOptions
19
- * @property {CameraOptions} [cameraOptions] - 相机配置选项
20
- * @property {QRScannerOptions} [qrScannerOptions] - 二维码扫描配置选项
21
- * @property {BarcodeScannerOptions} [barcodeScannerOptions] - 条形码扫描配置选项
22
- * @property {Function} [onQRCodeScanned] - 二维码识别成功回调
23
- * @property {Function} [onBarcodeScanned] - 条形码识别成功回调
24
- * @property {Function} [onIDCardScanned] - 身份证识别成功回调
25
- * @property {Function} [onError] - 错误处理回调
26
14
  */
27
15
  export interface IDScannerOptions {
28
16
  cameraOptions?: CameraOptions;
@@ -37,107 +25,54 @@ export interface IDScannerOptions {
37
25
  * IDScanner 主类
38
26
  *
39
27
  * 整合二维码、条形码扫描和身份证识别功能,提供统一的接口
40
- *
41
- * @example
42
- * ```typescript
43
- * // 创建扫描器实例
44
- * const scanner = new IDScanner({
45
- * onQRCodeScanned: (result) => {
46
- * console.log('扫描到二维码:', result);
47
- * },
48
- * onIDCardScanned: (info) => {
49
- * console.log('识别到身份证信息:', info);
50
- * }
51
- * });
52
- *
53
- * // 初始化OCR引擎和相关资源
54
- * await scanner.initialize();
55
- *
56
- * // 启动二维码扫描
57
- * const videoElement = document.getElementById('video');
58
- * await scanner.startQRScanner(videoElement);
59
- *
60
- * // 停止扫描
61
- * scanner.stop();
62
- *
63
- * // 使用结束后释放资源
64
- * scanner.terminate();
65
- * ```
28
+ * 使用动态导入实现按需加载
66
29
  */
67
30
  export declare class IDScanner {
68
31
  private options;
69
- private qrScanner;
70
- private barcodeScanner;
71
- private idDetector;
72
- private ocrProcessor;
32
+ private camera;
73
33
  private scanMode;
34
+ private videoElement;
35
+ private qrModule;
36
+ private ocrModule;
37
+ private isQRModuleLoaded;
38
+ private isOCRModuleLoaded;
74
39
  /**
75
- * 创建IDScanner实例
76
- * @param {IDScannerOptions} [options] - 配置选项
40
+ * 构造函数
41
+ * @param options 配置选项
77
42
  */
78
43
  constructor(options?: IDScannerOptions);
79
44
  /**
80
- * 初始化OCR引擎和相关资源
81
- *
82
- * @returns {Promise<void>} 初始化完成的Promise
83
- * @throws 如果初始化失败,将抛出错误
45
+ * 初始化模块
46
+ * 根据需要初始化OCR引擎
84
47
  */
85
48
  initialize(): Promise<void>;
86
49
  /**
87
50
  * 启动二维码扫描
88
- *
89
- * @param {HTMLVideoElement} videoElement - 用于显示相机画面的video元素
90
- * @returns {Promise<void>} 启动完成的Promise
51
+ * @param videoElement HTML视频元素
91
52
  */
92
53
  startQRScanner(videoElement: HTMLVideoElement): Promise<void>;
93
54
  /**
94
55
  * 启动条形码扫描
95
- *
96
- * @param {HTMLVideoElement} videoElement - 用于显示相机画面的video元素
97
- * @returns {Promise<void>} 启动完成的Promise
56
+ * @param videoElement HTML视频元素
98
57
  */
99
58
  startBarcodeScanner(videoElement: HTMLVideoElement): Promise<void>;
100
59
  /**
101
- * 启动身份证扫描识别
102
- *
103
- * @param {HTMLVideoElement} videoElement - 用于显示相机画面的video元素
104
- * @returns {Promise<void>} 启动完成的Promise
60
+ * 启动身份证扫描
61
+ * @param videoElement HTML视频元素
105
62
  */
106
63
  startIDCardScanner(videoElement: HTMLVideoElement): Promise<void>;
107
64
  /**
108
- * 停止当前扫描
65
+ * 停止扫描
109
66
  */
110
67
  stop(): void;
111
- /**
112
- * 处理二维码扫描结果
113
- * @private
114
- * @param {string} result - 扫描到的二维码内容
115
- */
116
- private handleQRScan;
117
- /**
118
- * 处理条形码扫描结果
119
- * @private
120
- * @param {string} result - 扫描到的条形码内容
121
- */
122
- private handleBarcodeScan;
123
- /**
124
- * 处理身份证检测结果
125
- * @private
126
- * @param {DetectionResult} result - 身份证检测结果
127
- */
128
- private handleIDDetection;
129
68
  /**
130
69
  * 处理错误
131
- * @private
132
- * @param {Error} error - 错误对象
133
70
  */
134
71
  private handleError;
135
72
  /**
136
- * 终止所有扫描并释放资源
137
- *
138
- * @returns {Promise<void>} 资源释放完成的Promise
73
+ * 释放资源
139
74
  */
140
75
  terminate(): Promise<void>;
141
76
  }
142
- export { Camera, QRScanner, BarcodeScanner, IDCardDetector, OCRProcessor, DataExtractor, ImageProcessor };
143
- export type { CameraOptions, QRScannerOptions, BarcodeScannerOptions, IDCardInfo, DetectionResult };
77
+ export { IDCardInfo } from './utils/types';
78
+ export { CameraOptions } from './utils/camera';
@@ -0,0 +1,67 @@
1
+ /**
2
+ * @file OCR模块入口文件
3
+ * @description 包含身份证OCR识别相关功能
4
+ * @module IDScannerOCR
5
+ * @version 1.0.0
6
+ * @license MIT
7
+ */
8
+ import { IDCardInfo } from './utils/types';
9
+ import { CameraOptions } from './utils/camera';
10
+ /**
11
+ * OCR模块配置选项
12
+ */
13
+ export interface OCRModuleOptions {
14
+ cameraOptions?: CameraOptions;
15
+ onIDCardScanned?: (info: IDCardInfo) => void;
16
+ onError?: (error: Error) => void;
17
+ }
18
+ /**
19
+ * OCR模块类
20
+ *
21
+ * 提供身份证检测和OCR文字识别功能
22
+ */
23
+ export declare class OCRModule {
24
+ private options;
25
+ private idDetector;
26
+ private ocrProcessor;
27
+ private dataExtractor;
28
+ private camera;
29
+ private isRunning;
30
+ private videoElement;
31
+ /**
32
+ * 构造函数
33
+ * @param options 配置选项
34
+ */
35
+ constructor(options?: OCRModuleOptions);
36
+ /**
37
+ * 初始化OCR引擎
38
+ *
39
+ * @returns Promise<void>
40
+ */
41
+ initialize(): Promise<void>;
42
+ /**
43
+ * 启动身份证扫描
44
+ * @param videoElement HTML视频元素
45
+ */
46
+ startIDCardScanner(videoElement: HTMLVideoElement): Promise<void>;
47
+ /**
48
+ * 停止扫描
49
+ */
50
+ stop(): void;
51
+ /**
52
+ * 处理身份证检测结果
53
+ */
54
+ private handleIDDetection;
55
+ /**
56
+ * 处理错误
57
+ */
58
+ private handleError;
59
+ /**
60
+ * 释放资源
61
+ */
62
+ terminate(): Promise<void>;
63
+ }
64
+ export { IDCardDetector } from './id-recognition/id-detector';
65
+ export { OCRProcessor } from './id-recognition/ocr-processor';
66
+ export { DataExtractor } from './id-recognition/data-extractor';
67
+ export { IDCardInfo } from './utils/types';
@@ -0,0 +1,68 @@
1
+ /**
2
+ * @file 二维码和条形码扫描模块
3
+ * @description 包含二维码和条形码扫描功能
4
+ * @module IDScannerQR
5
+ * @version 1.0.0
6
+ * @license MIT
7
+ */
8
+ import { QRScannerOptions } from './scanner/qr-scanner';
9
+ import { BarcodeScannerOptions } from './scanner/barcode-scanner';
10
+ import { CameraOptions } from './utils/camera';
11
+ /**
12
+ * 扫描模块配置选项
13
+ */
14
+ export interface ScannerModuleOptions {
15
+ cameraOptions?: CameraOptions;
16
+ qrScannerOptions?: QRScannerOptions;
17
+ barcodeScannerOptions?: BarcodeScannerOptions;
18
+ onQRCodeScanned?: (result: string) => void;
19
+ onBarcodeScanned?: (result: string) => void;
20
+ onError?: (error: Error) => void;
21
+ }
22
+ /**
23
+ * 扫描模块类
24
+ *
25
+ * 提供独立的二维码和条形码扫描功能
26
+ */
27
+ export declare class ScannerModule {
28
+ private options;
29
+ private qrScanner;
30
+ private barcodeScanner;
31
+ private camera;
32
+ private scanMode;
33
+ private videoElement;
34
+ /**
35
+ * 构造函数
36
+ * @param options 配置选项
37
+ */
38
+ constructor(options?: ScannerModuleOptions);
39
+ /**
40
+ * 启动二维码扫描
41
+ * @param videoElement HTML视频元素
42
+ */
43
+ startQRScanner(videoElement: HTMLVideoElement): Promise<void>;
44
+ /**
45
+ * 启动条形码扫描
46
+ * @param videoElement HTML视频元素
47
+ */
48
+ startBarcodeScanner(videoElement: HTMLVideoElement): Promise<void>;
49
+ /**
50
+ * 停止扫描
51
+ */
52
+ stop(): void;
53
+ /**
54
+ * 处理二维码扫描结果
55
+ */
56
+ private handleQRScan;
57
+ /**
58
+ * 处理条形码扫描结果
59
+ */
60
+ private handleBarcodeScan;
61
+ /**
62
+ * 处理错误
63
+ */
64
+ private handleError;
65
+ }
66
+ export { QRScanner, QRScannerOptions } from './scanner/qr-scanner';
67
+ export { BarcodeScanner, BarcodeScannerOptions } from './scanner/barcode-scanner';
68
+ export { Camera, CameraOptions } from './utils/camera';
@@ -0,0 +1,77 @@
1
+ /**
2
+ * @file 轻量级扫描库核心
3
+ * @description 不包含OCR功能的轻量版,只提供二维码和条形码扫描功能
4
+ * @module IDScannerCore
5
+ * @version 1.0.0
6
+ * @license MIT
7
+ */
8
+ import { QRScannerOptions } from './scanner/qr-scanner';
9
+ import { BarcodeScannerOptions } from './scanner/barcode-scanner';
10
+ import { CameraOptions } from './utils/camera';
11
+ /**
12
+ * IDScannerCore配置选项
13
+ */
14
+ export interface IDScannerCoreOptions {
15
+ cameraOptions?: CameraOptions;
16
+ qrScannerOptions?: QRScannerOptions;
17
+ barcodeScannerOptions?: BarcodeScannerOptions;
18
+ onQRCodeScanned?: (result: string) => void;
19
+ onBarcodeScanned?: (result: string) => void;
20
+ onError?: (error: Error) => void;
21
+ }
22
+ /**
23
+ * IDScannerCore 轻量级扫描类
24
+ *
25
+ * 提供二维码和条形码扫描功能,不包含OCR身份证识别功能
26
+ */
27
+ export declare class IDScannerCore {
28
+ private options;
29
+ private qrScanner;
30
+ private barcodeScanner;
31
+ private camera;
32
+ private scanMode;
33
+ private videoElement;
34
+ /**
35
+ * 构造函数
36
+ * @param options 配置选项
37
+ */
38
+ constructor(options?: IDScannerCoreOptions);
39
+ /**
40
+ * 初始化扫描器
41
+ */
42
+ initialize(): Promise<void>;
43
+ /**
44
+ * 启动二维码扫描
45
+ * @param videoElement HTML视频元素
46
+ */
47
+ startQRScanner(videoElement: HTMLVideoElement): Promise<void>;
48
+ /**
49
+ * 启动条形码扫描
50
+ * @param videoElement HTML视频元素
51
+ */
52
+ startBarcodeScanner(videoElement: HTMLVideoElement): Promise<void>;
53
+ /**
54
+ * 停止扫描
55
+ */
56
+ stop(): void;
57
+ /**
58
+ * 处理二维码扫描结果
59
+ */
60
+ private handleQRScan;
61
+ /**
62
+ * 处理条形码扫描结果
63
+ */
64
+ private handleBarcodeScan;
65
+ /**
66
+ * 处理错误
67
+ */
68
+ private handleError;
69
+ /**
70
+ * 释放资源
71
+ */
72
+ terminate(): Promise<void>;
73
+ }
74
+ export { QRScanner, QRScannerOptions } from './scanner/qr-scanner';
75
+ export { BarcodeScanner, BarcodeScannerOptions } from './scanner/barcode-scanner';
76
+ export { Camera, CameraOptions } from './utils/camera';
77
+ export { ImageProcessor } from './utils/image-processing';
@@ -0,0 +1,14 @@
1
+ export declare class IDScannerDemo {
2
+ private scanner;
3
+ private videoElement;
4
+ private resultContainer;
5
+ private switchButton;
6
+ constructor(videoElementId: string, resultContainerId: string, switchButtonId: string);
7
+ initialize(): Promise<void>;
8
+ private currentMode;
9
+ private toggleScanMode;
10
+ private handleQRCodeResult;
11
+ private handleIDCardResult;
12
+ private handleError;
13
+ stop(): void;
14
+ }
@@ -0,0 +1,105 @@
1
+ /**
2
+ * @file 数据提取工具类
3
+ * @description 提供身份证信息的验证和格式化功能
4
+ * @module DataExtractor
5
+ */
6
+ import { IDCardInfo } from '../utils/types';
7
+ /**
8
+ * 数据提取工具类
9
+ *
10
+ * 提供身份证信息的验证、提取和增强功能,可以从身份证号码中提取出生日期、性别等信息,
11
+ * 并对OCR识别结果进行补充和验证
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * // 验证身份证号码
16
+ * const isValid = DataExtractor.validateIDNumber('110101199001011234');
17
+ *
18
+ * // 从身份证号码提取出生日期
19
+ * const birthDate = DataExtractor.extractBirthDateFromID('110101199001011234');
20
+ * // 结果: '1990-01-01'
21
+ *
22
+ * // 增强OCR识别结果
23
+ * const enhancedInfo = DataExtractor.enhanceIDCardInfo({
24
+ * name: '张三',
25
+ * idNumber: '110101199001011234'
26
+ * });
27
+ * // 结果会自动补充性别和出生日期
28
+ * ```
29
+ */
30
+ export declare class DataExtractor {
31
+ /**
32
+ * 验证身份证号码格式
33
+ *
34
+ * 检查身份证号码的长度、格式和出生日期部分是否有效
35
+ *
36
+ * @param {string} idNumber - 要验证的身份证号码
37
+ * @returns {boolean} 是否是有效的身份证号码
38
+ */
39
+ static validateIDNumber(idNumber: string): boolean;
40
+ /**
41
+ * 从身份证号码提取出生日期
42
+ *
43
+ * @param {string} idNumber - 身份证号码
44
+ * @returns {string|null} 格式化的出生日期(YYYY-MM-DD),如果身份证号码无效则返回null
45
+ */
46
+ static extractBirthDateFromID(idNumber: string): string | null;
47
+ /**
48
+ * 从身份证号码提取性别
49
+ *
50
+ * 根据身份证号码第17位判断性别,奇数为男,偶数为女
51
+ *
52
+ * @param {string} idNumber - 身份证号码
53
+ * @returns {string|null} '男'或'女',如果身份证号码无效则返回null
54
+ */
55
+ static extractGenderFromID(idNumber: string): string | null;
56
+ /**
57
+ * 从身份证号码提取地区编码
58
+ *
59
+ * @param {string} idNumber - 身份证号码
60
+ * @returns {string|null} 地区编码(前6位),如果身份证号码无效则返回null
61
+ */
62
+ static extractRegionFromID(idNumber: string): string | null;
63
+ /**
64
+ * 合并并优化身份证信息
65
+ *
66
+ * 使用多个来源的数据进行交叉验证和补充,如果OCR识别结果缺少某些信息,
67
+ * 但有身份证号码,则可以从号码中提取出生日期和性别等信息
68
+ *
69
+ * @param {IDCardInfo} ocrInfo - OCR识别到的身份证信息
70
+ * @param {string} [idNumber] - 可选的外部提供的身份证号码,优先级高于OCR识别结果
71
+ * @returns {IDCardInfo} 增强后的身份证信息
72
+ */
73
+ static enhanceIDCardInfo(ocrInfo: IDCardInfo, idNumber?: string): IDCardInfo;
74
+ /**
75
+ * 提取并验证身份证信息
76
+ *
77
+ * @param idCardInfo 初步提取的身份证信息
78
+ * @returns 验证和增强后的身份证信息
79
+ */
80
+ extractAndValidate(idCardInfo: IDCardInfo): IDCardInfo;
81
+ /**
82
+ * 规范化身份证号码
83
+ */
84
+ private normalizeIDNumber;
85
+ /**
86
+ * 验证身份证号码是否有效
87
+ */
88
+ private validateIDNumber;
89
+ /**
90
+ * 从身份证号中提取出生日期
91
+ */
92
+ private extractBirthDateFromID;
93
+ /**
94
+ * 从身份证号中提取性别信息
95
+ */
96
+ private extractGenderFromID;
97
+ /**
98
+ * 规范化日期格式
99
+ */
100
+ private normalizeDate;
101
+ /**
102
+ * 规范化地址信息
103
+ */
104
+ private normalizeAddress;
105
+ }
@@ -0,0 +1,100 @@
1
+ /**
2
+ * @file 身份证检测模块
3
+ * @description 提供自动检测和定位图像中的身份证功能
4
+ * @module IDCardDetector
5
+ */
6
+ /**
7
+ * DetectionResult接口
8
+ *
9
+ * 包含身份证检测结果信息
10
+ */
11
+ export interface DetectionResult {
12
+ success: boolean;
13
+ imageData?: ImageData;
14
+ boundingBox?: {
15
+ x: number;
16
+ y: number;
17
+ width: number;
18
+ height: number;
19
+ };
20
+ confidence?: number;
21
+ message?: string;
22
+ }
23
+ /**
24
+ * IDCardDetector配置选项
25
+ */
26
+ export interface IDCardDetectorOptions {
27
+ onDetection?: (result: DetectionResult) => void;
28
+ onError?: (error: Error) => void;
29
+ detectionInterval?: number;
30
+ }
31
+ /**
32
+ * 身份证检测器类
33
+ *
34
+ * 通过图像处理和计算机视觉技术,实时检测视频流中的身份证,并提取身份证区域
35
+ * 注意:当前实现是简化版,实际项目中建议使用OpenCV.js进行更精确的检测
36
+ *
37
+ * @example
38
+ * ```typescript
39
+ * // 创建身份证检测器
40
+ * const detector = new IDCardDetector((result) => {
41
+ * if (result.success && result.croppedImage) {
42
+ * console.log('检测到身份证!');
43
+ * // 对裁剪出的身份证图像进行处理
44
+ * processIDCardImage(result.croppedImage);
45
+ * }
46
+ * });
47
+ *
48
+ * // 启动检测
49
+ * const videoElement = document.getElementById('video') as HTMLVideoElement;
50
+ * await detector.start(videoElement);
51
+ *
52
+ * // 停止检测
53
+ * detector.stop();
54
+ * ```
55
+ */
56
+ export declare class IDCardDetector {
57
+ private onDetected?;
58
+ private camera;
59
+ private detecting;
60
+ private detectTimer;
61
+ /**
62
+ * 创建身份证检测器实例
63
+ *
64
+ * @param {Function} [onDetected] - 身份证检测成功回调函数,接收检测结果对象
65
+ */
66
+ constructor(onDetected?: ((result: DetectionResult) => void) | undefined);
67
+ /**
68
+ * 启动身份证检测
69
+ *
70
+ * 初始化相机并开始连续检测视频帧中的身份证
71
+ *
72
+ * @param {HTMLVideoElement} videoElement - 用于显示相机画面的video元素
73
+ * @returns {Promise<void>} 启动完成的Promise
74
+ */
75
+ start(videoElement: HTMLVideoElement): Promise<void>;
76
+ /**
77
+ * 执行一次身份证检测
78
+ *
79
+ * 内部方法,捕获当前视频帧并尝试检测其中的身份证
80
+ *
81
+ * @private
82
+ */
83
+ private detect;
84
+ /**
85
+ * 身份证检测核心算法
86
+ *
87
+ * 通过图像处理技术检测和提取图像中的身份证区域
88
+ *
89
+ * @private
90
+ * @param {ImageData} imageData - 需要检测身份证的图像数据
91
+ * @returns {Promise<DetectionResult>} 检测结果,包含成功标志和裁剪后的身份证图像
92
+ */
93
+ private detectIDCard;
94
+ /**
95
+ * 停止身份证检测
96
+ *
97
+ * 停止检测循环并释放相机资源
98
+ */
99
+ stop(): void;
100
+ }