id-scanner-lib 1.2.2 → 1.3.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 (67) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +375 -363
  3. package/dist/id-scanner-core.esm.js +427 -221
  4. package/dist/id-scanner-core.esm.js.map +1 -1
  5. package/dist/id-scanner-core.js +427 -221
  6. package/dist/id-scanner-core.js.map +1 -1
  7. package/dist/id-scanner-core.min.js +1 -9
  8. package/dist/id-scanner-core.min.js.map +1 -1
  9. package/dist/id-scanner-ocr.esm.js +451 -276
  10. package/dist/id-scanner-ocr.esm.js.map +1 -1
  11. package/dist/id-scanner-ocr.js +451 -276
  12. package/dist/id-scanner-ocr.js.map +1 -1
  13. package/dist/id-scanner-ocr.min.js +1 -9
  14. package/dist/id-scanner-ocr.min.js.map +1 -1
  15. package/dist/id-scanner-qr.esm.js +483 -233
  16. package/dist/id-scanner-qr.esm.js.map +1 -1
  17. package/dist/id-scanner-qr.js +482 -232
  18. package/dist/id-scanner-qr.js.map +1 -1
  19. package/dist/id-scanner-qr.min.js +1 -9
  20. package/dist/id-scanner-qr.min.js.map +1 -1
  21. package/dist/id-scanner.js +2138 -358
  22. package/dist/id-scanner.js.map +1 -1
  23. package/dist/id-scanner.min.js +1 -9
  24. package/dist/id-scanner.min.js.map +1 -1
  25. package/package.json +27 -7
  26. package/src/demo/demo.ts +178 -62
  27. package/src/id-recognition/anti-fake-detector.ts +317 -0
  28. package/src/id-recognition/id-detector.ts +184 -155
  29. package/src/id-recognition/ocr-processor.ts +193 -146
  30. package/src/id-recognition/ocr-worker.ts +82 -72
  31. package/src/index-umd.ts +347 -110
  32. package/src/index.ts +866 -91
  33. package/src/ocr-module.ts +108 -60
  34. package/src/qr-module.ts +104 -54
  35. package/src/scanner/barcode-scanner.ts +145 -58
  36. package/src/scanner/qr-scanner.ts +86 -47
  37. package/src/utils/image-processing.ts +479 -294
  38. package/dist/core.d.ts +0 -77
  39. package/dist/demo/demo.d.ts +0 -14
  40. package/dist/id-recognition/data-extractor.d.ts +0 -105
  41. package/dist/id-recognition/id-detector.d.ts +0 -100
  42. package/dist/id-recognition/ocr-processor.d.ts +0 -64
  43. package/dist/id-scanner.esm.js +0 -94656
  44. package/dist/id-scanner.esm.js.map +0 -1
  45. package/dist/index-umd.d.ts +0 -96
  46. package/dist/index.d.ts +0 -78
  47. package/dist/ocr-module.d.ts +0 -67
  48. package/dist/qr-module.d.ts +0 -68
  49. package/dist/scanner/barcode-scanner.d.ts +0 -90
  50. package/dist/scanner/qr-scanner.d.ts +0 -80
  51. package/dist/types/core.d.ts +0 -77
  52. package/dist/types/demo/demo.d.ts +0 -14
  53. package/dist/types/id-recognition/data-extractor.d.ts +0 -105
  54. package/dist/types/id-recognition/id-detector.d.ts +0 -100
  55. package/dist/types/id-recognition/ocr-processor.d.ts +0 -64
  56. package/dist/types/index-umd.d.ts +0 -96
  57. package/dist/types/index.d.ts +0 -78
  58. package/dist/types/ocr-module.d.ts +0 -67
  59. package/dist/types/qr-module.d.ts +0 -68
  60. package/dist/types/scanner/barcode-scanner.d.ts +0 -90
  61. package/dist/types/scanner/qr-scanner.d.ts +0 -80
  62. package/dist/types/utils/camera.d.ts +0 -81
  63. package/dist/types/utils/image-processing.d.ts +0 -75
  64. package/dist/types/utils/types.d.ts +0 -65
  65. package/dist/utils/camera.d.ts +0 -81
  66. package/dist/utils/image-processing.d.ts +0 -75
  67. package/dist/utils/types.d.ts +0 -65
@@ -4,143 +4,153 @@
4
4
  * @module OCRWorker
5
5
  */
6
6
 
7
- import type { IDCardInfo } from '../utils/types';
7
+ import type { IDCardInfo } from "../utils/types"
8
8
 
9
9
  /**
10
10
  * OCR处理输入接口
11
11
  */
12
12
  export interface OCRProcessInput {
13
- imageBase64: string;
14
- tessWorkerOptions?: any;
13
+ imageBase64: string
14
+ tessWorkerOptions?: any
15
15
  }
16
16
 
17
17
  /**
18
18
  * OCR处理输出接口
19
19
  */
20
20
  export interface OCRProcessOutput {
21
- idCardInfo: IDCardInfo;
22
- processingTime: number;
21
+ idCardInfo: IDCardInfo
22
+ processingTime: number
23
23
  }
24
24
 
25
25
  /**
26
26
  * 在Web Worker中执行OCR处理的函数
27
- *
27
+ *
28
28
  * 该函数用于在使用 createWorker 创建的 Worker 中执行
29
- *
29
+ *
30
30
  * @param input OCR处理输入数据
31
31
  * @returns OCR处理结果
32
32
  */
33
- export async function processOCRInWorker(input: OCRProcessInput): Promise<OCRProcessOutput> {
33
+ export async function processOCRInWorker(
34
+ input: OCRProcessInput
35
+ ): Promise<OCRProcessOutput> {
34
36
  // 计时开始
35
- const startTime = performance.now();
36
-
37
+ const startTime = performance.now()
38
+
37
39
  // 加载Tesseract.js (Worker 环境下动态导入)
38
- const { createWorker } = await import('tesseract.js');
39
-
40
+ const { createWorker } = await import("tesseract.js")
41
+
40
42
  // 创建OCR Worker
41
- const worker = await createWorker(input.tessWorkerOptions || {
42
- logger: (m: any) => console.log(m)
43
- }) as any; // 添加类型断言,避免TypeScript错误
44
-
43
+ const worker = (await createWorker(
44
+ input.tessWorkerOptions || {
45
+ logger: (m: any) => console.log(m),
46
+ }
47
+ )) as any // 添加类型断言,避免TypeScript错误
48
+
45
49
  try {
46
50
  // 初始化OCR引擎
47
- await worker.load();
48
- await worker.loadLanguage('chi_sim');
49
- await worker.initialize('chi_sim');
51
+ await worker.load()
52
+ await worker.loadLanguage("chi_sim")
53
+ await worker.initialize("chi_sim")
50
54
  await worker.setParameters({
51
- tessedit_char_whitelist: '0123456789X-年月日一二三四五六七八九十零壹贰叁肆伍陆柒捌玖拾ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz民族汉族满族回族维吾尔族藏族苗族彝族壮族朝鲜族侗族瑶族白族土家族哈尼族哈萨克族傣族黎族傈僳族佤族高山族拉祜族水族东乡族钠西族景颇族柯尔克孜族士族达斡尔族仫佬族羌族布朗族撒拉族毛南族仡佬族锡伯族阿昌族普米族塔吉克族怒族乌孜别克族俄罗斯族鄂温克族德昂族保安族裕固族京族塔塔尔族独龙族鄂伦春族赫哲族门巴族珞巴族基诺族男女性别住址出生公民身份号码签发机关有效期'
52
- });
53
-
55
+ tessedit_char_whitelist:
56
+ "0123456789X-年月日一二三四五六七八九十零壹贰叁肆伍陆柒捌玖拾ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz民族汉族满族回族维吾尔族藏族苗族彝族壮族朝鲜族侗族瑶族白族土家族哈尼族哈萨克族傣族黎族傈僳族佤族高山族拉祜族水族东乡族钠西族景颇族柯尔克孜族士族达斡尔族仫佬族羌族布朗族撒拉族毛南族仡佬族锡伯族阿昌族普米族塔吉克族怒族乌孜别克族俄罗斯族鄂温克族德昂族保安族裕固族京族塔塔尔族独龙族鄂伦春族赫哲族门巴族珞巴族基诺族男女性别住址出生公民身份号码签发机关有效期",
57
+ })
58
+
54
59
  // 识别图像
55
- const { data } = await worker.recognize(input.imageBase64);
56
-
60
+ const { data } = await worker.recognize(input.imageBase64)
61
+
57
62
  // 解析识别结果
58
- const idCardInfo = parseIDCardText(data.text);
59
-
63
+ const idCardInfo = parseIDCardText(data.text)
64
+
60
65
  // 处理完成后终止worker
61
- await worker.terminate();
62
-
66
+ await worker.terminate()
67
+
63
68
  // 计算处理时间
64
- const processingTime = performance.now() - startTime;
65
-
69
+ const processingTime = performance.now() - startTime
70
+
66
71
  // 返回处理结果
67
72
  return {
68
73
  idCardInfo,
69
- processingTime
70
- };
74
+ processingTime,
75
+ }
71
76
  } catch (error) {
72
77
  // 确保资源被释放
73
- await worker.terminate();
74
- throw error;
78
+ await worker.terminate()
79
+ throw error
75
80
  }
76
81
  }
77
82
 
78
83
  /**
79
84
  * 解析身份证文本信息
80
- *
85
+ *
81
86
  * 从OCR识别到的文本中提取结构化的身份证信息
82
- *
87
+ *
83
88
  * @private
84
89
  * @param {string} text - OCR识别到的文本
85
90
  * @returns {IDCardInfo} 提取到的身份证信息对象
86
91
  */
87
92
  function parseIDCardText(text: string): IDCardInfo {
88
- const info: IDCardInfo = {};
89
-
93
+ const info: IDCardInfo = {}
94
+
90
95
  // 拆分为行
91
- const lines = text.split('\n').filter(line => line.trim());
92
-
96
+ const lines = text.split("\n").filter((line) => line.trim())
97
+
93
98
  // 解析身份证号码(最容易识别的部分)
94
- const idNumberRegex = /(\d{17}[\dX])/;
95
- const idNumberMatch = text.match(idNumberRegex);
99
+ const idNumberRegex = /(\d{17}[\dX])/
100
+ const idNumberMatch = text.match(idNumberRegex)
96
101
  if (idNumberMatch) {
97
- info.idNumber = idNumberMatch[1];
102
+ info.idNumber = idNumberMatch[1]
98
103
  }
99
-
104
+
100
105
  // 解析姓名
101
106
  for (const line of lines) {
102
- if (line.includes('姓名') || line.length < 10 && line.length > 1 && !/\d/.test(line)) {
103
- info.name = line.replace('姓名', '').trim();
104
- break;
107
+ if (
108
+ line.includes("姓名") ||
109
+ (line.length < 10 && line.length > 1 && !/\d/.test(line))
110
+ ) {
111
+ info.name = line.replace("姓名", "").trim()
112
+ break
105
113
  }
106
114
  }
107
-
115
+
108
116
  // 解析性别和民族
109
- const genderNationalityRegex = /(男|女).*(族)/;
110
- const genderMatch = text.match(genderNationalityRegex);
117
+ const genderNationalityRegex = /(男|女).*(族)/
118
+ const genderMatch = text.match(genderNationalityRegex)
111
119
  if (genderMatch) {
112
- info.gender = genderMatch[1];
113
- const nationalityText = genderMatch[0];
114
- info.nationality = nationalityText.substring(nationalityText.indexOf(genderMatch[1]) + 1).trim();
120
+ info.gender = genderMatch[1]
121
+ const nationalityText = genderMatch[0]
122
+ info.nationality = nationalityText
123
+ .substring(nationalityText.indexOf(genderMatch[1]) + 1)
124
+ .trim()
115
125
  }
116
-
126
+
117
127
  // 解析出生日期
118
- const birthDateRegex = /(\d{4})年(\d{1,2})月(\d{1,2})日/;
119
- const birthDateMatch = text.match(birthDateRegex);
128
+ const birthDateRegex = /(\d{4})年(\d{1,2})月(\d{1,2})日/
129
+ const birthDateMatch = text.match(birthDateRegex)
120
130
  if (birthDateMatch) {
121
- info.birthDate = `${birthDateMatch[1]}-${birthDateMatch[2]}-${birthDateMatch[3]}`;
131
+ info.birthDate = `${birthDateMatch[1]}-${birthDateMatch[2]}-${birthDateMatch[3]}`
122
132
  }
123
-
133
+
124
134
  // 解析地址
125
- const addressRegex = /住址([\s\S]*?)公民身份号码/;
126
- const addressMatch = text.match(addressRegex);
135
+ const addressRegex = /住址([\s\S]*?)公民身份号码/
136
+ const addressMatch = text.match(addressRegex)
127
137
  if (addressMatch) {
128
- info.address = addressMatch[1].replace(/\n/g, '').trim();
138
+ info.address = addressMatch[1].replace(/\n/g, "").trim()
129
139
  }
130
-
140
+
131
141
  // 解析签发机关
132
- const authorityRegex = /签发机关([\s\S]*?)有效期/;
133
- const authorityMatch = text.match(authorityRegex);
142
+ const authorityRegex = /签发机关([\s\S]*?)有效期/
143
+ const authorityMatch = text.match(authorityRegex)
134
144
  if (authorityMatch) {
135
- info.issuingAuthority = authorityMatch[1].replace(/\n/g, '').trim();
145
+ info.issuingAuthority = authorityMatch[1].replace(/\n/g, "").trim()
136
146
  }
137
-
147
+
138
148
  // 解析有效期限
139
- const validPeriodRegex = /有效期限([\s\S]*?)(-|至)/;
140
- const validPeriodMatch = text.match(validPeriodRegex);
149
+ const validPeriodRegex = /有效期限([\s\S]*?)(-|至)/
150
+ const validPeriodMatch = text.match(validPeriodRegex)
141
151
  if (validPeriodMatch) {
142
- info.validPeriod = validPeriodMatch[0].replace('有效期限', '').trim();
152
+ info.validPeriod = validPeriodMatch[0].replace("有效期限", "").trim()
143
153
  }
144
-
145
- return info;
146
- }
154
+
155
+ return info
156
+ }