id-scanner-lib 1.2.2 → 1.3.2

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 (67) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +375 -363
  3. package/dist/id-scanner-core.esm.js +427 -221
  4. package/dist/id-scanner-core.esm.js.map +1 -1
  5. package/dist/id-scanner-core.js +427 -221
  6. package/dist/id-scanner-core.js.map +1 -1
  7. package/dist/id-scanner-core.min.js +1 -9
  8. package/dist/id-scanner-core.min.js.map +1 -1
  9. package/dist/id-scanner-ocr.esm.js +451 -276
  10. package/dist/id-scanner-ocr.esm.js.map +1 -1
  11. package/dist/id-scanner-ocr.js +451 -276
  12. package/dist/id-scanner-ocr.js.map +1 -1
  13. package/dist/id-scanner-ocr.min.js +1 -9
  14. package/dist/id-scanner-ocr.min.js.map +1 -1
  15. package/dist/id-scanner-qr.esm.js +483 -233
  16. package/dist/id-scanner-qr.esm.js.map +1 -1
  17. package/dist/id-scanner-qr.js +482 -232
  18. package/dist/id-scanner-qr.js.map +1 -1
  19. package/dist/id-scanner-qr.min.js +1 -9
  20. package/dist/id-scanner-qr.min.js.map +1 -1
  21. package/dist/id-scanner.js +2138 -358
  22. package/dist/id-scanner.js.map +1 -1
  23. package/dist/id-scanner.min.js +1 -9
  24. package/dist/id-scanner.min.js.map +1 -1
  25. package/package.json +27 -7
  26. package/src/demo/demo.ts +178 -62
  27. package/src/id-recognition/anti-fake-detector.ts +317 -0
  28. package/src/id-recognition/id-detector.ts +184 -155
  29. package/src/id-recognition/ocr-processor.ts +193 -146
  30. package/src/id-recognition/ocr-worker.ts +82 -72
  31. package/src/index-umd.ts +347 -110
  32. package/src/index.ts +866 -91
  33. package/src/ocr-module.ts +108 -60
  34. package/src/qr-module.ts +104 -54
  35. package/src/scanner/barcode-scanner.ts +145 -58
  36. package/src/scanner/qr-scanner.ts +86 -47
  37. package/src/utils/image-processing.ts +479 -294
  38. package/dist/core.d.ts +0 -77
  39. package/dist/demo/demo.d.ts +0 -14
  40. package/dist/id-recognition/data-extractor.d.ts +0 -105
  41. package/dist/id-recognition/id-detector.d.ts +0 -100
  42. package/dist/id-recognition/ocr-processor.d.ts +0 -64
  43. package/dist/id-scanner.esm.js +0 -94656
  44. package/dist/id-scanner.esm.js.map +0 -1
  45. package/dist/index-umd.d.ts +0 -96
  46. package/dist/index.d.ts +0 -78
  47. package/dist/ocr-module.d.ts +0 -67
  48. package/dist/qr-module.d.ts +0 -68
  49. package/dist/scanner/barcode-scanner.d.ts +0 -90
  50. package/dist/scanner/qr-scanner.d.ts +0 -80
  51. package/dist/types/core.d.ts +0 -77
  52. package/dist/types/demo/demo.d.ts +0 -14
  53. package/dist/types/id-recognition/data-extractor.d.ts +0 -105
  54. package/dist/types/id-recognition/id-detector.d.ts +0 -100
  55. package/dist/types/id-recognition/ocr-processor.d.ts +0 -64
  56. package/dist/types/index-umd.d.ts +0 -96
  57. package/dist/types/index.d.ts +0 -78
  58. package/dist/types/ocr-module.d.ts +0 -67
  59. package/dist/types/qr-module.d.ts +0 -68
  60. package/dist/types/scanner/barcode-scanner.d.ts +0 -90
  61. package/dist/types/scanner/qr-scanner.d.ts +0 -80
  62. package/dist/types/utils/camera.d.ts +0 -81
  63. package/dist/types/utils/image-processing.d.ts +0 -75
  64. package/dist/types/utils/types.d.ts +0 -65
  65. package/dist/utils/camera.d.ts +0 -81
  66. package/dist/utils/image-processing.d.ts +0 -75
  67. package/dist/utils/types.d.ts +0 -65
package/src/ocr-module.ts CHANGED
@@ -6,134 +6,182 @@
6
6
  * @license MIT
7
7
  */
8
8
 
9
- import { Camera, CameraOptions } from './utils/camera';
10
- import { ImageProcessor } from './utils/image-processing';
11
- import { IDCardInfo, DetectionResult } from './utils/types';
12
- import { IDCardDetector, IDCardDetectorOptions } from './id-recognition/id-detector';
13
- import { OCRProcessor } from './id-recognition/ocr-processor';
14
- import { DataExtractor } from './id-recognition/data-extractor';
9
+ import { Camera, CameraOptions } from "./utils/camera"
10
+ import { ImageProcessor } from "./utils/image-processing"
11
+ import { IDCardInfo, DetectionResult } from "./utils/types"
12
+ import {
13
+ IDCardDetector,
14
+ IDCardDetectorOptions,
15
+ } from "./id-recognition/id-detector"
16
+ import { OCRProcessor } from "./id-recognition/ocr-processor"
17
+ import { DataExtractor } from "./id-recognition/data-extractor"
15
18
 
16
19
  /**
17
20
  * OCR模块配置选项
18
21
  */
19
22
  export interface OCRModuleOptions {
20
- cameraOptions?: CameraOptions;
21
- onIDCardScanned?: (info: IDCardInfo) => void;
22
- onError?: (error: Error) => void;
23
+ cameraOptions?: CameraOptions
24
+ onIDCardScanned?: (info: IDCardInfo) => void
25
+ onError?: (error: Error) => void
23
26
  }
24
27
 
25
28
  /**
26
29
  * OCR模块类
27
- *
30
+ *
28
31
  * 提供身份证检测和OCR文字识别功能
29
32
  */
30
33
  export class OCRModule {
31
- private idDetector: IDCardDetector;
32
- private ocrProcessor: OCRProcessor;
33
- private dataExtractor: DataExtractor;
34
- private camera: Camera;
35
- private isRunning: boolean = false;
36
- private videoElement: HTMLVideoElement | null = null;
37
-
34
+ private idDetector: IDCardDetector
35
+ private ocrProcessor: OCRProcessor
36
+ private dataExtractor: DataExtractor
37
+ private camera: Camera
38
+ private isRunning: boolean = false
39
+ private videoElement: HTMLVideoElement | null = null
40
+
38
41
  /**
39
42
  * 构造函数
40
43
  * @param options 配置选项
41
44
  */
42
45
  constructor(private options: OCRModuleOptions = {}) {
43
- this.camera = new Camera(options.cameraOptions);
46
+ this.camera = new Camera(options.cameraOptions)
44
47
  this.idDetector = new IDCardDetector({
45
48
  onDetection: this.handleIDDetection.bind(this),
46
- onError: this.handleError.bind(this)
47
- } as IDCardDetectorOptions);
48
- this.ocrProcessor = new OCRProcessor();
49
- this.dataExtractor = new DataExtractor();
49
+ onError: this.handleError.bind(this),
50
+ } as IDCardDetectorOptions)
51
+ this.ocrProcessor = new OCRProcessor()
52
+ this.dataExtractor = new DataExtractor()
50
53
  }
51
-
54
+
52
55
  /**
53
56
  * 初始化OCR引擎
54
- *
57
+ *
55
58
  * @returns Promise<void>
56
59
  */
57
60
  async initialize(): Promise<void> {
58
61
  try {
59
- await this.ocrProcessor.initialize();
60
- console.log('OCR engine initialized');
62
+ await this.ocrProcessor.initialize()
63
+ console.log("OCR engine initialized")
61
64
  } catch (error) {
62
- this.handleError(error as Error);
63
- throw error;
65
+ this.handleError(error as Error)
66
+ throw error
64
67
  }
65
68
  }
66
-
69
+
67
70
  /**
68
71
  * 启动身份证扫描
69
72
  * @param videoElement HTML视频元素
70
73
  */
71
74
  async startIDCardScanner(videoElement: HTMLVideoElement): Promise<void> {
72
75
  if (!this.ocrProcessor) {
73
- throw new Error('OCR engine not initialized. Call initialize() first.');
76
+ throw new Error("OCR engine not initialized. Call initialize() first.")
74
77
  }
75
-
76
- this.videoElement = videoElement;
77
- this.isRunning = true;
78
- await this.camera.start(videoElement);
79
- this.idDetector.start(videoElement);
78
+
79
+ this.videoElement = videoElement
80
+ this.isRunning = true
81
+ await this.camera.start(videoElement)
82
+ this.idDetector.start(videoElement)
80
83
  }
81
-
84
+
82
85
  /**
83
86
  * 停止扫描
84
87
  */
85
88
  stop(): void {
86
- this.isRunning = false;
87
- this.idDetector.stop();
88
- this.camera.stop();
89
+ this.isRunning = false
90
+ this.idDetector.stop()
91
+ this.camera.stop()
89
92
  }
90
-
93
+
91
94
  /**
92
95
  * 处理身份证检测结果
93
96
  */
94
97
  private async handleIDDetection(result: DetectionResult): Promise<void> {
95
- if (!this.isRunning) return;
96
-
98
+ if (!this.isRunning) return
99
+
97
100
  try {
98
101
  // 检查 imageData 是否存在
99
102
  if (!result.imageData) {
100
- this.handleError(new Error('无效的图像数据'));
101
- return;
103
+ this.handleError(new Error("无效的图像数据"))
104
+ return
102
105
  }
103
-
104
- const idCardInfo = await this.ocrProcessor.processIDCard(result.imageData);
105
- const extractedInfo = this.dataExtractor.extractAndValidate(idCardInfo);
106
-
106
+
107
+ const idCardInfo = await this.ocrProcessor.processIDCard(result.imageData)
108
+ const extractedInfo = this.dataExtractor.extractAndValidate(idCardInfo)
109
+
107
110
  if (this.options.onIDCardScanned) {
108
- this.options.onIDCardScanned(extractedInfo);
111
+ this.options.onIDCardScanned(extractedInfo)
109
112
  }
110
113
  } catch (error) {
111
- this.handleError(error as Error);
114
+ this.handleError(error as Error)
112
115
  }
113
116
  }
114
-
117
+
115
118
  /**
116
119
  * 处理错误
117
120
  */
118
121
  private handleError(error: Error): void {
119
122
  if (this.options.onError) {
120
- this.options.onError(error);
123
+ this.options.onError(error)
121
124
  } else {
122
- console.error('OCRModule error:', error);
125
+ console.error("OCRModule error:", error)
123
126
  }
124
127
  }
125
-
128
+
126
129
  /**
127
130
  * 释放资源
128
131
  */
129
132
  async terminate(): Promise<void> {
130
- this.stop();
131
- await this.ocrProcessor.terminate();
133
+ this.stop()
134
+ await this.ocrProcessor.terminate()
135
+ }
136
+
137
+ /**
138
+ * 直接处理图像数据中的身份证
139
+ * @param imageData 要处理的图像数据
140
+ * @returns 返回Promise,解析为身份证信息
141
+ */
142
+ async processIDCard(imageData: ImageData): Promise<IDCardInfo> {
143
+ try {
144
+ if (!this.ocrProcessor) {
145
+ throw new Error("OCR engine not initialized. Call initialize() first.")
146
+ }
147
+
148
+ // 检查图像数据有效性
149
+ if (
150
+ !imageData ||
151
+ !imageData.data ||
152
+ imageData.width <= 0 ||
153
+ imageData.height <= 0
154
+ ) {
155
+ throw new Error("无效的图像数据")
156
+ }
157
+
158
+ // 进行图像预处理,提高识别率
159
+ const processedImage = ImageProcessor.adjustBrightnessContrast(
160
+ imageData,
161
+ 5, // 轻微提高亮度
162
+ 10 // 适度提高对比度
163
+ )
164
+
165
+ // 调用OCR处理器进行文字识别
166
+ const idCardInfo = await this.ocrProcessor.processIDCard(processedImage)
167
+ // 提取和验证身份证信息
168
+ const extractedInfo = this.dataExtractor.extractAndValidate(idCardInfo)
169
+
170
+ // 如果有回调,触发回调
171
+ if (this.options.onIDCardScanned) {
172
+ this.options.onIDCardScanned(extractedInfo)
173
+ }
174
+
175
+ return extractedInfo
176
+ } catch (error) {
177
+ this.handleError(error as Error)
178
+ throw error
179
+ }
132
180
  }
133
181
  }
134
182
 
135
183
  // 导出相关类型和工具
136
- export { IDCardDetector } from './id-recognition/id-detector';
137
- export { OCRProcessor } from './id-recognition/ocr-processor';
138
- export { DataExtractor } from './id-recognition/data-extractor';
139
- export { IDCardInfo } from './utils/types';
184
+ export { IDCardDetector } from "./id-recognition/id-detector"
185
+ export { OCRProcessor } from "./id-recognition/ocr-processor"
186
+ export { DataExtractor } from "./id-recognition/data-extractor"
187
+ export { IDCardInfo } from "./utils/types"
package/src/qr-module.ts CHANGED
@@ -6,124 +6,174 @@
6
6
  * @license MIT
7
7
  */
8
8
 
9
- import { QRScanner, QRScannerOptions } from './scanner/qr-scanner';
10
- import { BarcodeScanner, BarcodeScannerOptions } from './scanner/barcode-scanner';
11
- import { Camera, CameraOptions } from './utils/camera';
9
+ import { QRScanner, QRScannerOptions } from "./scanner/qr-scanner"
10
+ import {
11
+ BarcodeScanner,
12
+ BarcodeScannerOptions,
13
+ } from "./scanner/barcode-scanner"
14
+ import { Camera, CameraOptions } from "./utils/camera"
12
15
 
13
16
  /**
14
17
  * 扫描模块配置选项
15
18
  */
16
19
  export interface ScannerModuleOptions {
17
- cameraOptions?: CameraOptions;
18
- qrScannerOptions?: QRScannerOptions;
19
- barcodeScannerOptions?: BarcodeScannerOptions;
20
- onQRCodeScanned?: (result: string) => void;
21
- onBarcodeScanned?: (result: string) => void;
22
- onError?: (error: Error) => void;
20
+ cameraOptions?: CameraOptions
21
+ qrScannerOptions?: QRScannerOptions
22
+ barcodeScannerOptions?: BarcodeScannerOptions
23
+ onQRCodeScanned?: (result: string) => void
24
+ onBarcodeScanned?: (result: string) => void
25
+ onError?: (error: Error) => void
23
26
  }
24
27
 
25
28
  /**
26
29
  * 扫描模块类
27
- *
30
+ *
28
31
  * 提供独立的二维码和条形码扫描功能
29
32
  */
30
33
  export class ScannerModule {
31
- private qrScanner: QRScanner;
32
- private barcodeScanner: BarcodeScanner;
33
- private camera: Camera;
34
- private scanMode: 'qr' | 'barcode' | null = null;
35
- private videoElement: HTMLVideoElement | null = null;
36
-
34
+ private qrScanner: QRScanner
35
+ private barcodeScanner: BarcodeScanner
36
+ private camera: Camera
37
+ private scanMode: "qr" | "barcode" | null = null
38
+ private videoElement: HTMLVideoElement | null = null
39
+
37
40
  /**
38
41
  * 构造函数
39
42
  * @param options 配置选项
40
43
  */
41
44
  constructor(private options: ScannerModuleOptions = {}) {
42
- this.camera = new Camera(options.cameraOptions);
45
+ this.camera = new Camera(options.cameraOptions)
43
46
  this.qrScanner = new QRScanner({
44
47
  ...options.qrScannerOptions,
45
- onScan: this.handleQRScan.bind(this)
46
- });
48
+ onScan: this.handleQRScan.bind(this),
49
+ })
47
50
  this.barcodeScanner = new BarcodeScanner({
48
51
  ...options.barcodeScannerOptions,
49
- onScan: this.handleBarcodeScan.bind(this)
50
- });
52
+ onScan: this.handleBarcodeScan.bind(this),
53
+ })
51
54
  }
52
-
55
+
53
56
  /**
54
57
  * 启动二维码扫描
55
58
  * @param videoElement HTML视频元素
56
59
  */
57
60
  async startQRScanner(videoElement: HTMLVideoElement): Promise<void> {
58
- this.stop(); // 确保先停止可能正在运行的扫描
59
-
60
- this.videoElement = videoElement;
61
- this.scanMode = 'qr';
62
- await this.camera.start(videoElement);
63
- this.qrScanner.start(videoElement);
61
+ this.stop() // 确保先停止可能正在运行的扫描
62
+
63
+ this.videoElement = videoElement
64
+ this.scanMode = "qr"
65
+ await this.camera.start(videoElement)
66
+ this.qrScanner.start(videoElement)
64
67
  }
65
-
68
+
66
69
  /**
67
70
  * 启动条形码扫描
68
71
  * @param videoElement HTML视频元素
69
72
  */
70
73
  async startBarcodeScanner(videoElement: HTMLVideoElement): Promise<void> {
71
- this.stop(); // 确保先停止可能正在运行的扫描
72
-
73
- this.videoElement = videoElement;
74
- this.scanMode = 'barcode';
75
- await this.camera.start(videoElement);
76
- this.barcodeScanner.start(videoElement);
74
+ this.stop() // 确保先停止可能正在运行的扫描
75
+
76
+ this.videoElement = videoElement
77
+ this.scanMode = "barcode"
78
+ await this.camera.start(videoElement)
79
+ this.barcodeScanner.start(videoElement)
77
80
  }
78
-
81
+
79
82
  /**
80
83
  * 停止扫描
81
84
  */
82
85
  stop(): void {
83
- if (this.scanMode === 'qr') {
84
- this.qrScanner.stop();
85
- } else if (this.scanMode === 'barcode') {
86
- this.barcodeScanner.stop();
86
+ if (this.scanMode === "qr") {
87
+ this.qrScanner.stop()
88
+ } else if (this.scanMode === "barcode") {
89
+ this.barcodeScanner.stop()
87
90
  }
88
-
91
+
89
92
  if (this.videoElement) {
90
- this.camera.stop();
93
+ this.camera.stop()
91
94
  }
92
-
93
- this.scanMode = null;
95
+
96
+ this.scanMode = null
94
97
  }
95
-
98
+
96
99
  /**
97
100
  * 处理二维码扫描结果
98
101
  */
99
102
  private handleQRScan(result: string): void {
100
103
  if (this.options.onQRCodeScanned) {
101
- this.options.onQRCodeScanned(result);
104
+ this.options.onQRCodeScanned(result)
102
105
  }
103
106
  }
104
-
107
+
105
108
  /**
106
109
  * 处理条形码扫描结果
107
110
  */
108
111
  private handleBarcodeScan(result: string): void {
109
112
  if (this.options.onBarcodeScanned) {
110
- this.options.onBarcodeScanned(result);
113
+ this.options.onBarcodeScanned(result)
111
114
  }
112
115
  }
113
-
116
+
114
117
  /**
115
118
  * 处理错误
116
119
  */
117
120
  private handleError(error: Error): void {
118
121
  if (this.options.onError) {
119
- this.options.onError(error);
122
+ this.options.onError(error)
120
123
  } else {
121
- console.error('ScannerModule error:', error);
124
+ console.error("ScannerModule error:", error)
125
+ }
126
+ }
127
+
128
+ /**
129
+ * 处理图像数据中的二维码
130
+ * @param imageData 要处理的图像数据
131
+ * @returns 返回Promise,解析为扫描结果
132
+ */
133
+ async processQRCodeImage(imageData: ImageData): Promise<string> {
134
+ try {
135
+ const result = this.qrScanner.processImageData(imageData)
136
+ if (result) {
137
+ // 如果需要,触发回调
138
+ if (this.options.onQRCodeScanned) {
139
+ this.options.onQRCodeScanned(result)
140
+ }
141
+ return result
142
+ }
143
+ throw new Error("未检测到二维码")
144
+ } catch (error) {
145
+ this.handleError(error as Error)
146
+ throw error
147
+ }
148
+ }
149
+
150
+ /**
151
+ * 处理图像数据中的条形码
152
+ * @param imageData 要处理的图像数据
153
+ * @returns 返回Promise,解析为扫描结果
154
+ */
155
+ async processBarcodeImage(imageData: ImageData): Promise<string> {
156
+ try {
157
+ const result = this.barcodeScanner.processImageData(imageData)
158
+ if (result) {
159
+ // 如果需要,触发回调
160
+ if (this.options.onBarcodeScanned) {
161
+ this.options.onBarcodeScanned(result)
162
+ }
163
+ return result
164
+ }
165
+ throw new Error("未检测到条形码")
166
+ } catch (error) {
167
+ this.handleError(error as Error)
168
+ throw error
122
169
  }
123
170
  }
124
171
  }
125
172
 
126
173
  // 导出相关类型和工具
127
- export { QRScanner, QRScannerOptions } from './scanner/qr-scanner';
128
- export { BarcodeScanner, BarcodeScannerOptions } from './scanner/barcode-scanner';
129
- export { Camera, CameraOptions } from './utils/camera';
174
+ export { QRScanner, QRScannerOptions } from "./scanner/qr-scanner"
175
+ export {
176
+ BarcodeScanner,
177
+ BarcodeScannerOptions,
178
+ } from "./scanner/barcode-scanner"
179
+ export { Camera, CameraOptions } from "./utils/camera"