id-scanner-lib 1.0.0 → 1.1.1

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 (57) hide show
  1. package/README.md +96 -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 +10710 -0
  6. package/dist/id-scanner-core.esm.js.map +1 -0
  7. package/dist/id-scanner-core.js +10722 -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 +914 -0
  12. package/dist/id-scanner-ocr.esm.js.map +1 -0
  13. package/dist/id-scanner-ocr.js +923 -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 +613 -0
  18. package/dist/id-scanner-qr.esm.js.map +1 -0
  19. package/dist/id-scanner-qr.js +622 -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 +1243 -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 +20 -2
  51. package/src/id-recognition/ocr-processor.ts +3 -6
  52. package/src/index-umd.ts +240 -0
  53. package/src/index.ts +125 -139
  54. package/src/ocr-module.ts +139 -0
  55. package/src/qr-module.ts +129 -0
  56. package/src/utils/camera.ts +61 -36
  57. package/src/utils/types.ts +23 -6
@@ -0,0 +1,139 @@
1
+ /**
2
+ * @file OCR模块入口文件
3
+ * @description 包含身份证OCR识别相关功能
4
+ * @module IDScannerOCR
5
+ * @version 1.0.0
6
+ * @license MIT
7
+ */
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';
15
+
16
+ /**
17
+ * OCR模块配置选项
18
+ */
19
+ export interface OCRModuleOptions {
20
+ cameraOptions?: CameraOptions;
21
+ onIDCardScanned?: (info: IDCardInfo) => void;
22
+ onError?: (error: Error) => void;
23
+ }
24
+
25
+ /**
26
+ * OCR模块类
27
+ *
28
+ * 提供身份证检测和OCR文字识别功能
29
+ */
30
+ 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
+
38
+ /**
39
+ * 构造函数
40
+ * @param options 配置选项
41
+ */
42
+ constructor(private options: OCRModuleOptions = {}) {
43
+ this.camera = new Camera(options.cameraOptions);
44
+ this.idDetector = new IDCardDetector({
45
+ onDetection: this.handleIDDetection.bind(this),
46
+ onError: this.handleError.bind(this)
47
+ } as IDCardDetectorOptions);
48
+ this.ocrProcessor = new OCRProcessor();
49
+ this.dataExtractor = new DataExtractor();
50
+ }
51
+
52
+ /**
53
+ * 初始化OCR引擎
54
+ *
55
+ * @returns Promise<void>
56
+ */
57
+ async initialize(): Promise<void> {
58
+ try {
59
+ await this.ocrProcessor.initialize();
60
+ console.log('OCR engine initialized');
61
+ } catch (error) {
62
+ this.handleError(error as Error);
63
+ throw error;
64
+ }
65
+ }
66
+
67
+ /**
68
+ * 启动身份证扫描
69
+ * @param videoElement HTML视频元素
70
+ */
71
+ async startIDCardScanner(videoElement: HTMLVideoElement): Promise<void> {
72
+ if (!this.ocrProcessor) {
73
+ throw new Error('OCR engine not initialized. Call initialize() first.');
74
+ }
75
+
76
+ this.videoElement = videoElement;
77
+ this.isRunning = true;
78
+ await this.camera.start(videoElement);
79
+ this.idDetector.start(videoElement);
80
+ }
81
+
82
+ /**
83
+ * 停止扫描
84
+ */
85
+ stop(): void {
86
+ this.isRunning = false;
87
+ this.idDetector.stop();
88
+ this.camera.stop();
89
+ }
90
+
91
+ /**
92
+ * 处理身份证检测结果
93
+ */
94
+ private async handleIDDetection(result: DetectionResult): Promise<void> {
95
+ if (!this.isRunning) return;
96
+
97
+ try {
98
+ // 检查 imageData 是否存在
99
+ if (!result.imageData) {
100
+ this.handleError(new Error('无效的图像数据'));
101
+ return;
102
+ }
103
+
104
+ const idCardInfo = await this.ocrProcessor.processIDCard(result.imageData);
105
+ const extractedInfo = this.dataExtractor.extractAndValidate(idCardInfo);
106
+
107
+ if (this.options.onIDCardScanned) {
108
+ this.options.onIDCardScanned(extractedInfo);
109
+ }
110
+ } catch (error) {
111
+ this.handleError(error as Error);
112
+ }
113
+ }
114
+
115
+ /**
116
+ * 处理错误
117
+ */
118
+ private handleError(error: Error): void {
119
+ if (this.options.onError) {
120
+ this.options.onError(error);
121
+ } else {
122
+ console.error('OCRModule error:', error);
123
+ }
124
+ }
125
+
126
+ /**
127
+ * 释放资源
128
+ */
129
+ async terminate(): Promise<void> {
130
+ this.stop();
131
+ await this.ocrProcessor.terminate();
132
+ }
133
+ }
134
+
135
+ // 导出相关类型和工具
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';
@@ -0,0 +1,129 @@
1
+ /**
2
+ * @file 二维码和条形码扫描模块
3
+ * @description 包含二维码和条形码扫描功能
4
+ * @module IDScannerQR
5
+ * @version 1.0.0
6
+ * @license MIT
7
+ */
8
+
9
+ import { QRScanner, QRScannerOptions } from './scanner/qr-scanner';
10
+ import { BarcodeScanner, BarcodeScannerOptions } from './scanner/barcode-scanner';
11
+ import { Camera, CameraOptions } from './utils/camera';
12
+
13
+ /**
14
+ * 扫描模块配置选项
15
+ */
16
+ 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;
23
+ }
24
+
25
+ /**
26
+ * 扫描模块类
27
+ *
28
+ * 提供独立的二维码和条形码扫描功能
29
+ */
30
+ 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
+
37
+ /**
38
+ * 构造函数
39
+ * @param options 配置选项
40
+ */
41
+ constructor(private options: ScannerModuleOptions = {}) {
42
+ this.camera = new Camera(options.cameraOptions);
43
+ this.qrScanner = new QRScanner({
44
+ ...options.qrScannerOptions,
45
+ onScan: this.handleQRScan.bind(this)
46
+ });
47
+ this.barcodeScanner = new BarcodeScanner({
48
+ ...options.barcodeScannerOptions,
49
+ onScan: this.handleBarcodeScan.bind(this)
50
+ });
51
+ }
52
+
53
+ /**
54
+ * 启动二维码扫描
55
+ * @param videoElement HTML视频元素
56
+ */
57
+ 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);
64
+ }
65
+
66
+ /**
67
+ * 启动条形码扫描
68
+ * @param videoElement HTML视频元素
69
+ */
70
+ 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);
77
+ }
78
+
79
+ /**
80
+ * 停止扫描
81
+ */
82
+ stop(): void {
83
+ if (this.scanMode === 'qr') {
84
+ this.qrScanner.stop();
85
+ } else if (this.scanMode === 'barcode') {
86
+ this.barcodeScanner.stop();
87
+ }
88
+
89
+ if (this.videoElement) {
90
+ this.camera.stop();
91
+ }
92
+
93
+ this.scanMode = null;
94
+ }
95
+
96
+ /**
97
+ * 处理二维码扫描结果
98
+ */
99
+ private handleQRScan(result: string): void {
100
+ if (this.options.onQRCodeScanned) {
101
+ this.options.onQRCodeScanned(result);
102
+ }
103
+ }
104
+
105
+ /**
106
+ * 处理条形码扫描结果
107
+ */
108
+ private handleBarcodeScan(result: string): void {
109
+ if (this.options.onBarcodeScanned) {
110
+ this.options.onBarcodeScanned(result);
111
+ }
112
+ }
113
+
114
+ /**
115
+ * 处理错误
116
+ */
117
+ private handleError(error: Error): void {
118
+ if (this.options.onError) {
119
+ this.options.onError(error);
120
+ } else {
121
+ console.error('ScannerModule error:', error);
122
+ }
123
+ }
124
+ }
125
+
126
+ // 导出相关类型和工具
127
+ export { QRScanner, QRScannerOptions } from './scanner/qr-scanner';
128
+ export { BarcodeScanner, BarcodeScannerOptions } from './scanner/barcode-scanner';
129
+ export { Camera, CameraOptions } from './utils/camera';
@@ -34,13 +34,13 @@ export interface CameraOptions {
34
34
  *
35
35
  * // 初始化相机
36
36
  * const videoElement = document.getElementById('video') as HTMLVideoElement;
37
- * await camera.initialize(videoElement);
37
+ * await camera.start(videoElement);
38
38
  *
39
39
  * // 捕获当前视频帧
40
40
  * const imageData = camera.captureFrame();
41
41
  *
42
42
  * // 使用结束后释放资源
43
- * camera.release();
43
+ * camera.stop();
44
44
  * ```
45
45
  */
46
46
  export class Camera {
@@ -49,7 +49,6 @@ export class Camera {
49
49
 
50
50
  /**
51
51
  * 创建相机实例
52
- *
53
52
  * @param {CameraOptions} [options] - 相机配置选项
54
53
  */
55
54
  constructor(private options: CameraOptions = {}) {
@@ -62,75 +61,101 @@ export class Camera {
62
61
  }
63
62
 
64
63
  /**
65
- * 初始化相机,请求摄像头权限并设置视频流
64
+ * 启动摄像头并将视频流绑定到视频元素
65
+ * @param videoElement HTML视频元素
66
+ * @returns Promise<void>
67
+ */
68
+ async start(videoElement: HTMLVideoElement): Promise<void> {
69
+ return this.initialize(videoElement);
70
+ }
71
+
72
+ /**
73
+ * 停止摄像头并释放资源
74
+ */
75
+ stop(): void {
76
+ this.release();
77
+ }
78
+
79
+ /**
80
+ * 初始化相机,获取视频流并绑定到视频元素
66
81
  *
67
- * @param {HTMLVideoElement} videoElement - 用于显示相机画面的video元素
82
+ * @param {HTMLVideoElement} videoElement - 用于显示视频流的视频元素
68
83
  * @returns {Promise<void>} 初始化完成的Promise
69
- * @throws 如果无法访问相机,将抛出错误
84
+ * @throws 如果无法访问摄像头,将抛出错误
70
85
  */
71
86
  async initialize(videoElement: HTMLVideoElement): Promise<void> {
87
+ this.videoElement = videoElement;
88
+
72
89
  try {
73
- this.videoElement = videoElement;
74
-
90
+ // 构建媒体约束
75
91
  const constraints: MediaStreamConstraints = {
76
92
  video: {
77
- width: this.options.width,
78
- height: this.options.height,
93
+ width: { ideal: this.options.width },
94
+ height: { ideal: this.options.height },
79
95
  facingMode: this.options.facingMode
80
96
  }
81
97
  };
82
98
 
99
+ // 获取视频流
83
100
  this.stream = await navigator.mediaDevices.getUserMedia(constraints);
84
- this.videoElement.srcObject = this.stream;
85
101
 
86
- return new Promise((resolve) => {
87
- if (this.videoElement) {
88
- this.videoElement.onloadedmetadata = () => {
89
- if (this.videoElement) {
90
- this.videoElement.play();
91
- resolve();
92
- }
93
- };
94
- }
95
- });
102
+ // 绑定到视频元素
103
+ if (this.videoElement) {
104
+ this.videoElement.srcObject = this.stream;
105
+ await new Promise<void>((resolve) => {
106
+ if (this.videoElement) {
107
+ this.videoElement.onloadedmetadata = () => {
108
+ if (this.videoElement) {
109
+ this.videoElement.play().then(() => resolve());
110
+ }
111
+ };
112
+ }
113
+ });
114
+ }
96
115
  } catch (error) {
97
- console.error('相机初始化失败:', error);
98
- throw new Error('无法访问相机');
116
+ console.error('无法访问摄像头:', error);
117
+ throw new Error('无法访问摄像头。请确保已授予摄像头访问权限,并且摄像头未被其他应用程序占用。');
99
118
  }
100
119
  }
101
120
 
102
121
  /**
103
- * 获取当前视频帧
104
- *
105
- * 捕获当前视频画面并转换为ImageData对象,可用于图像处理和分析
122
+ * 捕获当前视频帧
106
123
  *
107
- * @returns {ImageData|null} 当前视频帧的ImageData对象,如果未初始化视频则返回null
124
+ * @returns {ImageData|null} 视频帧的ImageData对象,如果未初始化则返回null
108
125
  */
109
126
  captureFrame(): ImageData | null {
110
- if (!this.videoElement) return null;
127
+ if (!this.videoElement) {
128
+ return null;
129
+ }
111
130
 
131
+ // 创建Canvas元素用于捕获视频帧
112
132
  const canvas = document.createElement('canvas');
113
- const ctx = canvas.getContext('2d');
114
- if (!ctx) return null;
115
-
116
133
  canvas.width = this.videoElement.videoWidth;
117
134
  canvas.height = this.videoElement.videoHeight;
118
- ctx.drawImage(this.videoElement, 0, 0);
119
135
 
120
- return ctx.getImageData(0, 0, canvas.width, canvas.height);
136
+ const context = canvas.getContext('2d');
137
+ if (!context) {
138
+ return null;
139
+ }
140
+
141
+ // 将视频内容绘制到Canvas中
142
+ context.drawImage(this.videoElement, 0, 0, canvas.width, canvas.height);
143
+
144
+ // 获取ImageData对象
145
+ return context.getImageData(0, 0, canvas.width, canvas.height);
121
146
  }
122
147
 
123
148
  /**
124
- * 释放相机资源
125
- *
126
- * 停止所有视频流轨道并清理资源。在不再需要相机时应调用此方法。
149
+ * 释放摄像头资源
127
150
  */
128
151
  release(): void {
152
+ // 停止视频流的所有轨道
129
153
  if (this.stream) {
130
154
  this.stream.getTracks().forEach(track => track.stop());
131
155
  this.stream = null;
132
156
  }
133
157
 
158
+ // 清除视频元素绑定
134
159
  if (this.videoElement) {
135
160
  this.videoElement.srcObject = null;
136
161
  this.videoElement = null;
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @file 类型定义文件
3
- * @description 定义库中使用的公共类型和接口
3
+ * @description 定义全局类型
4
4
  * @module Types
5
5
  */
6
6
 
@@ -15,11 +15,28 @@
15
15
  * @property {number} corners[].x - 角点X坐标
16
16
  * @property {number} corners[].y - 角点Y坐标
17
17
  * @property {ImageData} [croppedImage] - 裁剪后的身份证图像
18
+ * @property {ImageData} [imageData] - 原始图像数据
19
+ * @property {Object} [boundingBox] - 检测到的身份证边界框
20
+ * @property {number} boundingBox.x - 边界框左上角X坐标
21
+ * @property {number} boundingBox.y - 边界框左上角Y坐标
22
+ * @property {number} boundingBox.width - 边界框宽度
23
+ * @property {number} boundingBox.height - 边界框高度
24
+ * @property {number} [confidence] - 检测结果的置信度
25
+ * @property {string} [message] - 检测结果的消息
18
26
  */
19
27
  export interface DetectionResult {
20
28
  success: boolean;
21
29
  corners?: { x: number; y: number }[];
22
30
  croppedImage?: ImageData;
31
+ imageData?: ImageData;
32
+ boundingBox?: {
33
+ x: number;
34
+ y: number;
35
+ width: number;
36
+ height: number;
37
+ };
38
+ confidence?: number;
39
+ message?: string;
23
40
  }
24
41
 
25
42
  /**
@@ -29,11 +46,11 @@ export interface DetectionResult {
29
46
  *
30
47
  * @interface IDCardInfo
31
48
  * @property {string} [name] - 姓名
32
- * @property {string} [gender] - 性别,通常为"男"或"女"
33
- * @property {string} [nationality] - 民族,如"汉族"、"满族"等
34
- * @property {string} [birthDate] - 出生日期,格式为"YYYY-MM-DD"
35
- * @property {string} [address] - 住址
36
- * @property {string} [idNumber] - 身份证号码,18位
49
+ * @property {string} [gender] - 性别
50
+ * @property {string} [nationality] - 民族
51
+ * @property {string} [birthDate] - 出生日期
52
+ * @property {string} [address] - 地址
53
+ * @property {string} [idNumber] - 身份证号码
37
54
  * @property {string} [issuingAuthority] - 签发机关
38
55
  * @property {string} [validPeriod] - 有效期限
39
56
  *