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,384 @@
1
+ /**
2
+ * @file 扫描器模块接口
3
+ * @description 定义所有扫描模块共享的核心接口
4
+ * @module interfaces/scanner-module
5
+ */
6
+
7
+ import { EventEmitter } from '../core/event-emitter';
8
+ import { Result } from '../core/result';
9
+ import { ModuleConfig } from '../core/config';
10
+ import { Module } from '../core/module-manager';
11
+
12
+ /**
13
+ * 模块状态枚举
14
+ */
15
+ export enum ModuleStatus {
16
+ /** 未初始化 */
17
+ NOT_INITIALIZED = 'not_initialized',
18
+ /** 初始化中 */
19
+ INITIALIZING = 'initializing',
20
+ /** 就绪 */
21
+ READY = 'ready',
22
+ /** 处理中 */
23
+ PROCESSING = 'processing',
24
+ /** 暂停 */
25
+ PAUSED = 'paused',
26
+ /** 错误 */
27
+ ERROR = 'error'
28
+ }
29
+
30
+ /**
31
+ * 模块类型枚举
32
+ */
33
+ export enum ModuleType {
34
+ /** 人脸识别模块 */
35
+ FACE = 'face',
36
+ /** 二维码扫描模块 */
37
+ QR = 'qr',
38
+ /** 身份证识别模块 */
39
+ IDCARD = 'idcard',
40
+ /** OCR识别模块 */
41
+ OCR = 'ocr'
42
+ }
43
+
44
+ /**
45
+ * 模块能力接口
46
+ * 描述模块支持的功能
47
+ */
48
+ export interface ModuleCapabilities {
49
+ /** 是否支持视频处理 */
50
+ supportsVideo: boolean;
51
+ /** 是否支持图片处理 */
52
+ supportsImage: boolean;
53
+ /** 是否支持批量处理 */
54
+ supportsBatch: boolean;
55
+ /** 是否支持实时处理 */
56
+ supportsRealtime: boolean;
57
+ /** 是否支持Web Worker处理 */
58
+ supportsWebWorker: boolean;
59
+ /** 支持的媒体类型 */
60
+ supportedMediaTypes: string[];
61
+ /** 其他能力 */
62
+ [key: string]: any;
63
+ }
64
+
65
+ /**
66
+ * 模块事件
67
+ */
68
+ export enum ModuleEvent {
69
+ /** 初始化开始 */
70
+ INIT_START = 'module:init:start',
71
+ /** 初始化完成 */
72
+ INIT_COMPLETE = 'module:init:complete',
73
+ /** 初始化失败 */
74
+ INIT_ERROR = 'module:init:error',
75
+ /** 处理开始 */
76
+ PROCESS_START = 'module:process:start',
77
+ /** 处理完成 */
78
+ PROCESS_COMPLETE = 'module:process:complete',
79
+ /** 处理失败 */
80
+ PROCESS_ERROR = 'module:process:error',
81
+ /** 处理进度 */
82
+ PROCESS_PROGRESS = 'module:process:progress',
83
+ /** 实时结果 */
84
+ REALTIME_RESULT = 'module:realtime:result',
85
+ /** 状态变更 */
86
+ STATUS_CHANGE = 'module:status:change'
87
+ }
88
+
89
+ /**
90
+ * 模块初始化选项
91
+ */
92
+ export interface ModuleInitOptions {
93
+ /** 模块配置 */
94
+ config?: ModuleConfig;
95
+ /** 使用Web Worker */
96
+ useWorker?: boolean;
97
+ /** Web Worker脚本路径 */
98
+ workerPath?: string;
99
+ /** 是否启用调试 */
100
+ debug?: boolean;
101
+ /** 是否绑定摄像头 */
102
+ bindCamera?: boolean;
103
+ /** 目标DOM元素 */
104
+ targetElement?: HTMLElement;
105
+ /** 模型路径 */
106
+ modelPath?: string;
107
+ /** 其他选项 */
108
+ [key: string]: any;
109
+ }
110
+
111
+ /**
112
+ * 扫描器模块接口
113
+ * 定义所有扫描模块必须实现的基础接口
114
+ */
115
+ export interface IScannerModule extends EventEmitter, Module {
116
+ /** 模块类型 */
117
+ readonly type: ModuleType;
118
+
119
+ /** 模块当前状态 */
120
+ readonly status: ModuleStatus;
121
+
122
+ /** 模块能力 */
123
+ readonly capabilities: ModuleCapabilities;
124
+
125
+ /**
126
+ * 处理图片
127
+ * @param image 图片源(URL、HTMLImageElement或其他支持的格式)
128
+ * @param options 处理选项
129
+ */
130
+ processImage(image: string | HTMLImageElement | HTMLCanvasElement | ImageData,
131
+ options?: Record<string, any>): Promise<Result<any>>;
132
+
133
+ /**
134
+ * 开始实时处理
135
+ * @param videoElement 视频元素,不提供时将使用绑定的摄像头
136
+ * @param options 处理选项
137
+ */
138
+ startRealtime(videoElement?: HTMLVideoElement, options?: Record<string, any>): Promise<Result<boolean>>;
139
+
140
+ /**
141
+ * 停止实时处理
142
+ */
143
+ stopRealtime(): void;
144
+
145
+ /**
146
+ * 暂停处理
147
+ */
148
+ pause(): void;
149
+
150
+ /**
151
+ * 恢复处理
152
+ */
153
+ resume(): Promise<boolean>;
154
+
155
+ /**
156
+ * 重置模块状态
157
+ */
158
+ reset(): void;
159
+
160
+ /**
161
+ * 获取模块版本
162
+ */
163
+ getVersion(): string;
164
+
165
+ /**
166
+ * 获取模块状态
167
+ */
168
+ getStatus(): ModuleStatus;
169
+
170
+ /**
171
+ * 获取模块配置
172
+ */
173
+ getConfig(): ModuleConfig;
174
+
175
+ /**
176
+ * 更新模块配置
177
+ * @param config 新配置
178
+ */
179
+ updateConfig(config: Partial<ModuleConfig>): void;
180
+ }
181
+
182
+ /**
183
+ * 基础扫描器结果接口
184
+ */
185
+ export interface BaseScannerResult {
186
+ /** 结果ID */
187
+ id: string;
188
+ /** 结果类型 */
189
+ type: string;
190
+ /** 处理时间(毫秒) */
191
+ processingTime: number;
192
+ /** 时间戳 */
193
+ timestamp: number;
194
+ /** 置信度(0-1) */
195
+ confidence: number;
196
+ /** 原始数据 */
197
+ rawData?: any;
198
+ }
199
+
200
+ /**
201
+ * 基础扫描器模块抽象类
202
+ * 提供通用的模块功能实现
203
+ */
204
+ export abstract class BaseScannerModule extends EventEmitter implements IScannerModule {
205
+ /** 模块类型 */
206
+ abstract readonly type: ModuleType;
207
+
208
+ /** 模块当前状态 */
209
+ protected _status: ModuleStatus = ModuleStatus.NOT_INITIALIZED;
210
+
211
+ /** 模块配置 */
212
+ protected config: ModuleConfig;
213
+
214
+ /** 模块版本 */
215
+ public readonly version: string = '1.0.0';
216
+
217
+ /** 是否为调试模式 */
218
+ protected debug: boolean = false;
219
+
220
+ /**
221
+ * 构造函数
222
+ * @param config 初始配置
223
+ */
224
+ constructor(config: ModuleConfig = { enabled: true }) {
225
+ super();
226
+ this.config = { ...config };
227
+ }
228
+
229
+ /**
230
+ * 获取模块状态
231
+ */
232
+ get status(): ModuleStatus {
233
+ return this._status;
234
+ }
235
+
236
+ /**
237
+ * 设置模块状态
238
+ */
239
+ protected setStatus(status: ModuleStatus): void {
240
+ if (this._status !== status) {
241
+ const oldStatus = this._status;
242
+ this._status = status;
243
+ this.emit(ModuleEvent.STATUS_CHANGE, { oldStatus, newStatus: status });
244
+ }
245
+ }
246
+
247
+ /**
248
+ * 获取模块能力
249
+ */
250
+ abstract get capabilities(): ModuleCapabilities;
251
+
252
+ /**
253
+ * 初始化模块
254
+ * @param options 初始化选项
255
+ */
256
+ abstract initialize(options?: ModuleInitOptions): Promise<void>;
257
+
258
+ /**
259
+ * 处理图片
260
+ * @param image 图片源(URL、HTMLImageElement或其他支持的格式)
261
+ * @param options 处理选项
262
+ */
263
+ abstract processImage(image: string | HTMLImageElement | HTMLCanvasElement | ImageData,
264
+ options?: Record<string, any>): Promise<Result<any>>;
265
+
266
+ /**
267
+ * 开始实时处理
268
+ * @param videoElement 视频元素,不提供时将使用绑定的摄像头
269
+ * @param options 处理选项
270
+ */
271
+ abstract startRealtime(videoElement?: HTMLVideoElement, options?: Record<string, any>): Promise<Result<boolean>>;
272
+
273
+ /**
274
+ * 停止实时处理
275
+ */
276
+ abstract stopRealtime(): void;
277
+
278
+ /**
279
+ * 暂停处理
280
+ */
281
+ pause(): void {
282
+ this.checkInitialized();
283
+
284
+ if (this._status === ModuleStatus.PROCESSING) {
285
+ this.setStatus(ModuleStatus.PAUSED);
286
+ this.emit('paused');
287
+ }
288
+ }
289
+
290
+ /**
291
+ * 恢复处理
292
+ */
293
+ async resume(): Promise<boolean> {
294
+ this.checkInitialized();
295
+
296
+ if (this._status === ModuleStatus.PAUSED) {
297
+ this.setStatus(ModuleStatus.PROCESSING);
298
+ this.emit('resumed');
299
+ return true;
300
+ }
301
+
302
+ return false;
303
+ }
304
+
305
+ /**
306
+ * 释放模块资源
307
+ */
308
+ abstract dispose(): Promise<void>;
309
+
310
+ /**
311
+ * 重置模块状态
312
+ */
313
+ reset(): void {
314
+ this.checkInitialized();
315
+
316
+ // 重置模块状态
317
+ this.setStatus(ModuleStatus.READY);
318
+ this.emit('reset');
319
+ }
320
+
321
+ /**
322
+ * 获取模块版本
323
+ */
324
+ getVersion(): string {
325
+ return this.version;
326
+ }
327
+
328
+ /**
329
+ * 获取模块状态
330
+ */
331
+ getStatus(): ModuleStatus {
332
+ return this._status;
333
+ }
334
+
335
+ /**
336
+ * 获取模块配置
337
+ */
338
+ getConfig(): ModuleConfig {
339
+ return { ...this.config };
340
+ }
341
+
342
+ /**
343
+ * 更新模块配置
344
+ * @param config 新配置
345
+ */
346
+ updateConfig(config: Partial<ModuleConfig>): void {
347
+ Object.assign(this.config, config);
348
+ this.emit('config:updated', { config: this.config });
349
+ }
350
+
351
+ /**
352
+ * 检查模块是否已初始化
353
+ */
354
+ protected checkInitialized(): void {
355
+ if (this._status === ModuleStatus.NOT_INITIALIZED || this._status === ModuleStatus.INITIALIZING) {
356
+ throw new Error(`Module ${this.type} is not initialized`);
357
+ }
358
+ }
359
+
360
+ /**
361
+ * 检查模块是否就绪
362
+ */
363
+ protected checkReady(): void {
364
+ this.checkInitialized();
365
+
366
+ if (this._status !== ModuleStatus.READY) {
367
+ throw new Error(`Module ${this.type} is not ready`);
368
+ }
369
+ }
370
+
371
+ /**
372
+ * 获取模块名称
373
+ */
374
+ get name(): string {
375
+ return this.type;
376
+ }
377
+
378
+ /**
379
+ * 获取模块是否已初始化
380
+ */
381
+ get isInitialized(): boolean {
382
+ return this._status !== ModuleStatus.NOT_INITIALIZED;
383
+ }
384
+ }