id-scanner-lib 1.0.0
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.
- package/LICENSE +21 -0
- package/README.md +317 -0
- package/dist/demo/demo.d.ts +14 -0
- package/dist/id-recognition/data-extractor.d.ts +74 -0
- package/dist/id-recognition/id-detector.d.ts +76 -0
- package/dist/id-recognition/ocr-processor.d.ts +64 -0
- package/dist/id-scanner.esm.js +94656 -0
- package/dist/id-scanner.esm.js.map +1 -0
- package/dist/id-scanner.js +94660 -0
- package/dist/id-scanner.js.map +1 -0
- package/dist/id-scanner.min.js +9 -0
- package/dist/id-scanner.min.js.map +1 -0
- package/dist/index.d.ts +143 -0
- package/dist/scanner/barcode-scanner.d.ts +90 -0
- package/dist/scanner/qr-scanner.d.ts +80 -0
- package/dist/utils/camera.d.ts +76 -0
- package/dist/utils/image-processing.d.ts +75 -0
- package/dist/utils/types.d.ts +65 -0
- package/package.json +56 -0
- package/src/demo/demo.ts +88 -0
- package/src/id-recognition/data-extractor.ts +165 -0
- package/src/id-recognition/id-detector.ts +171 -0
- package/src/id-recognition/ocr-processor.ts +169 -0
- package/src/index.ts +228 -0
- package/src/scanner/barcode-scanner.ts +164 -0
- package/src/scanner/qr-scanner.ts +128 -0
- package/src/utils/camera.ts +139 -0
- package/src/utils/image-processing.ts +157 -0
- package/src/utils/types.ts +64 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file ID扫描识别库主入口文件
|
|
3
|
+
* @description 提供身份证识别与二维码、条形码扫描功能的纯前端TypeScript库
|
|
4
|
+
* @module IDScannerLib
|
|
5
|
+
* @version 1.0.0
|
|
6
|
+
* @license MIT
|
|
7
|
+
*/
|
|
8
|
+
import { QRScanner, QRScannerOptions } from './scanner/qr-scanner';
|
|
9
|
+
import { BarcodeScanner, BarcodeScannerOptions } from './scanner/barcode-scanner';
|
|
10
|
+
import { IDCardDetector } from './id-recognition/id-detector';
|
|
11
|
+
import { OCRProcessor } from './id-recognition/ocr-processor';
|
|
12
|
+
import { DataExtractor } from './id-recognition/data-extractor';
|
|
13
|
+
import { Camera, CameraOptions } from './utils/camera';
|
|
14
|
+
import { ImageProcessor } from './utils/image-processing';
|
|
15
|
+
import { IDCardInfo, DetectionResult } from './utils/types';
|
|
16
|
+
/**
|
|
17
|
+
* IDScanner配置选项接口
|
|
18
|
+
* @interface IDScannerOptions
|
|
19
|
+
* @property {CameraOptions} [cameraOptions] - 相机配置选项
|
|
20
|
+
* @property {QRScannerOptions} [qrScannerOptions] - 二维码扫描配置选项
|
|
21
|
+
* @property {BarcodeScannerOptions} [barcodeScannerOptions] - 条形码扫描配置选项
|
|
22
|
+
* @property {Function} [onQRCodeScanned] - 二维码识别成功回调
|
|
23
|
+
* @property {Function} [onBarcodeScanned] - 条形码识别成功回调
|
|
24
|
+
* @property {Function} [onIDCardScanned] - 身份证识别成功回调
|
|
25
|
+
* @property {Function} [onError] - 错误处理回调
|
|
26
|
+
*/
|
|
27
|
+
export interface IDScannerOptions {
|
|
28
|
+
cameraOptions?: CameraOptions;
|
|
29
|
+
qrScannerOptions?: QRScannerOptions;
|
|
30
|
+
barcodeScannerOptions?: BarcodeScannerOptions;
|
|
31
|
+
onQRCodeScanned?: (result: string) => void;
|
|
32
|
+
onBarcodeScanned?: (result: string) => void;
|
|
33
|
+
onIDCardScanned?: (info: IDCardInfo) => void;
|
|
34
|
+
onError?: (error: Error) => void;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* IDScanner 主类
|
|
38
|
+
*
|
|
39
|
+
* 整合二维码、条形码扫描和身份证识别功能,提供统一的接口
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* // 创建扫描器实例
|
|
44
|
+
* const scanner = new IDScanner({
|
|
45
|
+
* onQRCodeScanned: (result) => {
|
|
46
|
+
* console.log('扫描到二维码:', result);
|
|
47
|
+
* },
|
|
48
|
+
* onIDCardScanned: (info) => {
|
|
49
|
+
* console.log('识别到身份证信息:', info);
|
|
50
|
+
* }
|
|
51
|
+
* });
|
|
52
|
+
*
|
|
53
|
+
* // 初始化OCR引擎和相关资源
|
|
54
|
+
* await scanner.initialize();
|
|
55
|
+
*
|
|
56
|
+
* // 启动二维码扫描
|
|
57
|
+
* const videoElement = document.getElementById('video');
|
|
58
|
+
* await scanner.startQRScanner(videoElement);
|
|
59
|
+
*
|
|
60
|
+
* // 停止扫描
|
|
61
|
+
* scanner.stop();
|
|
62
|
+
*
|
|
63
|
+
* // 使用结束后释放资源
|
|
64
|
+
* scanner.terminate();
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export declare class IDScanner {
|
|
68
|
+
private options;
|
|
69
|
+
private qrScanner;
|
|
70
|
+
private barcodeScanner;
|
|
71
|
+
private idDetector;
|
|
72
|
+
private ocrProcessor;
|
|
73
|
+
private scanMode;
|
|
74
|
+
/**
|
|
75
|
+
* 创建IDScanner实例
|
|
76
|
+
* @param {IDScannerOptions} [options] - 配置选项
|
|
77
|
+
*/
|
|
78
|
+
constructor(options?: IDScannerOptions);
|
|
79
|
+
/**
|
|
80
|
+
* 初始化OCR引擎和相关资源
|
|
81
|
+
*
|
|
82
|
+
* @returns {Promise<void>} 初始化完成的Promise
|
|
83
|
+
* @throws 如果初始化失败,将抛出错误
|
|
84
|
+
*/
|
|
85
|
+
initialize(): Promise<void>;
|
|
86
|
+
/**
|
|
87
|
+
* 启动二维码扫描
|
|
88
|
+
*
|
|
89
|
+
* @param {HTMLVideoElement} videoElement - 用于显示相机画面的video元素
|
|
90
|
+
* @returns {Promise<void>} 启动完成的Promise
|
|
91
|
+
*/
|
|
92
|
+
startQRScanner(videoElement: HTMLVideoElement): Promise<void>;
|
|
93
|
+
/**
|
|
94
|
+
* 启动条形码扫描
|
|
95
|
+
*
|
|
96
|
+
* @param {HTMLVideoElement} videoElement - 用于显示相机画面的video元素
|
|
97
|
+
* @returns {Promise<void>} 启动完成的Promise
|
|
98
|
+
*/
|
|
99
|
+
startBarcodeScanner(videoElement: HTMLVideoElement): Promise<void>;
|
|
100
|
+
/**
|
|
101
|
+
* 启动身份证扫描识别
|
|
102
|
+
*
|
|
103
|
+
* @param {HTMLVideoElement} videoElement - 用于显示相机画面的video元素
|
|
104
|
+
* @returns {Promise<void>} 启动完成的Promise
|
|
105
|
+
*/
|
|
106
|
+
startIDCardScanner(videoElement: HTMLVideoElement): Promise<void>;
|
|
107
|
+
/**
|
|
108
|
+
* 停止当前扫描
|
|
109
|
+
*/
|
|
110
|
+
stop(): void;
|
|
111
|
+
/**
|
|
112
|
+
* 处理二维码扫描结果
|
|
113
|
+
* @private
|
|
114
|
+
* @param {string} result - 扫描到的二维码内容
|
|
115
|
+
*/
|
|
116
|
+
private handleQRScan;
|
|
117
|
+
/**
|
|
118
|
+
* 处理条形码扫描结果
|
|
119
|
+
* @private
|
|
120
|
+
* @param {string} result - 扫描到的条形码内容
|
|
121
|
+
*/
|
|
122
|
+
private handleBarcodeScan;
|
|
123
|
+
/**
|
|
124
|
+
* 处理身份证检测结果
|
|
125
|
+
* @private
|
|
126
|
+
* @param {DetectionResult} result - 身份证检测结果
|
|
127
|
+
*/
|
|
128
|
+
private handleIDDetection;
|
|
129
|
+
/**
|
|
130
|
+
* 处理错误
|
|
131
|
+
* @private
|
|
132
|
+
* @param {Error} error - 错误对象
|
|
133
|
+
*/
|
|
134
|
+
private handleError;
|
|
135
|
+
/**
|
|
136
|
+
* 终止所有扫描并释放资源
|
|
137
|
+
*
|
|
138
|
+
* @returns {Promise<void>} 资源释放完成的Promise
|
|
139
|
+
*/
|
|
140
|
+
terminate(): Promise<void>;
|
|
141
|
+
}
|
|
142
|
+
export { Camera, QRScanner, BarcodeScanner, IDCardDetector, OCRProcessor, DataExtractor, ImageProcessor };
|
|
143
|
+
export type { CameraOptions, QRScannerOptions, BarcodeScannerOptions, IDCardInfo, DetectionResult };
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file 条形码扫描模块
|
|
3
|
+
* @description 提供实时条形码扫描和识别功能
|
|
4
|
+
* @module BarcodeScanner
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* 条形码扫描器配置选项
|
|
8
|
+
*
|
|
9
|
+
* @interface BarcodeScannerOptions
|
|
10
|
+
* @property {number} [scanInterval] - 扫描间隔时间(毫秒),默认为200ms
|
|
11
|
+
* @property {Function} [onScan] - 扫描成功回调函数
|
|
12
|
+
* @property {Function} [onError] - 错误处理回调函数
|
|
13
|
+
*/
|
|
14
|
+
export interface BarcodeScannerOptions {
|
|
15
|
+
scanInterval?: number;
|
|
16
|
+
onScan?: (result: string) => void;
|
|
17
|
+
onError?: (error: Error) => void;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* 条形码扫描器类
|
|
21
|
+
*
|
|
22
|
+
* 提供实时扫描和识别摄像头中的条形码的功能
|
|
23
|
+
* 注意:当前实现是简化版,实际项目中建议集成专门的条形码识别库如ZXing或Quagga.js
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* // 创建条形码扫描器
|
|
28
|
+
* const barcodeScanner = new BarcodeScanner({
|
|
29
|
+
* scanInterval: 100, // 每100ms扫描一次
|
|
30
|
+
* onScan: (result) => {
|
|
31
|
+
* console.log('扫描到条形码:', result);
|
|
32
|
+
* },
|
|
33
|
+
* onError: (error) => {
|
|
34
|
+
* console.error('扫描错误:', error);
|
|
35
|
+
* }
|
|
36
|
+
* });
|
|
37
|
+
*
|
|
38
|
+
* // 启动扫描
|
|
39
|
+
* const videoElement = document.getElementById('video') as HTMLVideoElement;
|
|
40
|
+
* await barcodeScanner.start(videoElement);
|
|
41
|
+
*
|
|
42
|
+
* // 停止扫描
|
|
43
|
+
* barcodeScanner.stop();
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export declare class BarcodeScanner {
|
|
47
|
+
private options;
|
|
48
|
+
private camera;
|
|
49
|
+
private scanning;
|
|
50
|
+
private scanTimer;
|
|
51
|
+
/**
|
|
52
|
+
* 创建条形码扫描器实例
|
|
53
|
+
*
|
|
54
|
+
* @param {BarcodeScannerOptions} [options] - 扫描器配置选项
|
|
55
|
+
*/
|
|
56
|
+
constructor(options?: BarcodeScannerOptions);
|
|
57
|
+
/**
|
|
58
|
+
* 启动条形码扫描
|
|
59
|
+
*
|
|
60
|
+
* 初始化相机并开始连续扫描视频帧中的条形码
|
|
61
|
+
*
|
|
62
|
+
* @param {HTMLVideoElement} videoElement - 用于显示相机画面的video元素
|
|
63
|
+
* @returns {Promise<void>} 启动完成的Promise
|
|
64
|
+
* @throws 如果无法访问相机,将通过onError回调报告错误
|
|
65
|
+
*/
|
|
66
|
+
start(videoElement: HTMLVideoElement): Promise<void>;
|
|
67
|
+
/**
|
|
68
|
+
* 执行一次条形码扫描
|
|
69
|
+
*
|
|
70
|
+
* 内部方法,捕获当前视频帧并尝试识别其中的条形码
|
|
71
|
+
*
|
|
72
|
+
* @private
|
|
73
|
+
*/
|
|
74
|
+
private scan;
|
|
75
|
+
/**
|
|
76
|
+
* 条形码检测方法
|
|
77
|
+
*
|
|
78
|
+
* 注意:这是一个简化实现,实际需要集成专门的条形码识别库
|
|
79
|
+
*
|
|
80
|
+
* @private
|
|
81
|
+
* @param {ImageData} imageData - 要检测条形码的图像数据
|
|
82
|
+
*/
|
|
83
|
+
private detectBarcode;
|
|
84
|
+
/**
|
|
85
|
+
* 停止条形码扫描
|
|
86
|
+
*
|
|
87
|
+
* 停止扫描循环并释放相机资源
|
|
88
|
+
*/
|
|
89
|
+
stop(): void;
|
|
90
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file 二维码扫描模块
|
|
3
|
+
* @description 提供实时二维码扫描和识别功能
|
|
4
|
+
* @module QRScanner
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* 二维码扫描器配置选项
|
|
8
|
+
*
|
|
9
|
+
* @interface QRScannerOptions
|
|
10
|
+
* @property {number} [scanInterval] - 扫描间隔时间(毫秒),默认为200ms
|
|
11
|
+
* @property {Function} [onScan] - 扫描成功回调函数
|
|
12
|
+
* @property {Function} [onError] - 错误处理回调函数
|
|
13
|
+
*/
|
|
14
|
+
export interface QRScannerOptions {
|
|
15
|
+
scanInterval?: number;
|
|
16
|
+
onScan?: (result: string) => void;
|
|
17
|
+
onError?: (error: Error) => void;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* 二维码扫描器类
|
|
21
|
+
*
|
|
22
|
+
* 提供实时扫描和识别摄像头中的二维码的功能
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* // 创建二维码扫描器
|
|
27
|
+
* const qrScanner = new QRScanner({
|
|
28
|
+
* scanInterval: 100, // 每100ms扫描一次
|
|
29
|
+
* onScan: (result) => {
|
|
30
|
+
* console.log('扫描到二维码:', result);
|
|
31
|
+
* },
|
|
32
|
+
* onError: (error) => {
|
|
33
|
+
* console.error('扫描错误:', error);
|
|
34
|
+
* }
|
|
35
|
+
* });
|
|
36
|
+
*
|
|
37
|
+
* // 启动扫描
|
|
38
|
+
* const videoElement = document.getElementById('video') as HTMLVideoElement;
|
|
39
|
+
* await qrScanner.start(videoElement);
|
|
40
|
+
*
|
|
41
|
+
* // 停止扫描
|
|
42
|
+
* qrScanner.stop();
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export declare class QRScanner {
|
|
46
|
+
private options;
|
|
47
|
+
private camera;
|
|
48
|
+
private scanning;
|
|
49
|
+
private scanTimer;
|
|
50
|
+
/**
|
|
51
|
+
* 创建二维码扫描器实例
|
|
52
|
+
*
|
|
53
|
+
* @param {QRScannerOptions} [options] - 扫描器配置选项
|
|
54
|
+
*/
|
|
55
|
+
constructor(options?: QRScannerOptions);
|
|
56
|
+
/**
|
|
57
|
+
* 启动二维码扫描
|
|
58
|
+
*
|
|
59
|
+
* 初始化相机并开始连续扫描视频帧中的二维码
|
|
60
|
+
*
|
|
61
|
+
* @param {HTMLVideoElement} videoElement - 用于显示相机画面的video元素
|
|
62
|
+
* @returns {Promise<void>} 启动完成的Promise
|
|
63
|
+
* @throws 如果无法访问相机,将通过onError回调报告错误
|
|
64
|
+
*/
|
|
65
|
+
start(videoElement: HTMLVideoElement): Promise<void>;
|
|
66
|
+
/**
|
|
67
|
+
* 执行一次二维码扫描
|
|
68
|
+
*
|
|
69
|
+
* 内部方法,捕获当前视频帧并尝试识别其中的二维码
|
|
70
|
+
*
|
|
71
|
+
* @private
|
|
72
|
+
*/
|
|
73
|
+
private scan;
|
|
74
|
+
/**
|
|
75
|
+
* 停止二维码扫描
|
|
76
|
+
*
|
|
77
|
+
* 停止扫描循环并释放相机资源
|
|
78
|
+
*/
|
|
79
|
+
stop(): void;
|
|
80
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file 相机工具类
|
|
3
|
+
* @description 提供访问和控制设备摄像头的功能
|
|
4
|
+
* @module Camera
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* 相机配置选项接口
|
|
8
|
+
*
|
|
9
|
+
* @interface CameraOptions
|
|
10
|
+
* @property {number} [width] - 视频宽度,默认为640
|
|
11
|
+
* @property {number} [height] - 视频高度,默认为480
|
|
12
|
+
* @property {string} [facingMode] - 摄像头朝向,'user'为前置摄像头,'environment'为后置摄像头,默认为'environment'
|
|
13
|
+
*/
|
|
14
|
+
export interface CameraOptions {
|
|
15
|
+
width?: number;
|
|
16
|
+
height?: number;
|
|
17
|
+
facingMode?: 'user' | 'environment';
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* 相机工具类
|
|
21
|
+
*
|
|
22
|
+
* 提供访问设备摄像头、获取视频流以及捕获图像帧的功能
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* // 创建相机实例
|
|
27
|
+
* const camera = new Camera({
|
|
28
|
+
* width: 1280,
|
|
29
|
+
* height: 720,
|
|
30
|
+
* facingMode: 'environment' // 使用后置摄像头
|
|
31
|
+
* });
|
|
32
|
+
*
|
|
33
|
+
* // 初始化相机
|
|
34
|
+
* const videoElement = document.getElementById('video') as HTMLVideoElement;
|
|
35
|
+
* await camera.initialize(videoElement);
|
|
36
|
+
*
|
|
37
|
+
* // 捕获当前视频帧
|
|
38
|
+
* const imageData = camera.captureFrame();
|
|
39
|
+
*
|
|
40
|
+
* // 使用结束后释放资源
|
|
41
|
+
* camera.release();
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export declare class Camera {
|
|
45
|
+
private options;
|
|
46
|
+
private stream;
|
|
47
|
+
private videoElement;
|
|
48
|
+
/**
|
|
49
|
+
* 创建相机实例
|
|
50
|
+
*
|
|
51
|
+
* @param {CameraOptions} [options] - 相机配置选项
|
|
52
|
+
*/
|
|
53
|
+
constructor(options?: CameraOptions);
|
|
54
|
+
/**
|
|
55
|
+
* 初始化相机,请求摄像头权限并设置视频流
|
|
56
|
+
*
|
|
57
|
+
* @param {HTMLVideoElement} videoElement - 用于显示相机画面的video元素
|
|
58
|
+
* @returns {Promise<void>} 初始化完成的Promise
|
|
59
|
+
* @throws 如果无法访问相机,将抛出错误
|
|
60
|
+
*/
|
|
61
|
+
initialize(videoElement: HTMLVideoElement): Promise<void>;
|
|
62
|
+
/**
|
|
63
|
+
* 获取当前视频帧
|
|
64
|
+
*
|
|
65
|
+
* 捕获当前视频画面并转换为ImageData对象,可用于图像处理和分析
|
|
66
|
+
*
|
|
67
|
+
* @returns {ImageData|null} 当前视频帧的ImageData对象,如果未初始化视频则返回null
|
|
68
|
+
*/
|
|
69
|
+
captureFrame(): ImageData | null;
|
|
70
|
+
/**
|
|
71
|
+
* 释放相机资源
|
|
72
|
+
*
|
|
73
|
+
* 停止所有视频流轨道并清理资源。在不再需要相机时应调用此方法。
|
|
74
|
+
*/
|
|
75
|
+
release(): void;
|
|
76
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file 图像处理工具类
|
|
3
|
+
* @description 提供图像处理相关的辅助功能
|
|
4
|
+
* @module ImageProcessor
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* 图像处理工具类
|
|
8
|
+
*
|
|
9
|
+
* 提供常用的图像处理功能,如亮度和对比度调整、灰度转换、图像大小调整等。
|
|
10
|
+
* 这些功能可用于增强图像质量,提高OCR和扫描的识别率。
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* // 使用图像处理功能增强图像
|
|
15
|
+
* const enhancedImage = ImageProcessor.adjustBrightnessContrast(
|
|
16
|
+
* originalImageData,
|
|
17
|
+
* 15, // 增加亮度
|
|
18
|
+
* 25 // 增加对比度
|
|
19
|
+
* );
|
|
20
|
+
*
|
|
21
|
+
* // 转换为灰度图像
|
|
22
|
+
* const grayImage = ImageProcessor.toGrayscale(originalImageData);
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export declare class ImageProcessor {
|
|
26
|
+
/**
|
|
27
|
+
* 将ImageData转换为Canvas元素
|
|
28
|
+
*
|
|
29
|
+
* @param {ImageData} imageData - 要转换的图像数据
|
|
30
|
+
* @returns {HTMLCanvasElement} 包含图像的Canvas元素
|
|
31
|
+
*/
|
|
32
|
+
static imageDataToCanvas(imageData: ImageData): HTMLCanvasElement;
|
|
33
|
+
/**
|
|
34
|
+
* 将Canvas转换为ImageData
|
|
35
|
+
*
|
|
36
|
+
* @param {HTMLCanvasElement} canvas - 要转换的Canvas元素
|
|
37
|
+
* @returns {ImageData|null} Canvas的图像数据,如果获取失败则返回null
|
|
38
|
+
*/
|
|
39
|
+
static canvasToImageData(canvas: HTMLCanvasElement): ImageData | null;
|
|
40
|
+
/**
|
|
41
|
+
* 调整图像亮度和对比度
|
|
42
|
+
*
|
|
43
|
+
* @param {ImageData} imageData - 要处理的图像数据
|
|
44
|
+
* @param {number} [brightness=0] - 亮度调整值,正值增加亮度,负值降低亮度,范围建议为-100到100
|
|
45
|
+
* @param {number} [contrast=0] - 对比度调整值,正值增加对比度,负值降低对比度,范围建议为-100到100
|
|
46
|
+
* @returns {ImageData} 处理后的图像数据
|
|
47
|
+
*/
|
|
48
|
+
static adjustBrightnessContrast(imageData: ImageData, brightness?: number, contrast?: number): ImageData;
|
|
49
|
+
/**
|
|
50
|
+
* 确保值在0-255范围内
|
|
51
|
+
*
|
|
52
|
+
* @private
|
|
53
|
+
* @param {number} value - 要截断的值
|
|
54
|
+
* @returns {number} 截断后的值,范围为0-255
|
|
55
|
+
*/
|
|
56
|
+
private static truncate;
|
|
57
|
+
/**
|
|
58
|
+
* 将彩色图像转换为灰度图像
|
|
59
|
+
*
|
|
60
|
+
* 灰度转换可以简化图像,提高OCR和条形码识别的准确率
|
|
61
|
+
*
|
|
62
|
+
* @param {ImageData} imageData - 要转换的彩色图像
|
|
63
|
+
* @returns {ImageData} 转换后的灰度图像
|
|
64
|
+
*/
|
|
65
|
+
static toGrayscale(imageData: ImageData): ImageData;
|
|
66
|
+
/**
|
|
67
|
+
* 调整图像大小
|
|
68
|
+
*
|
|
69
|
+
* @param {ImageData} imageData - 原图像数据
|
|
70
|
+
* @param {number} newWidth - 新宽度
|
|
71
|
+
* @param {number} newHeight - 新高度
|
|
72
|
+
* @returns {ImageData} 调整大小后的图像数据
|
|
73
|
+
*/
|
|
74
|
+
static resize(imageData: ImageData, newWidth: number, newHeight: number): ImageData;
|
|
75
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file 类型定义文件
|
|
3
|
+
* @description 定义库中使用的公共类型和接口
|
|
4
|
+
* @module Types
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* 身份证检测结果接口
|
|
8
|
+
*
|
|
9
|
+
* 包含身份证检测的结果信息,如是否成功检测到身份证、身份证的四个角点坐标以及裁剪后的身份证图像
|
|
10
|
+
*
|
|
11
|
+
* @interface DetectionResult
|
|
12
|
+
* @property {boolean} success - 是否成功检测到身份证
|
|
13
|
+
* @property {Object[]} [corners] - 检测到的身份证四个角点坐标
|
|
14
|
+
* @property {number} corners[].x - 角点X坐标
|
|
15
|
+
* @property {number} corners[].y - 角点Y坐标
|
|
16
|
+
* @property {ImageData} [croppedImage] - 裁剪后的身份证图像
|
|
17
|
+
*/
|
|
18
|
+
export interface DetectionResult {
|
|
19
|
+
success: boolean;
|
|
20
|
+
corners?: {
|
|
21
|
+
x: number;
|
|
22
|
+
y: number;
|
|
23
|
+
}[];
|
|
24
|
+
croppedImage?: ImageData;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* 身份证信息接口
|
|
28
|
+
*
|
|
29
|
+
* 包含从身份证中提取的各项个人信息
|
|
30
|
+
*
|
|
31
|
+
* @interface IDCardInfo
|
|
32
|
+
* @property {string} [name] - 姓名
|
|
33
|
+
* @property {string} [gender] - 性别,通常为"男"或"女"
|
|
34
|
+
* @property {string} [nationality] - 民族,如"汉族"、"满族"等
|
|
35
|
+
* @property {string} [birthDate] - 出生日期,格式为"YYYY-MM-DD"
|
|
36
|
+
* @property {string} [address] - 住址
|
|
37
|
+
* @property {string} [idNumber] - 身份证号码,18位
|
|
38
|
+
* @property {string} [issuingAuthority] - 签发机关
|
|
39
|
+
* @property {string} [validPeriod] - 有效期限
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* // 身份证信息示例
|
|
44
|
+
* const idInfo: IDCardInfo = {
|
|
45
|
+
* name: '张三',
|
|
46
|
+
* gender: '男',
|
|
47
|
+
* nationality: '汉族',
|
|
48
|
+
* birthDate: '1990-01-01',
|
|
49
|
+
* address: '北京市海淀区xxxxx',
|
|
50
|
+
* idNumber: '110101199001011234',
|
|
51
|
+
* issuingAuthority: '北京市公安局海淀分局',
|
|
52
|
+
* validPeriod: '2020.01.01-2040.01.01'
|
|
53
|
+
* };
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export interface IDCardInfo {
|
|
57
|
+
name?: string;
|
|
58
|
+
gender?: string;
|
|
59
|
+
nationality?: string;
|
|
60
|
+
birthDate?: string;
|
|
61
|
+
address?: string;
|
|
62
|
+
idNumber?: string;
|
|
63
|
+
issuingAuthority?: string;
|
|
64
|
+
validPeriod?: string;
|
|
65
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "id-scanner-lib",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "dist/id-scanner.js",
|
|
6
|
+
"module": "dist/id-scanner.esm.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "rollup -c",
|
|
10
|
+
"dev": "rollup -c -w",
|
|
11
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
12
|
+
"prepublishOnly": "npm run build"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"id-card",
|
|
16
|
+
"qr-code",
|
|
17
|
+
"scanner",
|
|
18
|
+
"ocr"
|
|
19
|
+
],
|
|
20
|
+
"author": "agions",
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"description": "一款纯前端实现的TypeScript身份证&二维码识别库,无需后端支持,所有处理在浏览器端完成",
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/agions/id-scanner-lib.git"
|
|
26
|
+
},
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/agions/id-scanner-lib/issues"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://github.com/agions/id-scanner-lib#readme",
|
|
31
|
+
"files": [
|
|
32
|
+
"dist",
|
|
33
|
+
"src",
|
|
34
|
+
"README.md",
|
|
35
|
+
"LICENSE"
|
|
36
|
+
],
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@rollup/plugin-commonjs": "^28.0.3",
|
|
42
|
+
"@rollup/plugin-json": "^6.1.0",
|
|
43
|
+
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
44
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
45
|
+
"@rollup/plugin-typescript": "^12.1.2",
|
|
46
|
+
"@types/node": "^22.13.10",
|
|
47
|
+
"rollup": "^4.35.0",
|
|
48
|
+
"tslib": "^2.8.1",
|
|
49
|
+
"typescript": "^5.8.2"
|
|
50
|
+
},
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"jsqr": "^1.4.0",
|
|
53
|
+
"lodash-es": "^4.17.21",
|
|
54
|
+
"tesseract.js": "^6.0.0"
|
|
55
|
+
}
|
|
56
|
+
}
|
package/src/demo/demo.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { IDScanner, IDCardInfo } from '../index';
|
|
2
|
+
|
|
3
|
+
export class IDScannerDemo {
|
|
4
|
+
private scanner: IDScanner;
|
|
5
|
+
private videoElement: HTMLVideoElement;
|
|
6
|
+
private resultContainer: HTMLElement;
|
|
7
|
+
private switchButton: HTMLButtonElement;
|
|
8
|
+
|
|
9
|
+
constructor(videoElementId: string, resultContainerId: string, switchButtonId: string) {
|
|
10
|
+
this.videoElement = document.getElementById(videoElementId) as HTMLVideoElement;
|
|
11
|
+
this.resultContainer = document.getElementById(resultContainerId) as HTMLElement;
|
|
12
|
+
this.switchButton = document.getElementById(switchButtonId) as HTMLButtonElement;
|
|
13
|
+
|
|
14
|
+
this.scanner = new IDScanner({
|
|
15
|
+
onQRCodeScanned: this.handleQRCodeResult.bind(this),
|
|
16
|
+
onBarcodeScanned: this.handleQRCodeResult.bind(this), // 复用QR码结果处理
|
|
17
|
+
onIDCardScanned: this.handleIDCardResult.bind(this),
|
|
18
|
+
onError: this.handleError.bind(this)
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
this.switchButton.addEventListener('click', this.toggleScanMode.bind(this));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async initialize(): Promise<void> {
|
|
25
|
+
await this.scanner.initialize();
|
|
26
|
+
await this.scanner.startQRScanner(this.videoElement);
|
|
27
|
+
this.switchButton.textContent = '切换到身份证模式';
|
|
28
|
+
this.currentMode = 'qr';
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
private currentMode: 'qr' | 'barcode' | 'idcard' = 'qr';
|
|
32
|
+
|
|
33
|
+
private async toggleScanMode(): Promise<void> {
|
|
34
|
+
this.scanner.stop();
|
|
35
|
+
|
|
36
|
+
if (this.currentMode === 'qr') {
|
|
37
|
+
this.currentMode = 'barcode';
|
|
38
|
+
await this.scanner.startBarcodeScanner(this.videoElement);
|
|
39
|
+
this.switchButton.textContent = '切换到身份证模式';
|
|
40
|
+
} else if (this.currentMode === 'barcode') {
|
|
41
|
+
this.currentMode = 'idcard';
|
|
42
|
+
await this.scanner.startIDCardScanner(this.videoElement);
|
|
43
|
+
this.switchButton.textContent = '切换到二维码模式';
|
|
44
|
+
} else {
|
|
45
|
+
this.currentMode = 'qr';
|
|
46
|
+
await this.scanner.startQRScanner(this.videoElement);
|
|
47
|
+
this.switchButton.textContent = '切换到条形码模式';
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
this.resultContainer.innerHTML = '';
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
private handleQRCodeResult(result: string): void {
|
|
54
|
+
this.resultContainer.innerHTML = `
|
|
55
|
+
<h3>扫描结果:</h3>
|
|
56
|
+
<p>${result}</p>
|
|
57
|
+
`;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
private handleIDCardResult(info: IDCardInfo): void {
|
|
61
|
+
this.resultContainer.innerHTML = `
|
|
62
|
+
<h3>身份证信息:</h3>
|
|
63
|
+
<p>姓名: ${info.name || '未识别'}</p>
|
|
64
|
+
<p>性别: ${info.gender || '未识别'}</p>
|
|
65
|
+
<p>民族: ${info.nationality || '未识别'}</p>
|
|
66
|
+
<p>出生日期: ${info.birthDate || '未识别'}</p>
|
|
67
|
+
<p>地址: ${info.address || '未识别'}</p>
|
|
68
|
+
<p>身份证号: ${info.idNumber || '未识别'}</p>
|
|
69
|
+
<p>签发机关: ${info.issuingAuthority || '未识别'}</p>
|
|
70
|
+
<p>有效期限: ${info.validPeriod || '未识别'}</p>
|
|
71
|
+
`;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
private handleError(error: Error): void {
|
|
75
|
+
console.error('扫描错误:', error);
|
|
76
|
+
this.resultContainer.innerHTML = `
|
|
77
|
+
<div class="error">
|
|
78
|
+
<h3>错误:</h3>
|
|
79
|
+
<p>${error.message}</p>
|
|
80
|
+
</div>
|
|
81
|
+
`;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
stop(): void {
|
|
85
|
+
this.scanner.stop();
|
|
86
|
+
this.scanner.terminate();
|
|
87
|
+
}
|
|
88
|
+
}
|