id-scanner-lib 1.5.0 → 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 (72) hide show
  1. package/README.md +378 -59
  2. package/dist/id-scanner-lib.esm.js +195 -10
  3. package/dist/id-scanner-lib.esm.js.map +1 -1
  4. package/dist/id-scanner-lib.js +4812 -14709
  5. package/dist/id-scanner-lib.js.map +1 -1
  6. package/dist/types/browser-image-compression.d.ts +19 -0
  7. package/dist/types/tesseract.d.ts +280 -0
  8. package/package.json +21 -11
  9. package/src/core/camera-manager.ts +16 -1
  10. package/src/core/config.ts +37 -0
  11. package/src/core/errors.ts +3 -3
  12. package/src/core/event-emitter.test.ts +42 -0
  13. package/src/core/loading-state.test.ts +67 -0
  14. package/src/core/loading-state.ts +156 -0
  15. package/src/core/logger.test.ts +49 -0
  16. package/src/core/module-manager.ts +2 -4
  17. package/src/core/scanner-factory.ts +8 -9
  18. package/src/index.ts +3 -2
  19. package/src/modules/face/face-detector.ts +123 -66
  20. package/src/modules/id-card/anti-fake-detector.ts +2 -2
  21. package/src/modules/id-card/ocr-worker.ts +1 -1
  22. package/src/modules/qrcode/qr-code-scanner.ts +2 -1
  23. package/src/modules/qrcode/types.ts +111 -7
  24. package/src/types/common.test.ts +99 -0
  25. package/src/types/common.ts +166 -0
  26. package/src/utils/camera.test.ts +30 -0
  27. package/src/utils/camera.ts +4 -1
  28. package/src/utils/error-handler.test.ts +137 -0
  29. package/src/utils/error-handler.ts +110 -0
  30. package/src/utils/index.test.ts +186 -0
  31. package/src/utils/index.ts +3 -0
  32. package/src/utils/retry.test.ts +142 -0
  33. package/src/utils/retry.ts +282 -0
  34. package/src/utils/utils.test.ts +171 -0
  35. package/src/version.ts +1 -1
  36. package/dist/types/core/base-module.d.ts +0 -44
  37. package/dist/types/core/camera-manager.d.ts +0 -258
  38. package/dist/types/core/config.d.ts +0 -88
  39. package/dist/types/core/errors.d.ts +0 -111
  40. package/dist/types/core/event-emitter.d.ts +0 -55
  41. package/dist/types/core/logger.d.ts +0 -277
  42. package/dist/types/core/module-manager.d.ts +0 -78
  43. package/dist/types/core/plugin-manager.d.ts +0 -158
  44. package/dist/types/core/resource-manager.d.ts +0 -246
  45. package/dist/types/core/result.d.ts +0 -83
  46. package/dist/types/core/scanner-factory.d.ts +0 -93
  47. package/dist/types/index.bundle.d.ts +0 -1303
  48. package/dist/types/index.d.ts +0 -86
  49. package/dist/types/interfaces/external-types.d.ts +0 -174
  50. package/dist/types/interfaces/face-detection.d.ts +0 -293
  51. package/dist/types/interfaces/scanner-module.d.ts +0 -280
  52. package/dist/types/modules/face/face-detector.d.ts +0 -170
  53. package/dist/types/modules/face/index.d.ts +0 -56
  54. package/dist/types/modules/face/liveness-detector.d.ts +0 -177
  55. package/dist/types/modules/face/types.d.ts +0 -136
  56. package/dist/types/modules/id-card/anti-fake-detector.d.ts +0 -170
  57. package/dist/types/modules/id-card/id-card-detector.d.ts +0 -131
  58. package/dist/types/modules/id-card/index.d.ts +0 -89
  59. package/dist/types/modules/id-card/ocr-processor.d.ts +0 -110
  60. package/dist/types/modules/id-card/ocr-worker.d.ts +0 -31
  61. package/dist/types/modules/id-card/types.d.ts +0 -181
  62. package/dist/types/modules/qrcode/index.d.ts +0 -51
  63. package/dist/types/modules/qrcode/qr-code-scanner.d.ts +0 -64
  64. package/dist/types/modules/qrcode/types.d.ts +0 -67
  65. package/dist/types/utils/camera.d.ts +0 -81
  66. package/dist/types/utils/image-processing.d.ts +0 -176
  67. package/dist/types/utils/index.d.ts +0 -175
  68. package/dist/types/utils/performance.d.ts +0 -81
  69. package/dist/types/utils/resource-manager.d.ts +0 -53
  70. package/dist/types/utils/types.d.ts +0 -166
  71. package/dist/types/utils/worker.d.ts +0 -52
  72. package/dist/types/version.d.ts +0 -7
@@ -226,6 +226,14 @@ class ConfigManager {
226
226
  }
227
227
  }
228
228
  }
229
+ /**
230
+ * 检查模块是否启用
231
+ * @param moduleName 模块名称
232
+ */
233
+ isModuleEnabled(moduleName) {
234
+ const key = `modules.${moduleName}.enabled`;
235
+ return this.get(key) ?? false;
236
+ }
229
237
  }
230
238
 
231
239
  /**
@@ -784,7 +792,7 @@ class EventEmitter {
784
792
  * @module Version
785
793
  */
786
794
  // 当前版本号
787
- const VERSION = '1.5.0';
795
+ const VERSION = '1.3.3';
788
796
  // 构建日期
789
797
  const BUILD_DATE = new Date().toISOString();
790
798
 
@@ -855,8 +863,7 @@ class ModuleManager extends EventEmitter {
855
863
  this.logger.debug('ModuleManager', `模块 ${name} 初始化完成`);
856
864
  }
857
865
  catch (error) {
858
- const errorObj = error instanceof Error ? error : new Error(String(error));
859
- this.logger.error('ModuleManager', `模块 ${name} 初始化失败`, errorObj);
866
+ this.logger.error('ModuleManager', `模块 ${name} 初始化失败`, error instanceof Error ? error : undefined);
860
867
  this.emit('module:error', { name, error });
861
868
  throw new Error(`模块 ${name} 初始化失败: ${error instanceof Error ? error.message : String(error)}`);
862
869
  }
@@ -877,8 +884,7 @@ class ModuleManager extends EventEmitter {
877
884
  this.emit('module:disposed', { name });
878
885
  }
879
886
  catch (error) {
880
- const errorObj = error instanceof Error ? error : new Error(String(error));
881
- this.logger.error('ModuleManager', `模块 ${name} 资源释放失败`, errorObj);
887
+ this.logger.error('ModuleManager', `模块 ${name} 资源释放失败`, error instanceof Error ? error : undefined);
882
888
  this.emit('module:error', { name, error });
883
889
  }
884
890
  }
@@ -2536,7 +2542,7 @@ function parseIDCardText(text) {
2536
2542
  const birthDateRegex1 = /出生[\s\:]*(\d{4})年(\d{1,2})月(\d{1,2})[日号]/;
2537
2543
  const birthDateRegex2 = /出生[\s\:]*(\d{4})[-\/\.](\d{1,2})[-\/\.](\d{1,2})/;
2538
2544
  const birthDateRegex3 = /出生日期[\s\:]*(\d{4})[-\/\.\u5e74](\d{1,2})[-\/\.\u6708](\d{1,2})[日号]?/;
2539
- let birthDateMatch = processedText.match(birthDateRegex1) ||
2545
+ const birthDateMatch = processedText.match(birthDateRegex1) ||
2540
2546
  processedText.match(birthDateRegex2) ||
2541
2547
  processedText.match(birthDateRegex3);
2542
2548
  if (!birthDateMatch && info.idNumber && info.idNumber.length === 18) {
@@ -3122,7 +3128,7 @@ class AntiFakeDetector {
3122
3128
  const isAuthentic = normalizedConfidence >= this.options.sensitivity &&
3123
3129
  detectedFeatures.length >= 2;
3124
3130
  // 生成结果消息
3125
- let message = isAuthentic
3131
+ const message = isAuthentic
3126
3132
  ? `身份证真实,检测到${detectedFeatures.length}个防伪特征`
3127
3133
  : detectedFeatures.length > 0
3128
3134
  ? `可疑身份证,仅检测到${detectedFeatures.length}个防伪特征,置信度不足`
@@ -3914,6 +3920,57 @@ class IDCardModule extends BaseModule {
3914
3920
  }
3915
3921
  }
3916
3922
 
3923
+ /**
3924
+ * @file 二维码模块类型定义
3925
+ * @description 二维码模块相关的类型和接口定义
3926
+ * @module modules/qrcode/types
3927
+ */
3928
+ /**
3929
+ * 支持的条码格式
3930
+ */
3931
+ var BarcodeFormat;
3932
+ (function (BarcodeFormat) {
3933
+ /** QR码 */
3934
+ BarcodeFormat["QR_CODE"] = "qrcode";
3935
+ /** Code 128 */
3936
+ BarcodeFormat["CODE_128"] = "code_128";
3937
+ /** Code 39 */
3938
+ BarcodeFormat["CODE_39"] = "code_39";
3939
+ /** Code 93 */
3940
+ BarcodeFormat["CODE_93"] = "code_93";
3941
+ /** EAN-13 */
3942
+ BarcodeFormat["EAN_13"] = "ean_13";
3943
+ /** EAN-8 */
3944
+ BarcodeFormat["EAN_8"] = "ean_8";
3945
+ /** UPC-A */
3946
+ BarcodeFormat["UPC_A"] = "upc_a";
3947
+ /** UPC-E */
3948
+ BarcodeFormat["UPC_E"] = "upc_e";
3949
+ /** ITF */
3950
+ BarcodeFormat["ITF"] = "itf";
3951
+ /** PDF417 */
3952
+ BarcodeFormat["PDF_417"] = "pdf_417";
3953
+ /** DataMatrix */
3954
+ BarcodeFormat["DATA_MATRIX"] = "data_matrix";
3955
+ /** Aztec */
3956
+ BarcodeFormat["AZTEC"] = "aztec";
3957
+ /** Codabar */
3958
+ BarcodeFormat["CODABAR"] = "codabar";
3959
+ /** Industrial 2 of 5 */
3960
+ BarcodeFormat["INDUSTRIAL_2_OF_5"] = "industrial_2_of_5";
3961
+ /** QR Code Micro */
3962
+ BarcodeFormat["QR_CODE_MICRO"] = "qr_code_micro";
3963
+ })(BarcodeFormat || (BarcodeFormat = {}));
3964
+ /**
3965
+ * 默认支持的格式
3966
+ */
3967
+ const DEFAULT_FORMATS = [
3968
+ BarcodeFormat.QR_CODE,
3969
+ BarcodeFormat.CODE_128,
3970
+ BarcodeFormat.CODE_39,
3971
+ BarcodeFormat.EAN_13
3972
+ ];
3973
+
3917
3974
  /**
3918
3975
  * @file 二维码扫描器
3919
3976
  * @description 提供二维码检测和解析功能
@@ -3983,6 +4040,7 @@ class QRCodeScanner extends EventEmitter {
3983
4040
  // 构建结果
3984
4041
  const result = {
3985
4042
  data: code.data,
4043
+ barcodeFormat: BarcodeFormat.QR_CODE,
3986
4044
  boundingBox: {
3987
4045
  topLeft: code.location.topLeftCorner,
3988
4046
  topRight: code.location.topRightCorner,
@@ -4386,6 +4444,133 @@ class FaceModule extends BaseModule {
4386
4444
  }
4387
4445
  }
4388
4446
 
4447
+ /**
4448
+ * @file 加载状态管理
4449
+ * @description 提供模块加载状态跟踪功能
4450
+ * @module core/loading-state
4451
+ */
4452
+ /**
4453
+ * 加载状态类型
4454
+ */
4455
+ var LoadingState;
4456
+ (function (LoadingState) {
4457
+ /** 空闲 */
4458
+ LoadingState["IDLE"] = "idle";
4459
+ /** 加载中 */
4460
+ LoadingState["LOADING"] = "loading";
4461
+ /** 就绪 */
4462
+ LoadingState["READY"] = "ready";
4463
+ /** 错误 */
4464
+ LoadingState["ERROR"] = "error";
4465
+ /** 已释放 */
4466
+ LoadingState["DISPOSED"] = "disposed";
4467
+ })(LoadingState || (LoadingState = {}));
4468
+ /**
4469
+ * 加载状态管理器
4470
+ * 用于跟踪和管理模块的加载状态
4471
+ */
4472
+ class LoadingStateManager extends EventEmitter {
4473
+ constructor() {
4474
+ super(...arguments);
4475
+ this.state = LoadingState.IDLE;
4476
+ this.loadedModels = new Set();
4477
+ this.totalModels = 0;
4478
+ }
4479
+ /**
4480
+ * 开始加载
4481
+ * @param totalModels 总模型数
4482
+ */
4483
+ startLoading(totalModels) {
4484
+ this.totalModels = totalModels;
4485
+ this.state = LoadingState.LOADING;
4486
+ this.error = undefined;
4487
+ this.emit('stateChange', this.getProgress());
4488
+ }
4489
+ /**
4490
+ * 模型开始加载
4491
+ * @param modelName 模型名称
4492
+ */
4493
+ startModelLoading(modelName) {
4494
+ this.loadingModel = modelName;
4495
+ this.emit('progress', this.getProgress());
4496
+ }
4497
+ /**
4498
+ * 模型加载完成
4499
+ * @param modelName 模型名称
4500
+ */
4501
+ completeModelLoading(modelName) {
4502
+ this.loadedModels.add(modelName);
4503
+ this.loadingModel = undefined;
4504
+ this.emit('progress', this.getProgress());
4505
+ }
4506
+ /**
4507
+ * 加载完成
4508
+ */
4509
+ complete() {
4510
+ this.state = LoadingState.READY;
4511
+ this.totalModels = 0;
4512
+ this.emit('stateChange', this.getProgress());
4513
+ }
4514
+ /**
4515
+ * 加载失败
4516
+ * @param error 错误信息
4517
+ */
4518
+ fail(error) {
4519
+ this.state = LoadingState.ERROR;
4520
+ this.error = error;
4521
+ this.emit('error', { error, progress: this.getProgress() });
4522
+ }
4523
+ /**
4524
+ * 释放
4525
+ */
4526
+ dispose() {
4527
+ this.state = LoadingState.DISPOSED;
4528
+ this.loadedModels.clear();
4529
+ this.totalModels = 0;
4530
+ this.error = undefined;
4531
+ this.emit('stateChange', this.getProgress());
4532
+ }
4533
+ /**
4534
+ * 获取当前进度
4535
+ */
4536
+ getProgress() {
4537
+ const progress = this.totalModels > 0
4538
+ ? Math.round((this.loadedModels.size / this.totalModels) * 100)
4539
+ : 0;
4540
+ return {
4541
+ state: this.state,
4542
+ loadedModels: Array.from(this.loadedModels),
4543
+ loadingModel: this.loadingModel,
4544
+ progress,
4545
+ error: this.error
4546
+ };
4547
+ }
4548
+ /**
4549
+ * 获取当前状态
4550
+ */
4551
+ getState() {
4552
+ return this.state;
4553
+ }
4554
+ /**
4555
+ * 是否已就绪
4556
+ */
4557
+ isReady() {
4558
+ return this.state === LoadingState.READY;
4559
+ }
4560
+ /**
4561
+ * 是否有错误
4562
+ */
4563
+ hasError() {
4564
+ return this.state === LoadingState.ERROR;
4565
+ }
4566
+ }
4567
+ /**
4568
+ * 创建加载状态管理器
4569
+ */
4570
+ function createLoadingStateManager() {
4571
+ return new LoadingStateManager();
4572
+ }
4573
+
4389
4574
  /**
4390
4575
  * @file 错误处理模块
4391
4576
  * @description 定义ID-Scanner-Lib的错误类层次结构
@@ -4409,8 +4594,8 @@ class IDScannerError extends Error {
4409
4594
  this.code = options?.code || 'UNKNOWN_ERROR';
4410
4595
  // 设置错误原因
4411
4596
  this.cause = options?.cause;
4412
- // 捕获堆栈
4413
- if (Error.captureStackTrace) {
4597
+ // 捕获堆栈 (Node.js专有,浏览器环境忽略)
4598
+ if (typeof Error.captureStackTrace === 'function') {
4414
4599
  Error.captureStackTrace(this, this.constructor);
4415
4600
  }
4416
4601
  }
@@ -4637,5 +4822,5 @@ IDScanner.VERSION = VERSION;
4637
4822
  /** 构建日期 */
4638
4823
  IDScanner.BUILD_DATE = BUILD_DATE;
4639
4824
 
4640
- export { CameraAccessError, ConsoleLogHandler, DeviceError, FaceComparisonError, FaceDetectionError, FaceModule, IDCardDetectionError, IDCardModule, IDCardType, IDScanner, IDScannerError, InitializationError, InvalidArgumentError, LivenessDetectionError, LogLevel, Logger, MemoryLogHandler, ModuleManager, NotSupportedError, OCRProcessingError, QRCodeModule, QRScanError, RemoteLogHandler, ResourceLoadError, TaggedLogger, IDScanner as default };
4825
+ export { BarcodeFormat, CameraAccessError, ConsoleLogHandler, DEFAULT_FORMATS, DeviceError, FaceComparisonError, FaceDetectionError, FaceModule, IDCardDetectionError, IDCardModule, IDCardType, IDScanner, IDScannerError, InitializationError, InvalidArgumentError, LivenessDetectionError, LoadingState, LoadingStateManager, LogLevel, Logger, MemoryLogHandler, ModuleManager, NotSupportedError, OCRProcessingError, QRCodeModule, QRScanError, RemoteLogHandler, ResourceLoadError, TaggedLogger, createLoadingStateManager, IDScanner as default };
4641
4826
  //# sourceMappingURL=id-scanner-lib.esm.js.map