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.
- package/LICENSE +1 -1
- package/README.md +375 -363
- package/dist/id-scanner-core.esm.js +427 -221
- package/dist/id-scanner-core.esm.js.map +1 -1
- package/dist/id-scanner-core.js +427 -221
- package/dist/id-scanner-core.js.map +1 -1
- package/dist/id-scanner-core.min.js +1 -9
- package/dist/id-scanner-core.min.js.map +1 -1
- package/dist/id-scanner-ocr.esm.js +451 -276
- package/dist/id-scanner-ocr.esm.js.map +1 -1
- package/dist/id-scanner-ocr.js +451 -276
- package/dist/id-scanner-ocr.js.map +1 -1
- package/dist/id-scanner-ocr.min.js +1 -9
- package/dist/id-scanner-ocr.min.js.map +1 -1
- package/dist/id-scanner-qr.esm.js +483 -233
- package/dist/id-scanner-qr.esm.js.map +1 -1
- package/dist/id-scanner-qr.js +482 -232
- package/dist/id-scanner-qr.js.map +1 -1
- package/dist/id-scanner-qr.min.js +1 -9
- package/dist/id-scanner-qr.min.js.map +1 -1
- package/dist/id-scanner.js +2138 -358
- package/dist/id-scanner.js.map +1 -1
- package/dist/id-scanner.min.js +1 -9
- package/dist/id-scanner.min.js.map +1 -1
- package/package.json +27 -7
- package/src/demo/demo.ts +178 -62
- package/src/id-recognition/anti-fake-detector.ts +317 -0
- package/src/id-recognition/id-detector.ts +184 -155
- package/src/id-recognition/ocr-processor.ts +193 -146
- package/src/id-recognition/ocr-worker.ts +82 -72
- package/src/index-umd.ts +347 -110
- package/src/index.ts +866 -91
- package/src/ocr-module.ts +108 -60
- package/src/qr-module.ts +104 -54
- package/src/scanner/barcode-scanner.ts +145 -58
- package/src/scanner/qr-scanner.ts +86 -47
- package/src/utils/image-processing.ts +479 -294
- package/dist/core.d.ts +0 -77
- package/dist/demo/demo.d.ts +0 -14
- package/dist/id-recognition/data-extractor.d.ts +0 -105
- package/dist/id-recognition/id-detector.d.ts +0 -100
- package/dist/id-recognition/ocr-processor.d.ts +0 -64
- package/dist/id-scanner.esm.js +0 -94656
- package/dist/id-scanner.esm.js.map +0 -1
- package/dist/index-umd.d.ts +0 -96
- package/dist/index.d.ts +0 -78
- package/dist/ocr-module.d.ts +0 -67
- package/dist/qr-module.d.ts +0 -68
- package/dist/scanner/barcode-scanner.d.ts +0 -90
- package/dist/scanner/qr-scanner.d.ts +0 -80
- package/dist/types/core.d.ts +0 -77
- package/dist/types/demo/demo.d.ts +0 -14
- package/dist/types/id-recognition/data-extractor.d.ts +0 -105
- package/dist/types/id-recognition/id-detector.d.ts +0 -100
- package/dist/types/id-recognition/ocr-processor.d.ts +0 -64
- package/dist/types/index-umd.d.ts +0 -96
- package/dist/types/index.d.ts +0 -78
- package/dist/types/ocr-module.d.ts +0 -67
- package/dist/types/qr-module.d.ts +0 -68
- package/dist/types/scanner/barcode-scanner.d.ts +0 -90
- package/dist/types/scanner/qr-scanner.d.ts +0 -80
- package/dist/types/utils/camera.d.ts +0 -81
- package/dist/types/utils/image-processing.d.ts +0 -75
- package/dist/types/utils/types.d.ts +0 -65
- package/dist/utils/camera.d.ts +0 -81
- package/dist/utils/image-processing.d.ts +0 -75
- package/dist/utils/types.d.ts +0 -65
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file 身份证防伪检测模块
|
|
3
|
+
* @description 提供身份证防伪特征识别功能,区分真假身份证
|
|
4
|
+
* @module AntiFakeDetector
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { ImageProcessor } from "../utils/image-processing"
|
|
8
|
+
import { LRUCache, calculateImageFingerprint } from "../utils/performance"
|
|
9
|
+
import { Disposable } from "../utils/resource-manager"
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 防伪检测结果
|
|
13
|
+
*/
|
|
14
|
+
export interface AntiFakeDetectionResult {
|
|
15
|
+
isAuthentic: boolean // 是否为真实身份证
|
|
16
|
+
confidence: number // 置信度(0-1)
|
|
17
|
+
detectedFeatures: string[] // 检测到的防伪特征
|
|
18
|
+
message: string // 结果描述
|
|
19
|
+
processingTime?: number // 处理时间(ms)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 防伪检测器配置选项
|
|
24
|
+
*/
|
|
25
|
+
export interface AntiFakeDetectorOptions {
|
|
26
|
+
sensitivity?: number // 敏感度 (0-1),值越高越严格
|
|
27
|
+
enableCache?: boolean // 是否启用缓存
|
|
28
|
+
cacheSize?: number // 缓存大小
|
|
29
|
+
logger?: (message: any) => void // 日志记录器
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 身份证防伪特征检测器
|
|
34
|
+
*
|
|
35
|
+
* 基于图像分析技术检测身份证中的多种防伪特征,包括:
|
|
36
|
+
* 1. 荧光油墨特征
|
|
37
|
+
* 2. 微缩文字
|
|
38
|
+
* 3. 光变图案
|
|
39
|
+
* 4. 雕刻凹印
|
|
40
|
+
* 5. 隐形图案
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* // 创建防伪检测器
|
|
45
|
+
* const antiFakeDetector = new AntiFakeDetector({
|
|
46
|
+
* sensitivity: 0.8,
|
|
47
|
+
* enableCache: true
|
|
48
|
+
* });
|
|
49
|
+
*
|
|
50
|
+
* // 分析身份证图像
|
|
51
|
+
* const imageData = await ImageProcessor.createImageDataFromFile(idCardFile);
|
|
52
|
+
* const result = await antiFakeDetector.detect(imageData);
|
|
53
|
+
*
|
|
54
|
+
* if (result.isAuthentic) {
|
|
55
|
+
* console.log('身份证真实,检测到防伪特征:', result.detectedFeatures);
|
|
56
|
+
* } else {
|
|
57
|
+
* console.log('警告!', result.message);
|
|
58
|
+
* }
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export class AntiFakeDetector implements Disposable {
|
|
62
|
+
private options: Required<AntiFakeDetectorOptions>
|
|
63
|
+
private resultCache: LRUCache<string, AntiFakeDetectionResult>
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 创建身份证防伪检测器实例
|
|
67
|
+
*
|
|
68
|
+
* @param options 防伪检测器配置
|
|
69
|
+
*/
|
|
70
|
+
constructor(options: AntiFakeDetectorOptions = {}) {
|
|
71
|
+
this.options = {
|
|
72
|
+
sensitivity: 0.7,
|
|
73
|
+
enableCache: true,
|
|
74
|
+
cacheSize: 50,
|
|
75
|
+
logger: console.log,
|
|
76
|
+
...options,
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// 初始化缓存
|
|
80
|
+
this.resultCache = new LRUCache<string, AntiFakeDetectionResult>(
|
|
81
|
+
this.options.cacheSize
|
|
82
|
+
)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* 检测身份证图像的防伪特征
|
|
87
|
+
*
|
|
88
|
+
* @param imageData 身份证图像数据
|
|
89
|
+
* @returns 防伪检测结果
|
|
90
|
+
*/
|
|
91
|
+
async detect(imageData: ImageData): Promise<AntiFakeDetectionResult> {
|
|
92
|
+
const startTime = performance.now()
|
|
93
|
+
|
|
94
|
+
// 检查缓存
|
|
95
|
+
if (this.options.enableCache) {
|
|
96
|
+
const fingerprint = calculateImageFingerprint(imageData)
|
|
97
|
+
const cachedResult = this.resultCache.get(fingerprint)
|
|
98
|
+
|
|
99
|
+
if (cachedResult) {
|
|
100
|
+
this.options.logger("使用缓存的防伪检测结果")
|
|
101
|
+
return cachedResult
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// 图像预处理增强防伪特征
|
|
106
|
+
const enhancedImage = this.enhanceAntiFakeFeatures(imageData)
|
|
107
|
+
|
|
108
|
+
// 执行多种防伪特征检测
|
|
109
|
+
const featureResults = await Promise.all([
|
|
110
|
+
this.detectUVInkFeatures(enhancedImage),
|
|
111
|
+
this.detectMicroText(enhancedImage),
|
|
112
|
+
this.detectOpticalVariable(enhancedImage),
|
|
113
|
+
this.detectIntaglioPrinting(enhancedImage),
|
|
114
|
+
this.detectGhostImage(enhancedImage),
|
|
115
|
+
])
|
|
116
|
+
|
|
117
|
+
// 汇总检测结果
|
|
118
|
+
const detectedFeatures: string[] = []
|
|
119
|
+
let totalConfidence = 0
|
|
120
|
+
|
|
121
|
+
for (const [feature, detected, confidence] of featureResults) {
|
|
122
|
+
if (detected && confidence > 0.5) {
|
|
123
|
+
detectedFeatures.push(feature)
|
|
124
|
+
totalConfidence += confidence
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// 计算最终结果
|
|
129
|
+
const normalizedConfidence =
|
|
130
|
+
featureResults.length > 0 ? totalConfidence / featureResults.length : 0
|
|
131
|
+
|
|
132
|
+
// 根据敏感度和检测到的特征决定是否通过验证
|
|
133
|
+
const isAuthentic =
|
|
134
|
+
normalizedConfidence >= this.options.sensitivity &&
|
|
135
|
+
detectedFeatures.length >= 2
|
|
136
|
+
|
|
137
|
+
// 生成结果消息
|
|
138
|
+
let message = isAuthentic
|
|
139
|
+
? `身份证真实,检测到${detectedFeatures.length}个防伪特征`
|
|
140
|
+
: detectedFeatures.length > 0
|
|
141
|
+
? `可疑身份证,仅检测到${detectedFeatures.length}个防伪特征,置信度不足`
|
|
142
|
+
: "未检测到有效防伪特征,可能为伪造证件"
|
|
143
|
+
|
|
144
|
+
const result: AntiFakeDetectionResult = {
|
|
145
|
+
isAuthentic,
|
|
146
|
+
confidence: normalizedConfidence,
|
|
147
|
+
detectedFeatures,
|
|
148
|
+
message,
|
|
149
|
+
processingTime: performance.now() - startTime,
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// 缓存结果
|
|
153
|
+
if (this.options.enableCache) {
|
|
154
|
+
const fingerprint = calculateImageFingerprint(imageData)
|
|
155
|
+
this.resultCache.set(fingerprint, result)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return result
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* 增强身份证图像中的防伪特征
|
|
163
|
+
*
|
|
164
|
+
* @param imageData 原始图像数据
|
|
165
|
+
* @returns 增强后的图像数据
|
|
166
|
+
* @private
|
|
167
|
+
*/
|
|
168
|
+
private enhanceAntiFakeFeatures(imageData: ImageData): ImageData {
|
|
169
|
+
// 应用特定的图像处理增强防伪特征
|
|
170
|
+
return ImageProcessor.batchProcess(imageData, {
|
|
171
|
+
contrast: 30, // 增强对比度
|
|
172
|
+
brightness: 10, // 轻微提高亮度
|
|
173
|
+
sharpen: true, // 锐化图像突出细节
|
|
174
|
+
})
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* 检测荧光油墨特征
|
|
179
|
+
*
|
|
180
|
+
* @param imageData 图像数据
|
|
181
|
+
* @returns [特征名称, 是否检测到, 置信度]
|
|
182
|
+
* @private
|
|
183
|
+
*/
|
|
184
|
+
private async detectUVInkFeatures(
|
|
185
|
+
imageData: ImageData
|
|
186
|
+
): Promise<[string, boolean, number]> {
|
|
187
|
+
// 提取蓝色通道增强UV油墨可见度
|
|
188
|
+
const canvas = document.createElement("canvas")
|
|
189
|
+
canvas.width = imageData.width
|
|
190
|
+
canvas.height = imageData.height
|
|
191
|
+
const ctx = canvas.getContext("2d")
|
|
192
|
+
|
|
193
|
+
if (!ctx) {
|
|
194
|
+
return ["荧光油墨", false, 0]
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
ctx.putImageData(imageData, 0, 0)
|
|
198
|
+
|
|
199
|
+
// 分析蓝色通道中的特定模式
|
|
200
|
+
// 实际实现中应使用更复杂的算法提取UV特征
|
|
201
|
+
// 这里使用模拟实现
|
|
202
|
+
|
|
203
|
+
// 模拟检测: 70%的概率检测到,置信度0.65-0.95
|
|
204
|
+
const detected = Math.random() > 0.3
|
|
205
|
+
const confidence = detected ? 0.65 + Math.random() * 0.3 : 0
|
|
206
|
+
|
|
207
|
+
return ["荧光油墨", detected, confidence]
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* 检测微缩文字
|
|
212
|
+
*
|
|
213
|
+
* @param imageData 图像数据
|
|
214
|
+
* @returns [特征名称, 是否检测到, 置信度]
|
|
215
|
+
* @private
|
|
216
|
+
*/
|
|
217
|
+
private async detectMicroText(
|
|
218
|
+
imageData: ImageData
|
|
219
|
+
): Promise<[string, boolean, number]> {
|
|
220
|
+
// 应用边缘检测突出微缩文字
|
|
221
|
+
const grayscale = ImageProcessor.toGrayscale(
|
|
222
|
+
new ImageData(
|
|
223
|
+
new Uint8ClampedArray(imageData.data),
|
|
224
|
+
imageData.width,
|
|
225
|
+
imageData.height
|
|
226
|
+
)
|
|
227
|
+
)
|
|
228
|
+
|
|
229
|
+
// 寻找特定的微缩文字模式
|
|
230
|
+
// 实际实现中应使用计算机视觉算法寻找微小规则文字模式
|
|
231
|
+
// 这里使用模拟实现
|
|
232
|
+
|
|
233
|
+
// 模拟检测: 80%的概率检测到,置信度0.7-0.95
|
|
234
|
+
const detected = Math.random() > 0.2
|
|
235
|
+
const confidence = detected ? 0.7 + Math.random() * 0.25 : 0
|
|
236
|
+
|
|
237
|
+
return ["微缩文字", detected, confidence]
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* 检测光变图案
|
|
242
|
+
*
|
|
243
|
+
* @param imageData 图像数据
|
|
244
|
+
* @returns [特征名称, 是否检测到, 置信度]
|
|
245
|
+
* @private
|
|
246
|
+
*/
|
|
247
|
+
private async detectOpticalVariable(
|
|
248
|
+
imageData: ImageData
|
|
249
|
+
): Promise<[string, boolean, number]> {
|
|
250
|
+
// 提取特定区域并分析颜色变化
|
|
251
|
+
// 在实际实现中需要定位光变图案区域并分析其特征
|
|
252
|
+
// 这里使用模拟实现
|
|
253
|
+
|
|
254
|
+
// 模拟检测: 65%的概率检测到,置信度0.6-0.9
|
|
255
|
+
const detected = Math.random() > 0.35
|
|
256
|
+
const confidence = detected ? 0.6 + Math.random() * 0.3 : 0
|
|
257
|
+
|
|
258
|
+
return ["光变图案", detected, confidence]
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* 检测凹印雕刻特征
|
|
263
|
+
*
|
|
264
|
+
* @param imageData 图像数据
|
|
265
|
+
* @returns [特征名称, 是否检测到, 置信度]
|
|
266
|
+
* @private
|
|
267
|
+
*/
|
|
268
|
+
private async detectIntaglioPrinting(
|
|
269
|
+
imageData: ImageData
|
|
270
|
+
): Promise<[string, boolean, number]> {
|
|
271
|
+
// 使用特定滤镜增强凹印效果
|
|
272
|
+
// 在实际实现中应分析阴影和纹理模式
|
|
273
|
+
// 这里使用模拟实现
|
|
274
|
+
|
|
275
|
+
// 模拟检测: 75%的概率检测到,置信度0.65-0.9
|
|
276
|
+
const detected = Math.random() > 0.25
|
|
277
|
+
const confidence = detected ? 0.65 + Math.random() * 0.25 : 0
|
|
278
|
+
|
|
279
|
+
return ["雕刻凹印", detected, confidence]
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* 检测隐形图案(幽灵图像)
|
|
284
|
+
*
|
|
285
|
+
* @param imageData 图像数据
|
|
286
|
+
* @returns [特征名称, 是否检测到, 置信度]
|
|
287
|
+
* @private
|
|
288
|
+
*/
|
|
289
|
+
private async detectGhostImage(
|
|
290
|
+
imageData: ImageData
|
|
291
|
+
): Promise<[string, boolean, number]> {
|
|
292
|
+
// 调整对比度和亮度显现隐形图案
|
|
293
|
+
// 在实际实现中应使用特定滤镜和图像处理算法
|
|
294
|
+
// 这里使用模拟实现
|
|
295
|
+
|
|
296
|
+
// 模拟检测: 60%的概率检测到,置信度0.55-0.85
|
|
297
|
+
const detected = Math.random() > 0.4
|
|
298
|
+
const confidence = detected ? 0.55 + Math.random() * 0.3 : 0
|
|
299
|
+
|
|
300
|
+
return ["隐形图案", detected, confidence]
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* 清除结果缓存
|
|
305
|
+
*/
|
|
306
|
+
clearCache(): void {
|
|
307
|
+
this.resultCache.clear()
|
|
308
|
+
this.options.logger("防伪检测结果缓存已清除")
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* 释放资源
|
|
313
|
+
*/
|
|
314
|
+
dispose(): void {
|
|
315
|
+
this.resultCache.clear()
|
|
316
|
+
}
|
|
317
|
+
}
|