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
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file 图像处理工具类
|
|
3
|
+
* @description 提供图像处理相关的辅助功能
|
|
4
|
+
* @module ImageProcessor
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 图像处理工具类
|
|
9
|
+
*
|
|
10
|
+
* 提供常用的图像处理功能,如亮度和对比度调整、灰度转换、图像大小调整等。
|
|
11
|
+
* 这些功能可用于增强图像质量,提高OCR和扫描的识别率。
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* // 使用图像处理功能增强图像
|
|
16
|
+
* const enhancedImage = ImageProcessor.adjustBrightnessContrast(
|
|
17
|
+
* originalImageData,
|
|
18
|
+
* 15, // 增加亮度
|
|
19
|
+
* 25 // 增加对比度
|
|
20
|
+
* );
|
|
21
|
+
*
|
|
22
|
+
* // 转换为灰度图像
|
|
23
|
+
* const grayImage = ImageProcessor.toGrayscale(originalImageData);
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export class ImageProcessor {
|
|
27
|
+
/**
|
|
28
|
+
* 将ImageData转换为Canvas元素
|
|
29
|
+
*
|
|
30
|
+
* @param {ImageData} imageData - 要转换的图像数据
|
|
31
|
+
* @returns {HTMLCanvasElement} 包含图像的Canvas元素
|
|
32
|
+
*/
|
|
33
|
+
static imageDataToCanvas(imageData: ImageData): HTMLCanvasElement {
|
|
34
|
+
const canvas = document.createElement('canvas');
|
|
35
|
+
canvas.width = imageData.width;
|
|
36
|
+
canvas.height = imageData.height;
|
|
37
|
+
const ctx = canvas.getContext('2d');
|
|
38
|
+
|
|
39
|
+
if (ctx) {
|
|
40
|
+
ctx.putImageData(imageData, 0, 0);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return canvas;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 将Canvas转换为ImageData
|
|
48
|
+
*
|
|
49
|
+
* @param {HTMLCanvasElement} canvas - 要转换的Canvas元素
|
|
50
|
+
* @returns {ImageData|null} Canvas的图像数据,如果获取失败则返回null
|
|
51
|
+
*/
|
|
52
|
+
static canvasToImageData(canvas: HTMLCanvasElement): ImageData | null {
|
|
53
|
+
const ctx = canvas.getContext('2d');
|
|
54
|
+
return ctx ? ctx.getImageData(0, 0, canvas.width, canvas.height) : null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 调整图像亮度和对比度
|
|
59
|
+
*
|
|
60
|
+
* @param {ImageData} imageData - 要处理的图像数据
|
|
61
|
+
* @param {number} [brightness=0] - 亮度调整值,正值增加亮度,负值降低亮度,范围建议为-100到100
|
|
62
|
+
* @param {number} [contrast=0] - 对比度调整值,正值增加对比度,负值降低对比度,范围建议为-100到100
|
|
63
|
+
* @returns {ImageData} 处理后的图像数据
|
|
64
|
+
*/
|
|
65
|
+
static adjustBrightnessContrast(imageData: ImageData, brightness: number = 0, contrast: number = 0): ImageData {
|
|
66
|
+
const canvas = this.imageDataToCanvas(imageData);
|
|
67
|
+
const ctx = canvas.getContext('2d');
|
|
68
|
+
|
|
69
|
+
if (ctx) {
|
|
70
|
+
const imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
|
|
71
|
+
const data = imgData.data;
|
|
72
|
+
|
|
73
|
+
// 调整对比度算法
|
|
74
|
+
const factor = (259 * (contrast + 255)) / (255 * (259 - contrast));
|
|
75
|
+
|
|
76
|
+
for (let i = 0; i < data.length; i += 4) {
|
|
77
|
+
// 红色
|
|
78
|
+
data[i] = this.truncate(factor * (data[i] - 128) + 128 + brightness);
|
|
79
|
+
// 绿色
|
|
80
|
+
data[i + 1] = this.truncate(factor * (data[i + 1] - 128) + 128 + brightness);
|
|
81
|
+
// 蓝色
|
|
82
|
+
data[i + 2] = this.truncate(factor * (data[i + 2] - 128) + 128 + brightness);
|
|
83
|
+
// Alpha不变
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
ctx.putImageData(imgData, 0, 0);
|
|
87
|
+
return imgData;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return imageData;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* 确保值在0-255范围内
|
|
95
|
+
*
|
|
96
|
+
* @private
|
|
97
|
+
* @param {number} value - 要截断的值
|
|
98
|
+
* @returns {number} 截断后的值,范围为0-255
|
|
99
|
+
*/
|
|
100
|
+
private static truncate(value: number): number {
|
|
101
|
+
return Math.min(255, Math.max(0, value));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* 将彩色图像转换为灰度图像
|
|
106
|
+
*
|
|
107
|
+
* 灰度转换可以简化图像,提高OCR和条形码识别的准确率
|
|
108
|
+
*
|
|
109
|
+
* @param {ImageData} imageData - 要转换的彩色图像
|
|
110
|
+
* @returns {ImageData} 转换后的灰度图像
|
|
111
|
+
*/
|
|
112
|
+
static toGrayscale(imageData: ImageData): ImageData {
|
|
113
|
+
const canvas = this.imageDataToCanvas(imageData);
|
|
114
|
+
const ctx = canvas.getContext('2d');
|
|
115
|
+
|
|
116
|
+
if (ctx) {
|
|
117
|
+
const imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
|
|
118
|
+
const data = imgData.data;
|
|
119
|
+
|
|
120
|
+
for (let i = 0; i < data.length; i += 4) {
|
|
121
|
+
const avg = (data[i] + data[i + 1] + data[i + 2]) / 3;
|
|
122
|
+
data[i] = avg; // 红色
|
|
123
|
+
data[i + 1] = avg; // 绿色
|
|
124
|
+
data[i + 2] = avg; // 蓝色
|
|
125
|
+
// Alpha不变
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
ctx.putImageData(imgData, 0, 0);
|
|
129
|
+
return imgData;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return imageData;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* 调整图像大小
|
|
137
|
+
*
|
|
138
|
+
* @param {ImageData} imageData - 原图像数据
|
|
139
|
+
* @param {number} newWidth - 新宽度
|
|
140
|
+
* @param {number} newHeight - 新高度
|
|
141
|
+
* @returns {ImageData} 调整大小后的图像数据
|
|
142
|
+
*/
|
|
143
|
+
static resize(imageData: ImageData, newWidth: number, newHeight: number): ImageData {
|
|
144
|
+
const canvas = document.createElement('canvas');
|
|
145
|
+
canvas.width = newWidth;
|
|
146
|
+
canvas.height = newHeight;
|
|
147
|
+
const ctx = canvas.getContext('2d');
|
|
148
|
+
|
|
149
|
+
if (ctx) {
|
|
150
|
+
const tempCanvas = this.imageDataToCanvas(imageData);
|
|
151
|
+
ctx.drawImage(tempCanvas, 0, 0, imageData.width, imageData.height, 0, 0, newWidth, newHeight);
|
|
152
|
+
return ctx.getImageData(0, 0, newWidth, newHeight);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return imageData;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file 类型定义文件
|
|
3
|
+
* @description 定义库中使用的公共类型和接口
|
|
4
|
+
* @module Types
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 身份证检测结果接口
|
|
9
|
+
*
|
|
10
|
+
* 包含身份证检测的结果信息,如是否成功检测到身份证、身份证的四个角点坐标以及裁剪后的身份证图像
|
|
11
|
+
*
|
|
12
|
+
* @interface DetectionResult
|
|
13
|
+
* @property {boolean} success - 是否成功检测到身份证
|
|
14
|
+
* @property {Object[]} [corners] - 检测到的身份证四个角点坐标
|
|
15
|
+
* @property {number} corners[].x - 角点X坐标
|
|
16
|
+
* @property {number} corners[].y - 角点Y坐标
|
|
17
|
+
* @property {ImageData} [croppedImage] - 裁剪后的身份证图像
|
|
18
|
+
*/
|
|
19
|
+
export interface DetectionResult {
|
|
20
|
+
success: boolean;
|
|
21
|
+
corners?: { x: number; y: number }[];
|
|
22
|
+
croppedImage?: ImageData;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 身份证信息接口
|
|
27
|
+
*
|
|
28
|
+
* 包含从身份证中提取的各项个人信息
|
|
29
|
+
*
|
|
30
|
+
* @interface IDCardInfo
|
|
31
|
+
* @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位
|
|
37
|
+
* @property {string} [issuingAuthority] - 签发机关
|
|
38
|
+
* @property {string} [validPeriod] - 有效期限
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* // 身份证信息示例
|
|
43
|
+
* const idInfo: IDCardInfo = {
|
|
44
|
+
* name: '张三',
|
|
45
|
+
* gender: '男',
|
|
46
|
+
* nationality: '汉族',
|
|
47
|
+
* birthDate: '1990-01-01',
|
|
48
|
+
* address: '北京市海淀区xxxxx',
|
|
49
|
+
* idNumber: '110101199001011234',
|
|
50
|
+
* issuingAuthority: '北京市公安局海淀分局',
|
|
51
|
+
* validPeriod: '2020.01.01-2040.01.01'
|
|
52
|
+
* };
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export interface IDCardInfo {
|
|
56
|
+
name?: string;
|
|
57
|
+
gender?: string;
|
|
58
|
+
nationality?: string;
|
|
59
|
+
birthDate?: string;
|
|
60
|
+
address?: string;
|
|
61
|
+
idNumber?: string;
|
|
62
|
+
issuingAuthority?: string;
|
|
63
|
+
validPeriod?: string;
|
|
64
|
+
}
|