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,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file 数据提取工具类
|
|
3
|
+
* @description 提供身份证信息的验证和格式化功能
|
|
4
|
+
* @module DataExtractor
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { IDCardInfo } from '../utils/types';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 数据提取工具类
|
|
11
|
+
*
|
|
12
|
+
* 提供身份证信息的验证、提取和增强功能,可以从身份证号码中提取出生日期、性别等信息,
|
|
13
|
+
* 并对OCR识别结果进行补充和验证
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* // 验证身份证号码
|
|
18
|
+
* const isValid = DataExtractor.validateIDNumber('110101199001011234');
|
|
19
|
+
*
|
|
20
|
+
* // 从身份证号码提取出生日期
|
|
21
|
+
* const birthDate = DataExtractor.extractBirthDateFromID('110101199001011234');
|
|
22
|
+
* // 结果: '1990-01-01'
|
|
23
|
+
*
|
|
24
|
+
* // 增强OCR识别结果
|
|
25
|
+
* const enhancedInfo = DataExtractor.enhanceIDCardInfo({
|
|
26
|
+
* name: '张三',
|
|
27
|
+
* idNumber: '110101199001011234'
|
|
28
|
+
* });
|
|
29
|
+
* // 结果会自动补充性别和出生日期
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export class DataExtractor {
|
|
33
|
+
/**
|
|
34
|
+
* 验证身份证号码格式
|
|
35
|
+
*
|
|
36
|
+
* 检查身份证号码的长度、格式和出生日期部分是否有效
|
|
37
|
+
*
|
|
38
|
+
* @param {string} idNumber - 要验证的身份证号码
|
|
39
|
+
* @returns {boolean} 是否是有效的身份证号码
|
|
40
|
+
*/
|
|
41
|
+
static validateIDNumber(idNumber: string): boolean {
|
|
42
|
+
// 简单校验长度
|
|
43
|
+
if (!idNumber || idNumber.length !== 18) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// 校验格式 (前17位必须是数字,最后一位可以是数字或X)
|
|
48
|
+
const pattern = /^\d{17}[\dX]$/;
|
|
49
|
+
if (!pattern.test(idNumber)) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// 校验出生日期
|
|
54
|
+
const year = parseInt(idNumber.substr(6, 4));
|
|
55
|
+
const month = parseInt(idNumber.substr(10, 2));
|
|
56
|
+
const day = parseInt(idNumber.substr(12, 2));
|
|
57
|
+
|
|
58
|
+
const date = new Date(year, month - 1, day);
|
|
59
|
+
if (
|
|
60
|
+
date.getFullYear() !== year ||
|
|
61
|
+
date.getMonth() + 1 !== month ||
|
|
62
|
+
date.getDate() !== day
|
|
63
|
+
) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// 简单的校验规则,实际项目中可以加入更完善的验证
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 从身份证号码提取出生日期
|
|
73
|
+
*
|
|
74
|
+
* @param {string} idNumber - 身份证号码
|
|
75
|
+
* @returns {string|null} 格式化的出生日期(YYYY-MM-DD),如果身份证号码无效则返回null
|
|
76
|
+
*/
|
|
77
|
+
static extractBirthDateFromID(idNumber: string): string | null {
|
|
78
|
+
if (!this.validateIDNumber(idNumber)) {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const year = idNumber.substr(6, 4);
|
|
83
|
+
const month = idNumber.substr(10, 2);
|
|
84
|
+
const day = idNumber.substr(12, 2);
|
|
85
|
+
|
|
86
|
+
return `${year}-${month}-${day}`;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 从身份证号码提取性别
|
|
91
|
+
*
|
|
92
|
+
* 根据身份证号码第17位判断性别,奇数为男,偶数为女
|
|
93
|
+
*
|
|
94
|
+
* @param {string} idNumber - 身份证号码
|
|
95
|
+
* @returns {string|null} '男'或'女',如果身份证号码无效则返回null
|
|
96
|
+
*/
|
|
97
|
+
static extractGenderFromID(idNumber: string): string | null {
|
|
98
|
+
if (!this.validateIDNumber(idNumber)) {
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// 第17位,奇数为男,偶数为女
|
|
103
|
+
const genderCode = parseInt(idNumber.charAt(16));
|
|
104
|
+
return genderCode % 2 === 1 ? '男' : '女';
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* 从身份证号码提取地区编码
|
|
109
|
+
*
|
|
110
|
+
* @param {string} idNumber - 身份证号码
|
|
111
|
+
* @returns {string|null} 地区编码(前6位),如果身份证号码无效则返回null
|
|
112
|
+
*/
|
|
113
|
+
static extractRegionFromID(idNumber: string): string | null {
|
|
114
|
+
if (!this.validateIDNumber(idNumber)) {
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return idNumber.substr(0, 6);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* 合并并优化身份证信息
|
|
123
|
+
*
|
|
124
|
+
* 使用多个来源的数据进行交叉验证和补充,如果OCR识别结果缺少某些信息,
|
|
125
|
+
* 但有身份证号码,则可以从号码中提取出生日期和性别等信息
|
|
126
|
+
*
|
|
127
|
+
* @param {IDCardInfo} ocrInfo - OCR识别到的身份证信息
|
|
128
|
+
* @param {string} [idNumber] - 可选的外部提供的身份证号码,优先级高于OCR识别结果
|
|
129
|
+
* @returns {IDCardInfo} 增强后的身份证信息
|
|
130
|
+
*/
|
|
131
|
+
static enhanceIDCardInfo(ocrInfo: IDCardInfo, idNumber?: string): IDCardInfo {
|
|
132
|
+
const result = { ...ocrInfo };
|
|
133
|
+
|
|
134
|
+
// 如果OCR识别出身份证号,但没有识别出生日期或性别,则从身份证号码提取
|
|
135
|
+
if (result.idNumber && this.validateIDNumber(result.idNumber)) {
|
|
136
|
+
// 从身份证号提取出生日期
|
|
137
|
+
if (!result.birthDate) {
|
|
138
|
+
result.birthDate = this.extractBirthDateFromID(result.idNumber) || undefined;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// 从身份证号提取性别
|
|
142
|
+
if (!result.gender) {
|
|
143
|
+
result.gender = this.extractGenderFromID(result.idNumber) || undefined;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// 如果外部传入了身份证号,则优先使用它并提取信息
|
|
148
|
+
if (idNumber && this.validateIDNumber(idNumber)) {
|
|
149
|
+
result.idNumber = idNumber;
|
|
150
|
+
|
|
151
|
+
// 使用身份证号码再次验证或补充信息
|
|
152
|
+
const birthDate = this.extractBirthDateFromID(idNumber);
|
|
153
|
+
if (birthDate) {
|
|
154
|
+
result.birthDate = birthDate;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const gender = this.extractGenderFromID(idNumber);
|
|
158
|
+
if (gender) {
|
|
159
|
+
result.gender = gender;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return result;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file 身份证检测模块
|
|
3
|
+
* @description 提供自动检测和定位图像中的身份证功能
|
|
4
|
+
* @module IDCardDetector
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { Camera } from '../utils/camera';
|
|
8
|
+
import { ImageProcessor } from '../utils/image-processing';
|
|
9
|
+
import { DetectionResult } from '../utils/types';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 身份证检测器类
|
|
13
|
+
*
|
|
14
|
+
* 通过图像处理和计算机视觉技术,实时检测视频流中的身份证,并提取身份证区域
|
|
15
|
+
* 注意:当前实现是简化版,实际项目中建议使用OpenCV.js进行更精确的检测
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* // 创建身份证检测器
|
|
20
|
+
* const detector = new IDCardDetector((result) => {
|
|
21
|
+
* if (result.success && result.croppedImage) {
|
|
22
|
+
* console.log('检测到身份证!');
|
|
23
|
+
* // 对裁剪出的身份证图像进行处理
|
|
24
|
+
* processIDCardImage(result.croppedImage);
|
|
25
|
+
* }
|
|
26
|
+
* });
|
|
27
|
+
*
|
|
28
|
+
* // 启动检测
|
|
29
|
+
* const videoElement = document.getElementById('video') as HTMLVideoElement;
|
|
30
|
+
* await detector.start(videoElement);
|
|
31
|
+
*
|
|
32
|
+
* // 停止检测
|
|
33
|
+
* detector.stop();
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export class IDCardDetector {
|
|
37
|
+
private camera: Camera;
|
|
38
|
+
private detecting = false;
|
|
39
|
+
private detectTimer: number | null = null;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 创建身份证检测器实例
|
|
43
|
+
*
|
|
44
|
+
* @param {Function} [onDetected] - 身份证检测成功回调函数,接收检测结果对象
|
|
45
|
+
*/
|
|
46
|
+
constructor(private onDetected?: (result: DetectionResult) => void) {
|
|
47
|
+
this.camera = new Camera();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 启动身份证检测
|
|
52
|
+
*
|
|
53
|
+
* 初始化相机并开始连续检测视频帧中的身份证
|
|
54
|
+
*
|
|
55
|
+
* @param {HTMLVideoElement} videoElement - 用于显示相机画面的video元素
|
|
56
|
+
* @returns {Promise<void>} 启动完成的Promise
|
|
57
|
+
*/
|
|
58
|
+
async start(videoElement: HTMLVideoElement): Promise<void> {
|
|
59
|
+
await this.camera.initialize(videoElement);
|
|
60
|
+
this.detecting = true;
|
|
61
|
+
this.detect();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 执行一次身份证检测
|
|
66
|
+
*
|
|
67
|
+
* 内部方法,捕获当前视频帧并尝试检测其中的身份证
|
|
68
|
+
*
|
|
69
|
+
* @private
|
|
70
|
+
*/
|
|
71
|
+
private async detect(): Promise<void> {
|
|
72
|
+
if (!this.detecting) return;
|
|
73
|
+
|
|
74
|
+
const imageData = this.camera.captureFrame();
|
|
75
|
+
|
|
76
|
+
if (imageData) {
|
|
77
|
+
try {
|
|
78
|
+
// 简单实现,因为没有完整的OpenCV.js
|
|
79
|
+
// 实际项目中应该使用OpenCV.js做更精确的边缘检测
|
|
80
|
+
const result = await this.detectIDCard(imageData);
|
|
81
|
+
|
|
82
|
+
if (this.onDetected) {
|
|
83
|
+
this.onDetected(result);
|
|
84
|
+
}
|
|
85
|
+
} catch (error) {
|
|
86
|
+
console.error('身份证检测错误:', error);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
this.detectTimer = window.setTimeout(() => this.detect(), 200);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* 身份证检测核心算法
|
|
95
|
+
*
|
|
96
|
+
* 通过图像处理技术检测和提取图像中的身份证区域
|
|
97
|
+
*
|
|
98
|
+
* @private
|
|
99
|
+
* @param {ImageData} imageData - 需要检测身份证的图像数据
|
|
100
|
+
* @returns {Promise<DetectionResult>} 检测结果,包含成功标志和裁剪后的身份证图像
|
|
101
|
+
*/
|
|
102
|
+
private async detectIDCard(imageData: ImageData): Promise<DetectionResult> {
|
|
103
|
+
// 图像预处理
|
|
104
|
+
const grayscale = ImageProcessor.toGrayscale(imageData);
|
|
105
|
+
const enhanced = ImageProcessor.adjustBrightnessContrast(grayscale, 10, 30);
|
|
106
|
+
|
|
107
|
+
// 简化的身份证检测算法
|
|
108
|
+
// 在真实项目中,这里应该使用OpenCV.js进行轮廓检测和矩形检测
|
|
109
|
+
|
|
110
|
+
// 模拟检测过程
|
|
111
|
+
const success = Math.random() > 0.7; // 模拟70%的概率检测成功
|
|
112
|
+
|
|
113
|
+
if (success) {
|
|
114
|
+
// 模拟一个身份证区域,实际项目中应该是根据检测结果
|
|
115
|
+
const cardWidth = Math.floor(imageData.width * 0.8);
|
|
116
|
+
const cardHeight = Math.floor(cardWidth * 0.63); // 身份证比例大约是8:5
|
|
117
|
+
const x = Math.floor((imageData.width - cardWidth) / 2);
|
|
118
|
+
const y = Math.floor((imageData.height - cardHeight) / 2);
|
|
119
|
+
|
|
120
|
+
// 模拟四个角点
|
|
121
|
+
const corners = [
|
|
122
|
+
{ x, y }, // 左上
|
|
123
|
+
{ x: x + cardWidth, y }, // 右上
|
|
124
|
+
{ x: x + cardWidth, y: y + cardHeight }, // 右下
|
|
125
|
+
{ x, y: y + cardHeight } // 左下
|
|
126
|
+
];
|
|
127
|
+
|
|
128
|
+
// 模拟裁剪图像(实际项目中应该做透视变换)
|
|
129
|
+
const canvas = document.createElement('canvas');
|
|
130
|
+
canvas.width = cardWidth;
|
|
131
|
+
canvas.height = cardHeight;
|
|
132
|
+
const ctx = canvas.getContext('2d');
|
|
133
|
+
|
|
134
|
+
if (ctx) {
|
|
135
|
+
// 从原图中裁剪身份证区域
|
|
136
|
+
const sourceCanvas = ImageProcessor.imageDataToCanvas(imageData);
|
|
137
|
+
ctx.drawImage(
|
|
138
|
+
sourceCanvas,
|
|
139
|
+
x, y, cardWidth, cardHeight,
|
|
140
|
+
0, 0, cardWidth, cardHeight
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
const croppedImage = ctx.getImageData(0, 0, cardWidth, cardHeight);
|
|
144
|
+
|
|
145
|
+
return {
|
|
146
|
+
success: true,
|
|
147
|
+
corners,
|
|
148
|
+
croppedImage
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return { success: false };
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* 停止身份证检测
|
|
158
|
+
*
|
|
159
|
+
* 停止检测循环并释放相机资源
|
|
160
|
+
*/
|
|
161
|
+
stop(): void {
|
|
162
|
+
this.detecting = false;
|
|
163
|
+
|
|
164
|
+
if (this.detectTimer) {
|
|
165
|
+
clearTimeout(this.detectTimer);
|
|
166
|
+
this.detectTimer = null;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
this.camera.release();
|
|
170
|
+
}
|
|
171
|
+
}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file OCR处理模块
|
|
3
|
+
* @description 提供身份证文字识别和信息提取功能
|
|
4
|
+
* @module OCRProcessor
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { createWorker } from 'tesseract.js';
|
|
8
|
+
import { IDCardInfo } from '../utils/types';
|
|
9
|
+
import { ImageProcessor } from '../utils/image-processing';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* OCR处理器类
|
|
13
|
+
*
|
|
14
|
+
* 使用Tesseract.js实现对身份证图像的OCR文字识别和信息提取功能
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* // 创建OCR处理器
|
|
19
|
+
* const ocrProcessor = new OCRProcessor();
|
|
20
|
+
*
|
|
21
|
+
* // 初始化OCR引擎
|
|
22
|
+
* await ocrProcessor.initialize();
|
|
23
|
+
*
|
|
24
|
+
* // 处理身份证图像
|
|
25
|
+
* const idInfo = await ocrProcessor.processIDCard(idCardImageData);
|
|
26
|
+
* console.log('识别到的身份证信息:', idInfo);
|
|
27
|
+
*
|
|
28
|
+
* // 使用结束后释放资源
|
|
29
|
+
* await ocrProcessor.terminate();
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export class OCRProcessor {
|
|
33
|
+
private worker: any = null;
|
|
34
|
+
|
|
35
|
+
constructor() {}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 初始化OCR引擎
|
|
39
|
+
*
|
|
40
|
+
* 加载Tesseract OCR引擎和中文简体语言包,并设置适合身份证识别的参数
|
|
41
|
+
*
|
|
42
|
+
* @returns {Promise<void>} 初始化完成的Promise
|
|
43
|
+
*/
|
|
44
|
+
async initialize(): Promise<void> {
|
|
45
|
+
this.worker = createWorker({
|
|
46
|
+
logger: (m: any) => console.log(m)
|
|
47
|
+
} as any);
|
|
48
|
+
|
|
49
|
+
await this.worker.load();
|
|
50
|
+
await this.worker.loadLanguage('chi_sim');
|
|
51
|
+
await this.worker.initialize('chi_sim');
|
|
52
|
+
await this.worker.setParameters({
|
|
53
|
+
tessedit_char_whitelist: '0123456789X-年月日一二三四五六七八九十零壹贰叁肆伍陆柒捌玖拾ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz民族汉族满族回族维吾尔族藏族苗族彝族壮族朝鲜族侗族瑶族白族土家族哈尼族哈萨克族傣族黎族傈僳族佤族高山族拉祜族水族东乡族钠西族景颇族柯尔克孜族士族达斡尔族仫佬族羌族布朗族撒拉族毛南族仡佬族锡伯族阿昌族普米族塔吉克族怒族乌孜别克族俄罗斯族鄂温克族德昂族保安族裕固族京族塔塔尔族独龙族鄂伦春族赫哲族门巴族珞巴族基诺族男女性别住址出生公民身份号码签发机关有效期'
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 处理身份证图像并提取信息
|
|
59
|
+
*
|
|
60
|
+
* 对身份证图像进行OCR识别,并从识别结果中提取结构化信息
|
|
61
|
+
*
|
|
62
|
+
* @param {ImageData} imageData - 身份证图像数据
|
|
63
|
+
* @returns {Promise<IDCardInfo>} 提取到的身份证信息
|
|
64
|
+
*/
|
|
65
|
+
async processIDCard(imageData: ImageData): Promise<IDCardInfo> {
|
|
66
|
+
if (!this.worker) {
|
|
67
|
+
await this.initialize();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// 图像预处理,提高OCR识别率
|
|
71
|
+
const enhancedImage = ImageProcessor.adjustBrightnessContrast(imageData, 15, 25);
|
|
72
|
+
|
|
73
|
+
// 转换ImageData为Canvas
|
|
74
|
+
const canvas = ImageProcessor.imageDataToCanvas(enhancedImage);
|
|
75
|
+
|
|
76
|
+
// OCR识别
|
|
77
|
+
try {
|
|
78
|
+
const { data } = await this.worker.recognize(canvas);
|
|
79
|
+
|
|
80
|
+
// 解析身份证信息
|
|
81
|
+
return this.parseIDCardText(data.text);
|
|
82
|
+
} catch (error) {
|
|
83
|
+
console.error('OCR识别错误:', error);
|
|
84
|
+
return {}; // 返回空对象
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* 解析身份证文本信息
|
|
90
|
+
*
|
|
91
|
+
* 从OCR识别到的文本中提取结构化的身份证信息
|
|
92
|
+
*
|
|
93
|
+
* @private
|
|
94
|
+
* @param {string} text - OCR识别到的文本
|
|
95
|
+
* @returns {IDCardInfo} 提取到的身份证信息对象
|
|
96
|
+
*/
|
|
97
|
+
private parseIDCardText(text: string): IDCardInfo {
|
|
98
|
+
const info: IDCardInfo = {};
|
|
99
|
+
|
|
100
|
+
// 拆分为行
|
|
101
|
+
const lines = text.split('\n').filter(line => line.trim());
|
|
102
|
+
|
|
103
|
+
// 解析身份证号码(最容易识别的部分)
|
|
104
|
+
const idNumberRegex = /(\d{17}[\dX])/;
|
|
105
|
+
const idNumberMatch = text.match(idNumberRegex);
|
|
106
|
+
if (idNumberMatch) {
|
|
107
|
+
info.idNumber = idNumberMatch[1];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// 解析姓名
|
|
111
|
+
for (const line of lines) {
|
|
112
|
+
if (line.includes('姓名') || line.length < 10 && line.length > 1 && !/\d/.test(line)) {
|
|
113
|
+
info.name = line.replace('姓名', '').trim();
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// 解析性别和民族
|
|
119
|
+
const genderNationalityRegex = /(男|女).*(族)/;
|
|
120
|
+
const genderMatch = text.match(genderNationalityRegex);
|
|
121
|
+
if (genderMatch) {
|
|
122
|
+
info.gender = genderMatch[1];
|
|
123
|
+
const nationalityText = genderMatch[0];
|
|
124
|
+
info.nationality = nationalityText.substring(nationalityText.indexOf(genderMatch[1]) + 1).trim();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// 解析出生日期
|
|
128
|
+
const birthDateRegex = /(\d{4})年(\d{1,2})月(\d{1,2})日/;
|
|
129
|
+
const birthDateMatch = text.match(birthDateRegex);
|
|
130
|
+
if (birthDateMatch) {
|
|
131
|
+
info.birthDate = `${birthDateMatch[1]}-${birthDateMatch[2]}-${birthDateMatch[3]}`;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// 解析地址
|
|
135
|
+
const addressRegex = /住址([\s\S]*?)公民身份号码/;
|
|
136
|
+
const addressMatch = text.match(addressRegex);
|
|
137
|
+
if (addressMatch) {
|
|
138
|
+
info.address = addressMatch[1].replace(/\n/g, '').trim();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// 解析签发机关
|
|
142
|
+
const authorityRegex = /签发机关([\s\S]*?)有效期/;
|
|
143
|
+
const authorityMatch = text.match(authorityRegex);
|
|
144
|
+
if (authorityMatch) {
|
|
145
|
+
info.issuingAuthority = authorityMatch[1].replace(/\n/g, '').trim();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// 解析有效期限
|
|
149
|
+
const validPeriodRegex = /有效期限([\s\S]*?)(-|至)/;
|
|
150
|
+
const validPeriodMatch = text.match(validPeriodRegex);
|
|
151
|
+
if (validPeriodMatch) {
|
|
152
|
+
info.validPeriod = validPeriodMatch[0].replace('有效期限', '').trim();
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return info;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* 终止OCR引擎并释放资源
|
|
160
|
+
*
|
|
161
|
+
* @returns {Promise<void>} 终止完成的Promise
|
|
162
|
+
*/
|
|
163
|
+
async terminate(): Promise<void> {
|
|
164
|
+
if (this.worker) {
|
|
165
|
+
await this.worker.terminate();
|
|
166
|
+
this.worker = null;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|