id-scanner-lib 1.3.3 → 1.6.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 (80) hide show
  1. package/README.md +324 -410
  2. package/dist/id-scanner-lib.esm.js +4826 -0
  3. package/dist/id-scanner-lib.esm.js.map +1 -0
  4. package/dist/id-scanner-lib.js +4858 -0
  5. package/dist/id-scanner-lib.js.map +1 -0
  6. package/dist/types/browser-image-compression.d.ts +19 -0
  7. package/dist/types/tesseract.d.ts +280 -0
  8. package/package.json +89 -78
  9. package/src/core/base-module.ts +78 -0
  10. package/src/core/camera-manager.ts +813 -0
  11. package/src/core/config.ts +305 -0
  12. package/src/core/errors.ts +174 -0
  13. package/src/core/event-emitter.test.ts +42 -0
  14. package/src/core/event-emitter.ts +110 -0
  15. package/src/core/loading-state.test.ts +67 -0
  16. package/src/core/loading-state.ts +156 -0
  17. package/src/core/logger.test.ts +49 -0
  18. package/src/core/logger.ts +549 -0
  19. package/src/core/module-manager.ts +163 -0
  20. package/src/core/plugin-manager.ts +429 -0
  21. package/src/core/resource-manager.ts +762 -0
  22. package/src/core/result.ts +163 -0
  23. package/src/core/scanner-factory.ts +236 -0
  24. package/src/index.ts +117 -939
  25. package/src/interfaces/external-types.ts +200 -0
  26. package/src/interfaces/face-detection.ts +309 -0
  27. package/src/interfaces/scanner-module.ts +384 -0
  28. package/src/modules/face/face-detector.ts +988 -0
  29. package/src/modules/face/index.ts +208 -0
  30. package/src/modules/face/liveness-detector.ts +908 -0
  31. package/src/modules/face/types.ts +133 -0
  32. package/src/{id-recognition → modules/id-card}/anti-fake-detector.ts +274 -240
  33. package/src/modules/id-card/id-card-detector.ts +474 -0
  34. package/src/modules/id-card/index.ts +425 -0
  35. package/src/{id-recognition → modules/id-card}/ocr-processor.ts +149 -92
  36. package/src/modules/id-card/ocr-worker.ts +259 -0
  37. package/src/modules/id-card/types.ts +178 -0
  38. package/src/modules/qrcode/index.ts +175 -0
  39. package/src/modules/qrcode/qr-code-scanner.ts +231 -0
  40. package/src/modules/qrcode/types.ts +169 -0
  41. package/src/types/common.test.ts +99 -0
  42. package/src/types/common.ts +166 -0
  43. package/src/types/tesseract.d.ts +265 -22
  44. package/src/utils/camera.test.ts +30 -0
  45. package/src/utils/camera.ts +4 -1
  46. package/src/utils/error-handler.test.ts +137 -0
  47. package/src/utils/error-handler.ts +110 -0
  48. package/src/utils/image-processing.ts +68 -49
  49. package/src/utils/index.test.ts +186 -0
  50. package/src/utils/index.ts +429 -0
  51. package/src/utils/performance.ts +168 -131
  52. package/src/utils/resource-manager.ts +65 -146
  53. package/src/utils/retry.test.ts +142 -0
  54. package/src/utils/retry.ts +282 -0
  55. package/src/utils/types.ts +90 -2
  56. package/src/utils/utils.test.ts +171 -0
  57. package/src/utils/worker.ts +123 -84
  58. package/src/version.ts +11 -0
  59. package/tools/scaffold.js +543 -0
  60. package/dist/id-scanner-core.esm.js +0 -11349
  61. package/dist/id-scanner-core.js +0 -11361
  62. package/dist/id-scanner-core.min.js +0 -1
  63. package/dist/id-scanner-ocr.esm.js +0 -2319
  64. package/dist/id-scanner-ocr.js +0 -2328
  65. package/dist/id-scanner-ocr.min.js +0 -1
  66. package/dist/id-scanner-qr.esm.js +0 -1296
  67. package/dist/id-scanner-qr.js +0 -1305
  68. package/dist/id-scanner-qr.min.js +0 -1
  69. package/dist/id-scanner.js +0 -4561
  70. package/dist/id-scanner.min.js +0 -1
  71. package/src/core.ts +0 -138
  72. package/src/demo/demo.ts +0 -204
  73. package/src/id-recognition/data-extractor.ts +0 -262
  74. package/src/id-recognition/id-detector.ts +0 -510
  75. package/src/id-recognition/ocr-worker.ts +0 -156
  76. package/src/index-umd.ts +0 -477
  77. package/src/ocr-module.ts +0 -187
  78. package/src/qr-module.ts +0 -179
  79. package/src/scanner/barcode-scanner.ts +0 -251
  80. package/src/scanner/qr-scanner.ts +0 -167
@@ -0,0 +1,208 @@
1
+ /**
2
+ * @file 人脸模块入口
3
+ * @description 提供人脸检测、活体检测和人脸比对功能的模块入口
4
+ * @module modules/face
5
+ */
6
+
7
+ import { BaseModule } from '../../core/base-module';
8
+ import { FaceDetectionResult, FaceComparisonResult, FaceModuleOptions } from './types';
9
+
10
+ /**
11
+ * 人脸模块
12
+ * 提供人脸检测、活体检测和人脸比对功能
13
+ */
14
+ export class FaceModule extends BaseModule {
15
+ /** 模块名称 */
16
+ public readonly name: string = 'face';
17
+
18
+ /** 模块配置 */
19
+ private options: FaceModuleOptions;
20
+
21
+ /** 最后一次检测结果 */
22
+ private lastDetectionResult?: FaceDetectionResult;
23
+
24
+ /**
25
+ * 构造函数
26
+ * @param options 模块配置选项
27
+ */
28
+ constructor(options: FaceModuleOptions = {}) {
29
+ super();
30
+
31
+ this.options = {
32
+ enabled: true,
33
+ detector: {
34
+ minConfidence: 0.7,
35
+ detectLandmarks: true,
36
+ detectAttributes: true,
37
+ returnFaceImage: false,
38
+ ...options.detector
39
+ },
40
+ liveness: {
41
+ enabled: false,
42
+ type: 'passive',
43
+ minConfidence: 0.8,
44
+ timeout: 10000,
45
+ ...options.liveness
46
+ },
47
+ comparison: {
48
+ minSimilarity: 0.8,
49
+ ...options.comparison
50
+ },
51
+ ...options
52
+ };
53
+ }
54
+
55
+ /**
56
+ * 初始化模块
57
+ */
58
+ public async initialize(): Promise<void> {
59
+ if (this._isInitialized) {
60
+ return;
61
+ }
62
+
63
+ this.logger.debug(this.name, '初始化人脸模块');
64
+
65
+ try {
66
+ // 在此处初始化人脸检测、活体检测和人脸比对所需的模型
67
+ // 这里只是示例,实际实现需要根据具体的人脸识别库来实现
68
+
69
+ this._isInitialized = true;
70
+ this.emit('initialized');
71
+ this.logger.debug(this.name, '人脸模块初始化完成');
72
+ } catch (error) {
73
+ this.logger.error(this.name, '人脸模块初始化失败', error as Error);
74
+ throw new Error(`人脸模块初始化失败: ${error instanceof Error ? error.message : String(error)}`);
75
+ }
76
+ }
77
+
78
+ /**
79
+ * 检测图像中的人脸
80
+ * @param image 图像源
81
+ * @returns 人脸检测结果
82
+ */
83
+ public async detectFace(
84
+ image: ImageData | HTMLImageElement | HTMLCanvasElement
85
+ ): Promise<FaceDetectionResult | undefined> {
86
+ this.ensureInitialized();
87
+
88
+ try {
89
+ // 在此处实现人脸检测逻辑
90
+ // 这里只是示例,实际实现需要根据具体的人脸识别库来实现
91
+ const faceDetectionResult: FaceDetectionResult = {
92
+ boundingBox: {
93
+ x: 0,
94
+ y: 0,
95
+ width: 100,
96
+ height: 100
97
+ },
98
+ confidence: 0.9
99
+ };
100
+
101
+ // 保存最后一次检测结果
102
+ this.lastDetectionResult = faceDetectionResult;
103
+
104
+ // 触发事件
105
+ this.emit('face:detected', { result: faceDetectionResult });
106
+
107
+ return faceDetectionResult;
108
+ } catch (error) {
109
+ this.logger.error(this.name, '人脸检测失败', error as Error);
110
+ throw new Error(`人脸检测失败: ${error instanceof Error ? error.message : String(error)}`);
111
+ }
112
+ }
113
+
114
+ /**
115
+ * 进行活体检测
116
+ * @param image 图像源
117
+ * @returns 活体检测结果
118
+ */
119
+ public async detectLiveness(
120
+ image: ImageData | HTMLImageElement | HTMLCanvasElement
121
+ ): Promise<boolean> {
122
+ this.ensureInitialized();
123
+
124
+ if (!this.options.liveness?.enabled) {
125
+ throw new Error('活体检测未启用');
126
+ }
127
+
128
+ try {
129
+ // 在此处实现活体检测逻辑
130
+ // 这里只是示例,实际实现需要根据具体的活体检测算法来实现
131
+ const livenessResult = true;
132
+
133
+ // 触发事件
134
+ this.emit('face:liveness', { passed: livenessResult });
135
+
136
+ return livenessResult;
137
+ } catch (error) {
138
+ this.logger.error(this.name, '活体检测失败', error as Error);
139
+ throw new Error(`活体检测失败: ${error instanceof Error ? error.message : String(error)}`);
140
+ }
141
+ }
142
+
143
+ /**
144
+ * 比对两个人脸
145
+ * @param face1 第一个人脸图像
146
+ * @param face2 第二个人脸图像
147
+ * @returns 人脸比对结果
148
+ */
149
+ public async compareFaces(
150
+ face1: ImageData | HTMLImageElement | HTMLCanvasElement,
151
+ face2: ImageData | HTMLImageElement | HTMLCanvasElement
152
+ ): Promise<FaceComparisonResult> {
153
+ this.ensureInitialized();
154
+
155
+ try {
156
+ // 在此处实现人脸比对逻辑
157
+ // 这里只是示例,实际实现需要根据具体的人脸比对算法来实现
158
+ const similarity = 0.85;
159
+ const isMatch = similarity >= (this.options.comparison?.minSimilarity || 0.8);
160
+
161
+ const comparisonResult: FaceComparisonResult = {
162
+ isMatch,
163
+ similarity,
164
+ confidence: 0.9
165
+ };
166
+
167
+ // 触发事件
168
+ this.emit('face:compared', { result: comparisonResult });
169
+
170
+ return comparisonResult;
171
+ } catch (error) {
172
+ this.logger.error(this.name, '人脸比对失败', error as Error);
173
+ throw new Error(`人脸比对失败: ${error instanceof Error ? error.message : String(error)}`);
174
+ }
175
+ }
176
+
177
+ /**
178
+ * 获取最后一次检测结果
179
+ */
180
+ public getLastDetectionResult(): FaceDetectionResult | undefined {
181
+ return this.lastDetectionResult;
182
+ }
183
+
184
+ /**
185
+ * 释放模块资源
186
+ */
187
+ public async dispose(): Promise<void> {
188
+ if (!this._isInitialized) {
189
+ return;
190
+ }
191
+
192
+ this.logger.debug(this.name, '释放人脸模块资源');
193
+
194
+ try {
195
+ // 在此处释放人脸检测、活体检测和人脸比对所需的模型资源
196
+ // 这里只是示例,实际实现需要根据具体的人脸识别库来实现
197
+
198
+ // 调用基类的dispose方法
199
+ await super.dispose();
200
+ } catch (error) {
201
+ this.logger.error(this.name, '人脸模块资源释放失败', error as Error);
202
+ throw new Error(`人脸模块资源释放失败: ${error instanceof Error ? error.message : String(error)}`);
203
+ }
204
+ }
205
+ }
206
+
207
+ // 导出类型
208
+ export * from './types';