id-scanner-lib 1.3.3 → 1.5.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.
Files changed (101) hide show
  1. package/README.md +55 -460
  2. package/dist/id-scanner-lib.esm.js +4641 -0
  3. package/dist/id-scanner-lib.esm.js.map +1 -0
  4. package/dist/id-scanner-lib.js +14755 -0
  5. package/dist/id-scanner-lib.js.map +1 -0
  6. package/dist/types/core/base-module.d.ts +44 -0
  7. package/dist/types/core/camera-manager.d.ts +258 -0
  8. package/dist/types/core/config.d.ts +88 -0
  9. package/dist/types/core/errors.d.ts +111 -0
  10. package/dist/types/core/event-emitter.d.ts +55 -0
  11. package/dist/types/core/logger.d.ts +277 -0
  12. package/dist/types/core/module-manager.d.ts +78 -0
  13. package/dist/types/core/plugin-manager.d.ts +158 -0
  14. package/dist/types/core/resource-manager.d.ts +246 -0
  15. package/dist/types/core/result.d.ts +83 -0
  16. package/dist/types/core/scanner-factory.d.ts +93 -0
  17. package/dist/types/index.bundle.d.ts +1303 -0
  18. package/dist/types/index.d.ts +86 -0
  19. package/dist/types/interfaces/external-types.d.ts +174 -0
  20. package/dist/types/interfaces/face-detection.d.ts +293 -0
  21. package/dist/types/interfaces/scanner-module.d.ts +280 -0
  22. package/dist/types/modules/face/face-detector.d.ts +170 -0
  23. package/dist/types/modules/face/index.d.ts +56 -0
  24. package/dist/types/modules/face/liveness-detector.d.ts +177 -0
  25. package/dist/types/modules/face/types.d.ts +136 -0
  26. package/dist/types/modules/id-card/anti-fake-detector.d.ts +170 -0
  27. package/dist/types/modules/id-card/id-card-detector.d.ts +131 -0
  28. package/dist/types/modules/id-card/index.d.ts +89 -0
  29. package/dist/types/modules/id-card/ocr-processor.d.ts +110 -0
  30. package/dist/types/modules/id-card/ocr-worker.d.ts +31 -0
  31. package/dist/types/modules/id-card/types.d.ts +181 -0
  32. package/dist/types/modules/qrcode/index.d.ts +51 -0
  33. package/dist/types/modules/qrcode/qr-code-scanner.d.ts +64 -0
  34. package/dist/types/modules/qrcode/types.d.ts +67 -0
  35. package/dist/types/utils/camera.d.ts +81 -0
  36. package/dist/types/utils/image-processing.d.ts +176 -0
  37. package/dist/types/utils/index.d.ts +175 -0
  38. package/dist/types/utils/performance.d.ts +81 -0
  39. package/dist/types/utils/resource-manager.d.ts +53 -0
  40. package/dist/types/utils/types.d.ts +166 -0
  41. package/dist/types/utils/worker.d.ts +52 -0
  42. package/dist/types/version.d.ts +7 -0
  43. package/package.json +76 -75
  44. package/src/core/base-module.ts +78 -0
  45. package/src/core/camera-manager.ts +798 -0
  46. package/src/core/config.ts +268 -0
  47. package/src/core/errors.ts +174 -0
  48. package/src/core/event-emitter.ts +110 -0
  49. package/src/core/logger.ts +549 -0
  50. package/src/core/module-manager.ts +165 -0
  51. package/src/core/plugin-manager.ts +429 -0
  52. package/src/core/resource-manager.ts +762 -0
  53. package/src/core/result.ts +163 -0
  54. package/src/core/scanner-factory.ts +237 -0
  55. package/src/index.ts +113 -936
  56. package/src/interfaces/external-types.ts +200 -0
  57. package/src/interfaces/face-detection.ts +309 -0
  58. package/src/interfaces/scanner-module.ts +384 -0
  59. package/src/modules/face/face-detector.ts +931 -0
  60. package/src/modules/face/index.ts +208 -0
  61. package/src/modules/face/liveness-detector.ts +908 -0
  62. package/src/modules/face/types.ts +133 -0
  63. package/src/{id-recognition → modules/id-card}/anti-fake-detector.ts +273 -239
  64. package/src/modules/id-card/id-card-detector.ts +474 -0
  65. package/src/modules/id-card/index.ts +425 -0
  66. package/src/{id-recognition → modules/id-card}/ocr-processor.ts +149 -92
  67. package/src/modules/id-card/ocr-worker.ts +259 -0
  68. package/src/modules/id-card/types.ts +178 -0
  69. package/src/modules/qrcode/index.ts +175 -0
  70. package/src/modules/qrcode/qr-code-scanner.ts +230 -0
  71. package/src/modules/qrcode/types.ts +65 -0
  72. package/src/types/tesseract.d.ts +265 -22
  73. package/src/utils/image-processing.ts +68 -49
  74. package/src/utils/index.ts +426 -0
  75. package/src/utils/performance.ts +168 -131
  76. package/src/utils/resource-manager.ts +65 -146
  77. package/src/utils/types.ts +90 -2
  78. package/src/utils/worker.ts +123 -84
  79. package/src/version.ts +11 -0
  80. package/tools/scaffold.js +543 -0
  81. package/dist/id-scanner-core.esm.js +0 -11349
  82. package/dist/id-scanner-core.js +0 -11361
  83. package/dist/id-scanner-core.min.js +0 -1
  84. package/dist/id-scanner-ocr.esm.js +0 -2319
  85. package/dist/id-scanner-ocr.js +0 -2328
  86. package/dist/id-scanner-ocr.min.js +0 -1
  87. package/dist/id-scanner-qr.esm.js +0 -1296
  88. package/dist/id-scanner-qr.js +0 -1305
  89. package/dist/id-scanner-qr.min.js +0 -1
  90. package/dist/id-scanner.js +0 -4561
  91. package/dist/id-scanner.min.js +0 -1
  92. package/src/core.ts +0 -138
  93. package/src/demo/demo.ts +0 -204
  94. package/src/id-recognition/data-extractor.ts +0 -262
  95. package/src/id-recognition/id-detector.ts +0 -510
  96. package/src/id-recognition/ocr-worker.ts +0 -156
  97. package/src/index-umd.ts +0 -477
  98. package/src/ocr-module.ts +0 -187
  99. package/src/qr-module.ts +0 -179
  100. package/src/scanner/barcode-scanner.ts +0 -251
  101. package/src/scanner/qr-scanner.ts +0 -167
@@ -2,36 +2,279 @@
2
2
  * Type definitions for tesseract.js
3
3
  */
4
4
 
5
- declare module 'tesseract.js' {
5
+ declare module "tesseract.js" {
6
+ // Based on https://github.com/naptha/tesseract.js/blob/master/src/index.d.ts
7
+ // and https://github.com/naptha/tesseract.js/blob/master/docs/api.md
8
+
9
+ export interface Point {
10
+ x: number
11
+ y: number
12
+ }
13
+
14
+ export interface Bbox {
15
+ x0: number
16
+ y0: number
17
+ x1: number
18
+ y1: number
19
+ }
20
+
21
+ export interface Baseline {
22
+ x0: number
23
+ y0: number
24
+ x1: number
25
+ y1: number
26
+ has_descenders: boolean
27
+ has_ascenders: boolean
28
+ }
29
+
30
+ export interface Word {
31
+ symbols: Symbol[]
32
+ choices: Choice[]
33
+ text: string
34
+ confidence: number
35
+ baseline: Baseline
36
+ bbox: Bbox
37
+ is_numeric: boolean
38
+ in_dictionary: boolean
39
+ direction: string
40
+ language: string
41
+ is_from_dictionary: boolean
42
+ is_fuzzy: boolean
43
+ is_certain: boolean
44
+ is_bold: boolean
45
+ is_italic: boolean
46
+ is_underlined: boolean
47
+ is_monospace: boolean
48
+ is_serif: boolean
49
+ is_smallcaps: boolean
50
+ font_id: number
51
+ font_size: number
52
+ font_name: string
53
+ }
54
+
55
+ export interface Symbol {
56
+ choices: Choice[]
57
+ image: null | HTMLImageElement // Or string if it's a path/URL
58
+ text: string
59
+ confidence: number
60
+ baseline: Baseline
61
+ bbox: Bbox
62
+ is_superscript: boolean
63
+ is_subscript: boolean
64
+ is_dropcap: boolean
65
+ }
66
+
67
+ export interface Choice {
68
+ text: string
69
+ confidence: number
70
+ }
71
+
72
+ export interface Line {
73
+ words: Word[]
74
+ text: string
75
+ confidence: number
76
+ baseline: Baseline
77
+ bbox: Bbox
78
+ }
79
+
80
+ export interface Paragraph {
81
+ lines: Line[]
82
+ text: string
83
+ confidence: number
84
+ baseline: Baseline
85
+ bbox: Bbox
86
+ is_ltr: boolean
87
+ }
88
+
89
+ export interface Block {
90
+ paragraphs: Paragraph[]
91
+ lines: Line[]
92
+ words: Word[]
93
+ text: string
94
+ confidence: number
95
+ baseline: Baseline
96
+ bbox: Bbox
97
+ blocktype: string
98
+ polygon: Point[]
99
+ }
100
+
101
+ export interface Page {
102
+ blocks: Block[]
103
+ confidence: number
104
+ html: string // HTML representation of the page
105
+ jobId?: string
106
+ text: string
107
+ lines: Line[]
108
+ oem: string
109
+ operator: string
110
+ paragraphs: Paragraph[]
111
+ psm: string
112
+ symbols: Symbol[]
113
+ version: string
114
+ words: Word[]
115
+ hocr?: string // hOCR output
116
+ tsv?: string // TSV output
117
+ }
118
+
119
+ export interface LoggerMessage {
120
+ jobId?: string
121
+ workerId?: string
122
+ status: string
123
+ progress: number
124
+ userfriendlyText?: string
125
+ }
126
+
6
127
  export interface WorkerOptions {
7
- langPath?: string;
8
- corePath?: string;
9
- workerPath?: string;
10
- logger?: (message: any) => void;
11
- errorHandler?: (error: Error) => void;
128
+ langPath?: string
129
+ corePath?: string
130
+ workerPath?: string
131
+ logger?: (message: LoggerMessage) => void // More specific type for logger message <mcreference index="1" link="https://github.com/naptha/tesseract.js/blob/master/docs/api.md"></mcreference>
132
+ errorHandler?: (error: Error) => void
133
+ // Add other options based on documentation if needed
134
+ [key: string]: any // For other less common or dynamic options
12
135
  }
13
136
 
14
137
  export interface RecognizeResult {
138
+ data: Page // Use the detailed Page interface
139
+ }
140
+
141
+ export interface DetectResult {
15
142
  data: {
16
- text: string;
17
- hocr: string;
18
- tsv: string;
19
- confidence: number;
20
- blocks: any[];
21
- lines: any[];
22
- words: any[];
23
- symbols: any[];
24
- };
143
+ tesseract_script_id: number | null
144
+ script: string | null
145
+ script_confidence: number | null
146
+ orientation_degrees: number | null
147
+ orientation_confidence: number | null
148
+ }
149
+ jobId?: string
150
+ }
151
+
152
+ export interface ConfigResult {
153
+ data: null
154
+ jobId?: string
25
155
  }
26
156
 
157
+ export type ImageLike =
158
+ | HTMLImageElement
159
+ | HTMLCanvasElement
160
+ | File
161
+ | string
162
+ | Buffer
163
+ | ImageData // Common image types
164
+
27
165
  export interface Worker {
28
- load(): Promise<any>;
29
- loadLanguage(lang: string): Promise<any>;
30
- initialize(lang: string): Promise<any>;
31
- setParameters(params: Record<string, any>): Promise<any>;
32
- recognize(image: HTMLCanvasElement | ImageData | Buffer | string): Promise<RecognizeResult>;
33
- terminate(): Promise<any>;
166
+ load(jobId?: string): Promise<ConfigResult> // <mcreference index="4" link="https://github.com/naptha/tesseract.js/blob/master/src/index.d.ts"></mcreference>
167
+ loadLanguage(
168
+ langs?: string | string[],
169
+ jobId?: string
170
+ ): Promise<ConfigResult> // <mcreference index="4" link="https://github.com/naptha/tesseract.js/blob/master/src/index.d.ts"></mcreference>
171
+ initialize(
172
+ langs?: string | string[],
173
+ oem?: OEM,
174
+ config?: string | Partial<InitOptions>,
175
+ jobId?: string
176
+ ): Promise<ConfigResult> // <mcreference index="4" link="https://github.com/naptha/tesseract.js/blob/master/src/index.d.ts"></mcreference>
177
+ setParameters(
178
+ params: Partial<Parameters>,
179
+ jobId?: string
180
+ ): Promise<ConfigResult> // <mcreference index="4" link="https://github.com/naptha/tesseract.js/blob/master/src/index.d.ts"></mcreference>
181
+ recognize(
182
+ image: ImageLike,
183
+ options?: Partial<RecognizeOptions>,
184
+ output?: Partial<OutputFormats>,
185
+ jobId?: string
186
+ ): Promise<RecognizeResult> // <mcreference index="4" link="https://github.com/naptha/tesseract.js/blob/master/src/index.d.ts"></mcreference>
187
+ detect(
188
+ image: ImageLike,
189
+ options?: Partial<WorkerOptions>,
190
+ jobId?: string
191
+ ): Promise<DetectResult> // <mcreference index="4" link="https://github.com/naptha/tesseract.js/blob/master/src/index.d.ts"></mcreference>
192
+ terminate(jobId?: string): Promise<ConfigResult> // <mcreference index="4" link="https://github.com/naptha/tesseract.js/blob/master/src/index.d.ts"></mcreference>
193
+ // Add other worker methods if present in the version you are targeting
194
+ // Example from docs for other methods like FS operations:
195
+ writeText?(
196
+ path: string,
197
+ text: string,
198
+ jobId?: string
199
+ ): Promise<ConfigResult>
200
+ readText?(path: string, jobId?: string): Promise<ConfigResult>
201
+ removeFile?(path: string, jobId?: string): Promise<ConfigResult> // Assuming removeFile also returns ConfigResult or similar
202
+ FS?(method: string, args: any[], jobId?: string): Promise<any> // FS is more generic
203
+ }
204
+
205
+ // Based on Tesseract's OEM and PSM enums
206
+ export enum OEM {
207
+ TESSERACT_ONLY = 0,
208
+ LSTM_ONLY = 1,
209
+ TESSERACT_LSTM_COMBINED = 2,
210
+ DEFAULT = 3,
211
+ }
212
+
213
+ export enum PSM {
214
+ OSD_ONLY = 0,
215
+ AUTO_OSD = 1,
216
+ AUTO_ONLY = 2,
217
+ AUTO = 3,
218
+ SINGLE_COLUMN = 4,
219
+ SINGLE_BLOCK_VERT_TEXT = 5,
220
+ SINGLE_BLOCK = 6,
221
+ SINGLE_LINE = 7,
222
+ SINGLE_WORD = 8,
223
+ CIRCLE_WORD = 9,
224
+ SINGLE_CHAR = 10,
225
+ SPARSE_TEXT = 11,
226
+ SPARSE_TEXT_OSD = 12,
227
+ RAW_LINE = 13,
228
+ }
229
+
230
+ export interface Parameters {
231
+ tessedit_char_whitelist?: string
232
+ tessedit_pageseg_mode?: PSM
233
+ // Add other Tesseract parameters as needed
234
+ [key: string]: any // For flexibility with other parameters
235
+ }
236
+
237
+ export interface RecognizeOptions {
238
+ rectangle?: Bbox // For recognizing a specific region
239
+ rectangles?: Bbox[] // For recognizing multiple regions
240
+ // Add other recognize specific options
241
+ [key: string]: any
34
242
  }
35
243
 
36
- export function createWorker(options?: WorkerOptions): Worker;
244
+ export interface OutputFormats {
245
+ text?: boolean
246
+ blocks?: boolean
247
+ hocr?: boolean
248
+ tsv?: boolean
249
+ pdf?: boolean // If PDF output is supported
250
+ // Add other output formats
251
+ [key: string]: any
252
+ }
253
+
254
+ export interface InitOptions {
255
+ load_system_dawg?: boolean
256
+ load_freq_dawg?: boolean
257
+ load_punc_dawg?: boolean
258
+ load_number_dawg?: boolean
259
+ load_unambig_dawg?: boolean
260
+ load_bigram_dawg?: boolean
261
+ load_fixed_length_dawgs?: boolean
262
+ // Add other init-only parameters
263
+ [key: string]: any
264
+ }
265
+
266
+ export function createWorker(options?: Partial<WorkerOptions>): Worker // 修正返回类型为 Worker 而非 Promise<Worker>
267
+ export function setLogging(logging: boolean): void
268
+ export function recognize(
269
+ image: ImageLike,
270
+ langs?: string | string[],
271
+ options?: Partial<RecognizeOptions & WorkerOptions>
272
+ ): Promise<RecognizeResult>
273
+ export function detect(
274
+ image: ImageLike,
275
+ options?: Partial<WorkerOptions>
276
+ ): Promise<DetectResult>
277
+
278
+ // 正确导出 OEM 和 PSM 枚举
279
+ export { OEM, PSM }
37
280
  }
@@ -6,6 +6,7 @@
6
6
  */
7
7
 
8
8
  import imageCompression from "browser-image-compression"
9
+ import { Point, Rect, ImageProcessingOptions } from './types';
9
10
 
10
11
  /**
11
12
  * 图像处理器配置选项
@@ -481,68 +482,86 @@ export class ImageProcessor {
481
482
  }
482
483
 
483
484
  /**
484
- * 调整图像大小
485
- *
486
- * @param imageData 原始图像数据
485
+ * 将图像调整到指定大小
486
+ * @param image 输入图像
487
487
  * @param maxWidth 最大宽度
488
488
  * @param maxHeight 最大高度
489
- * @param maintainAspectRatio 是否保持宽高比
490
- * @returns ImageData 调整大小后的图像数据
489
+ * @param keepAspectRatio 是否保持宽高比
490
+ * @returns 调整后的图像
491
491
  */
492
- static resizeImage(
493
- imageData: ImageData,
492
+ public static resizeImage(
493
+ image: ImageData | HTMLImageElement | HTMLCanvasElement,
494
494
  maxWidth: number,
495
495
  maxHeight: number,
496
- maintainAspectRatio: boolean = true
496
+ keepAspectRatio: boolean = true
497
497
  ): ImageData {
498
- const { width, height } = imageData
499
-
500
- // 如果图像已经小于指定大小,则不需要调整
501
- if (width <= maxWidth && height <= maxHeight) {
502
- return imageData
498
+ // 创建canvas元素
499
+ const canvas = document.createElement('canvas');
500
+ const ctx = canvas.getContext('2d');
501
+
502
+ if (!ctx) {
503
+ throw new Error('无法创建Canvas上下文');
503
504
  }
504
-
505
- let newWidth = maxWidth
506
- let newHeight = maxHeight
507
-
508
- // 计算新的尺寸,保持宽高比
509
- if (maintainAspectRatio) {
510
- const ratio = Math.min(maxWidth / width, maxHeight / height)
511
- newWidth = Math.floor(width * ratio)
512
- newHeight = Math.floor(height * ratio)
505
+
506
+ // 获取图像尺寸
507
+ let width: number;
508
+ let height: number;
509
+
510
+ if (image instanceof ImageData) {
511
+ width = image.width;
512
+ height = image.height;
513
+ } else {
514
+ width = image.width;
515
+ height = image.height;
513
516
  }
514
-
515
- // 创建用于调整大小的Canvas
516
- const canvas = document.createElement("canvas")
517
- canvas.width = newWidth
518
- canvas.height = newHeight
519
-
520
- const ctx = canvas.getContext("2d")
521
- if (!ctx) {
522
- throw new Error("无法创建2D上下文")
517
+
518
+ // 计算调整后的尺寸
519
+ let newWidth = width;
520
+ let newHeight = height;
521
+
522
+ if (keepAspectRatio) {
523
+ if (width > height) {
524
+ if (width > maxWidth) {
525
+ newHeight = Math.round(height * (maxWidth / width));
526
+ newWidth = maxWidth;
527
+ }
528
+ } else {
529
+ if (height > maxHeight) {
530
+ newWidth = Math.round(width * (maxHeight / height));
531
+ newHeight = maxHeight;
532
+ }
533
+ }
534
+ } else {
535
+ newWidth = Math.min(width, maxWidth);
536
+ newHeight = Math.min(height, maxHeight);
523
537
  }
524
-
525
- // 创建临时Canvas绘制原始ImageData
526
- const tempCanvas = document.createElement("canvas")
527
- tempCanvas.width = width
528
- tempCanvas.height = height
529
-
530
- const tempCtx = tempCanvas.getContext("2d")
538
+
539
+ // 设置canvas尺寸
540
+ canvas.width = newWidth;
541
+ canvas.height = newHeight;
542
+
543
+ // 绘制调整后的图像
544
+ if (image instanceof ImageData) {
545
+ // 创建临时canvas存储ImageData
546
+ const tempCanvas = document.createElement('canvas');
547
+ const tempCtx = tempCanvas.getContext('2d');
548
+
531
549
  if (!tempCtx) {
532
- throw new Error("无法创建临时2D上下文")
550
+ throw new Error('无法创建临时Canvas上下文');
533
551
  }
534
552
 
535
- tempCtx.putImageData(imageData, 0, 0)
536
-
537
- // 使用缩放平滑算法
538
- ctx.imageSmoothingEnabled = true
539
- ctx.imageSmoothingQuality = "high"
540
-
541
- // 绘制调整大小的图像
542
- ctx.drawImage(tempCanvas, 0, 0, width, height, 0, 0, newWidth, newHeight)
553
+ tempCanvas.width = image.width;
554
+ tempCanvas.height = image.height;
555
+ tempCtx.putImageData(image, 0, 0);
556
+
557
+ // 绘制调整后的图像
558
+ ctx.drawImage(tempCanvas, 0, 0, width, height, 0, 0, newWidth, newHeight);
559
+ } else {
560
+ ctx.drawImage(image, 0, 0, width, height, 0, 0, newWidth, newHeight);
561
+ }
543
562
 
544
- // 获取新的ImageData
545
- return ctx.getImageData(0, 0, newWidth, newHeight)
563
+ // 返回调整后的ImageData
564
+ return ctx.getImageData(0, 0, newWidth, newHeight);
546
565
  }
547
566
 
548
567
  /**