id-scanner-lib 1.3.2 → 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.
- package/README.md +55 -460
- package/dist/id-scanner-lib.esm.js +4641 -0
- package/dist/id-scanner-lib.esm.js.map +1 -0
- package/dist/id-scanner-lib.js +14755 -0
- package/dist/id-scanner-lib.js.map +1 -0
- package/dist/types/core/base-module.d.ts +44 -0
- package/dist/types/core/camera-manager.d.ts +258 -0
- package/dist/types/core/config.d.ts +88 -0
- package/dist/types/core/errors.d.ts +111 -0
- package/dist/types/core/event-emitter.d.ts +55 -0
- package/dist/types/core/logger.d.ts +277 -0
- package/dist/types/core/module-manager.d.ts +78 -0
- package/dist/types/core/plugin-manager.d.ts +158 -0
- package/dist/types/core/resource-manager.d.ts +246 -0
- package/dist/types/core/result.d.ts +83 -0
- package/dist/types/core/scanner-factory.d.ts +93 -0
- package/dist/types/index.bundle.d.ts +1303 -0
- package/dist/types/index.d.ts +86 -0
- package/dist/types/interfaces/external-types.d.ts +174 -0
- package/dist/types/interfaces/face-detection.d.ts +293 -0
- package/dist/types/interfaces/scanner-module.d.ts +280 -0
- package/dist/types/modules/face/face-detector.d.ts +170 -0
- package/dist/types/modules/face/index.d.ts +56 -0
- package/dist/types/modules/face/liveness-detector.d.ts +177 -0
- package/dist/types/modules/face/types.d.ts +136 -0
- package/dist/types/modules/id-card/anti-fake-detector.d.ts +170 -0
- package/dist/types/modules/id-card/id-card-detector.d.ts +131 -0
- package/dist/types/modules/id-card/index.d.ts +89 -0
- package/dist/types/modules/id-card/ocr-processor.d.ts +110 -0
- package/dist/types/modules/id-card/ocr-worker.d.ts +31 -0
- package/dist/types/modules/id-card/types.d.ts +181 -0
- package/dist/types/modules/qrcode/index.d.ts +51 -0
- package/dist/types/modules/qrcode/qr-code-scanner.d.ts +64 -0
- package/dist/types/modules/qrcode/types.d.ts +67 -0
- package/dist/types/utils/camera.d.ts +81 -0
- package/dist/types/utils/image-processing.d.ts +176 -0
- package/dist/types/utils/index.d.ts +175 -0
- package/dist/types/utils/performance.d.ts +81 -0
- package/dist/types/utils/resource-manager.d.ts +53 -0
- package/dist/types/utils/types.d.ts +166 -0
- package/dist/types/utils/worker.d.ts +52 -0
- package/dist/types/version.d.ts +7 -0
- package/package.json +76 -77
- package/src/core/base-module.ts +78 -0
- package/src/core/camera-manager.ts +798 -0
- package/src/core/config.ts +268 -0
- package/src/core/errors.ts +174 -0
- package/src/core/event-emitter.ts +110 -0
- package/src/core/logger.ts +549 -0
- package/src/core/module-manager.ts +165 -0
- package/src/core/plugin-manager.ts +429 -0
- package/src/core/resource-manager.ts +762 -0
- package/src/core/result.ts +163 -0
- package/src/core/scanner-factory.ts +237 -0
- package/src/index.ts +113 -936
- package/src/interfaces/external-types.ts +200 -0
- package/src/interfaces/face-detection.ts +309 -0
- package/src/interfaces/scanner-module.ts +384 -0
- package/src/modules/face/face-detector.ts +931 -0
- package/src/modules/face/index.ts +208 -0
- package/src/modules/face/liveness-detector.ts +908 -0
- package/src/modules/face/types.ts +133 -0
- package/src/modules/id-card/anti-fake-detector.ts +732 -0
- package/src/modules/id-card/id-card-detector.ts +474 -0
- package/src/modules/id-card/index.ts +425 -0
- package/src/modules/id-card/ocr-processor.ts +538 -0
- package/src/modules/id-card/ocr-worker.ts +259 -0
- package/src/modules/id-card/types.ts +178 -0
- package/src/modules/qrcode/index.ts +175 -0
- package/src/modules/qrcode/qr-code-scanner.ts +230 -0
- package/src/modules/qrcode/types.ts +65 -0
- package/src/types/browser-image-compression.d.ts +19 -0
- package/src/types/tesseract.d.ts +280 -0
- package/src/utils/image-processing.ts +432 -49
- package/src/utils/index.ts +426 -0
- package/src/utils/performance.ts +168 -131
- package/src/utils/resource-manager.ts +65 -146
- package/src/utils/types.ts +90 -2
- package/src/utils/worker.ts +123 -84
- package/src/version.ts +11 -0
- package/tools/scaffold.js +543 -0
- package/dist/id-scanner-core.esm.js +0 -11076
- package/dist/id-scanner-core.esm.js.map +0 -1
- package/dist/id-scanner-core.js +0 -11088
- package/dist/id-scanner-core.js.map +0 -1
- package/dist/id-scanner-core.min.js +0 -1
- package/dist/id-scanner-core.min.js.map +0 -1
- package/dist/id-scanner-ocr.esm.js +0 -1802
- package/dist/id-scanner-ocr.esm.js.map +0 -1
- package/dist/id-scanner-ocr.js +0 -1811
- package/dist/id-scanner-ocr.js.map +0 -1
- package/dist/id-scanner-ocr.min.js +0 -1
- package/dist/id-scanner-ocr.min.js.map +0 -1
- package/dist/id-scanner-qr.esm.js +0 -1023
- package/dist/id-scanner-qr.esm.js.map +0 -1
- package/dist/id-scanner-qr.js +0 -1032
- package/dist/id-scanner-qr.js.map +0 -1
- package/dist/id-scanner-qr.min.js +0 -1
- package/dist/id-scanner-qr.min.js.map +0 -1
- package/dist/id-scanner.js +0 -3740
- package/dist/id-scanner.js.map +0 -1
- package/dist/id-scanner.min.js +0 -1
- package/dist/id-scanner.min.js.map +0 -1
- package/src/core.ts +0 -138
- package/src/demo/demo.ts +0 -204
- package/src/id-recognition/anti-fake-detector.ts +0 -317
- package/src/id-recognition/data-extractor.ts +0 -262
- package/src/id-recognition/id-detector.ts +0 -363
- package/src/id-recognition/ocr-processor.ts +0 -334
- package/src/id-recognition/ocr-worker.ts +0 -156
- package/src/index-umd.ts +0 -477
- package/src/ocr-module.ts +0 -187
- package/src/qr-module.ts +0 -179
- package/src/scanner/barcode-scanner.ts +0 -251
- package/src/scanner/qr-scanner.ts +0 -167
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file 二维码扫描器
|
|
3
|
+
* @description 提供二维码检测和解析功能
|
|
4
|
+
* @module modules/qrcode/qr-code-scanner
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { EventEmitter } from '../../core/event-emitter';
|
|
8
|
+
import { Logger } from '../../core/logger';
|
|
9
|
+
import { QRCodeResult } from './types';
|
|
10
|
+
import jsQR from 'jsqr';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 二维码扫描器配置选项
|
|
14
|
+
*/
|
|
15
|
+
export interface QRCodeScannerOptions {
|
|
16
|
+
/** 最小置信度 */
|
|
17
|
+
minConfidence?: number;
|
|
18
|
+
/** 是否返回原始图像 */
|
|
19
|
+
returnImage?: boolean;
|
|
20
|
+
/** 图像处理配置 */
|
|
21
|
+
imageProcess?: {
|
|
22
|
+
/** 是否进行预处理 */
|
|
23
|
+
preprocess?: boolean;
|
|
24
|
+
/** 是否增强对比度 */
|
|
25
|
+
enhanceContrast?: boolean;
|
|
26
|
+
/** 二值化阈值 */
|
|
27
|
+
threshold?: number;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 二维码扫描器类
|
|
33
|
+
*/
|
|
34
|
+
export class QRCodeScanner extends EventEmitter {
|
|
35
|
+
private options: QRCodeScannerOptions;
|
|
36
|
+
private logger: Logger;
|
|
37
|
+
private initialized: boolean = false;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 构造函数
|
|
41
|
+
* @param options 配置选项
|
|
42
|
+
*/
|
|
43
|
+
constructor(options: QRCodeScannerOptions = {}) {
|
|
44
|
+
super();
|
|
45
|
+
this.options = {
|
|
46
|
+
minConfidence: 0.6,
|
|
47
|
+
returnImage: false,
|
|
48
|
+
imageProcess: {
|
|
49
|
+
preprocess: true,
|
|
50
|
+
enhanceContrast: true,
|
|
51
|
+
threshold: 128,
|
|
52
|
+
...options.imageProcess
|
|
53
|
+
},
|
|
54
|
+
...options
|
|
55
|
+
};
|
|
56
|
+
this.logger = Logger.getInstance();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 初始化扫描器
|
|
61
|
+
*/
|
|
62
|
+
public async initialize(): Promise<void> {
|
|
63
|
+
if (this.initialized) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
this.logger.debug('QRCodeScanner', '初始化二维码扫描器');
|
|
68
|
+
|
|
69
|
+
// 验证jsQR是否可用
|
|
70
|
+
if (typeof jsQR !== 'function') {
|
|
71
|
+
throw new Error('jsQR库未加载,请确保已安装jsqr依赖');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
this.initialized = true;
|
|
75
|
+
this.logger.debug('QRCodeScanner', '二维码扫描器初始化完成');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 扫描图像中的二维码
|
|
80
|
+
* @param image 图像源
|
|
81
|
+
* @returns 二维码扫描结果
|
|
82
|
+
*/
|
|
83
|
+
public async scan(
|
|
84
|
+
image: ImageData | HTMLImageElement | HTMLCanvasElement
|
|
85
|
+
): Promise<QRCodeResult | undefined> {
|
|
86
|
+
if (!this.initialized) {
|
|
87
|
+
await this.initialize();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// 将输入转换为ImageData
|
|
91
|
+
const imageData = this.getImageData(image);
|
|
92
|
+
|
|
93
|
+
// 图像预处理
|
|
94
|
+
const processedImage = this.options.imageProcess?.preprocess
|
|
95
|
+
? this.preprocessImage(imageData)
|
|
96
|
+
: imageData;
|
|
97
|
+
|
|
98
|
+
// 使用jsQR进行扫描
|
|
99
|
+
const code = jsQR(
|
|
100
|
+
processedImage.data,
|
|
101
|
+
processedImage.width,
|
|
102
|
+
processedImage.height,
|
|
103
|
+
{
|
|
104
|
+
inversionAttempts: 'dontInvert'
|
|
105
|
+
}
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
if (!code) {
|
|
109
|
+
return undefined;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// 构建结果
|
|
113
|
+
const result: QRCodeResult = {
|
|
114
|
+
data: code.data,
|
|
115
|
+
boundingBox: {
|
|
116
|
+
topLeft: code.location.topLeftCorner,
|
|
117
|
+
topRight: code.location.topRightCorner,
|
|
118
|
+
bottomRight: code.location.bottomRightCorner,
|
|
119
|
+
bottomLeft: code.location.bottomLeftCorner
|
|
120
|
+
},
|
|
121
|
+
center: {
|
|
122
|
+
x: Math.round((code.location.topLeftCorner.x + code.location.bottomRightCorner.x) / 2),
|
|
123
|
+
y: Math.round((code.location.topLeftCorner.y + code.location.bottomRightCorner.y) / 2)
|
|
124
|
+
},
|
|
125
|
+
confidence: 1.0 // jsQR不提供置信度,默认为1.0
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
// 如果需要返回原始图像
|
|
129
|
+
if (this.options.returnImage) {
|
|
130
|
+
result.image = imageData;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
this.logger.debug('QRCodeScanner', `扫描到二维码: ${result.data.substring(0, 20)}${result.data.length > 20 ? '...' : ''}`);
|
|
134
|
+
|
|
135
|
+
return result;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* 将各种图像源转换为ImageData
|
|
140
|
+
* @param image 图像源
|
|
141
|
+
* @returns ImageData
|
|
142
|
+
*/
|
|
143
|
+
private getImageData(
|
|
144
|
+
image: ImageData | HTMLImageElement | HTMLCanvasElement
|
|
145
|
+
): ImageData {
|
|
146
|
+
// 如果已经是ImageData,直接返回
|
|
147
|
+
if (image instanceof ImageData) {
|
|
148
|
+
return image;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// 创建canvas并获取2D上下文
|
|
152
|
+
const canvas = document.createElement('canvas');
|
|
153
|
+
const ctx = canvas.getContext('2d');
|
|
154
|
+
|
|
155
|
+
if (!ctx) {
|
|
156
|
+
throw new Error('无法创建Canvas上下文');
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// 设置canvas尺寸
|
|
160
|
+
canvas.width = image.width;
|
|
161
|
+
canvas.height = image.height;
|
|
162
|
+
|
|
163
|
+
// 绘制图像
|
|
164
|
+
ctx.drawImage(image, 0, 0);
|
|
165
|
+
|
|
166
|
+
// 获取ImageData
|
|
167
|
+
return ctx.getImageData(0, 0, canvas.width, canvas.height);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* 图像预处理
|
|
172
|
+
* @param imageData 原始图像数据
|
|
173
|
+
* @returns 处理后的图像数据
|
|
174
|
+
*/
|
|
175
|
+
private preprocessImage(imageData: ImageData): ImageData {
|
|
176
|
+
// 创建canvas并获取2D上下文
|
|
177
|
+
const canvas = document.createElement('canvas');
|
|
178
|
+
const ctx = canvas.getContext('2d');
|
|
179
|
+
|
|
180
|
+
if (!ctx) {
|
|
181
|
+
return imageData;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// 设置canvas尺寸
|
|
185
|
+
canvas.width = imageData.width;
|
|
186
|
+
canvas.height = imageData.height;
|
|
187
|
+
|
|
188
|
+
// 绘制原始图像
|
|
189
|
+
ctx.putImageData(imageData, 0, 0);
|
|
190
|
+
|
|
191
|
+
// 增强对比度
|
|
192
|
+
if (this.options.imageProcess?.enhanceContrast) {
|
|
193
|
+
ctx.filter = 'contrast(150%)';
|
|
194
|
+
ctx.drawImage(canvas, 0, 0);
|
|
195
|
+
ctx.filter = 'none';
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// 应用二值化
|
|
199
|
+
const threshold = this.options.imageProcess?.threshold || 128;
|
|
200
|
+
const processedData = ctx.getImageData(0, 0, canvas.width, canvas.height);
|
|
201
|
+
const data = processedData.data;
|
|
202
|
+
|
|
203
|
+
for (let i = 0; i < data.length; i += 4) {
|
|
204
|
+
// 计算灰度值
|
|
205
|
+
const gray = 0.299 * data[i] + 0.587 * data[i + 1] + 0.114 * data[i + 2];
|
|
206
|
+
// 二值化
|
|
207
|
+
const value = gray < threshold ? 0 : 255;
|
|
208
|
+
data[i] = data[i + 1] = data[i + 2] = value;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return processedData;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* 释放资源
|
|
216
|
+
*/
|
|
217
|
+
public async dispose(): Promise<void> {
|
|
218
|
+
if (!this.initialized) {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
this.logger.debug('QRCodeScanner', '释放二维码扫描器资源');
|
|
223
|
+
|
|
224
|
+
// 移除所有事件监听器
|
|
225
|
+
this.removeAllListeners();
|
|
226
|
+
|
|
227
|
+
this.initialized = false;
|
|
228
|
+
this.logger.debug('QRCodeScanner', '二维码扫描器资源已释放');
|
|
229
|
+
}
|
|
230
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file 二维码模块类型定义
|
|
3
|
+
* @description 二维码模块相关的类型和接口定义
|
|
4
|
+
* @module modules/qrcode/types
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 二维码检测结果
|
|
9
|
+
*/
|
|
10
|
+
export interface QRCodeResult {
|
|
11
|
+
/** 二维码内容 */
|
|
12
|
+
data: string;
|
|
13
|
+
|
|
14
|
+
/** 二维码类型 */
|
|
15
|
+
type?: string;
|
|
16
|
+
|
|
17
|
+
/** 二维码边界框 */
|
|
18
|
+
boundingBox: {
|
|
19
|
+
topLeft: { x: number; y: number };
|
|
20
|
+
topRight: { x: number; y: number };
|
|
21
|
+
bottomRight: { x: number; y: number };
|
|
22
|
+
bottomLeft: { x: number; y: number };
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/** 二维码中心点 */
|
|
26
|
+
center: { x: number; y: number };
|
|
27
|
+
|
|
28
|
+
/** 原始图像 */
|
|
29
|
+
image?: ImageData;
|
|
30
|
+
|
|
31
|
+
/** 置信度 */
|
|
32
|
+
confidence?: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 二维码模块选项
|
|
37
|
+
*/
|
|
38
|
+
export interface QRCodeModuleOptions {
|
|
39
|
+
/** 是否启用模块 */
|
|
40
|
+
enabled?: boolean;
|
|
41
|
+
|
|
42
|
+
/** 二维码扫描配置 */
|
|
43
|
+
scanner?: {
|
|
44
|
+
/** 最小置信度 */
|
|
45
|
+
minConfidence?: number;
|
|
46
|
+
|
|
47
|
+
/** 是否尝试多次扫描 */
|
|
48
|
+
tryMultipleScan?: boolean;
|
|
49
|
+
|
|
50
|
+
/** 是否返回原始图像 */
|
|
51
|
+
returnImage?: boolean;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/** 图像处理配置 */
|
|
55
|
+
imageProcess?: {
|
|
56
|
+
/** 是否进行预处理 */
|
|
57
|
+
preprocess?: boolean;
|
|
58
|
+
|
|
59
|
+
/** 是否增强对比度 */
|
|
60
|
+
enhanceContrast?: boolean;
|
|
61
|
+
|
|
62
|
+
/** 二值化阈值 */
|
|
63
|
+
threshold?: number;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type definitions for browser-image-compression
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
declare module 'browser-image-compression' {
|
|
6
|
+
export interface Options {
|
|
7
|
+
maxSizeMB?: number;
|
|
8
|
+
maxWidthOrHeight?: number;
|
|
9
|
+
useWebWorker?: boolean;
|
|
10
|
+
maxIteration?: number;
|
|
11
|
+
quality?: number;
|
|
12
|
+
fileType?: string;
|
|
13
|
+
onProgress?: (progress: number) => void;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function imageCompression(file: File, options?: Options): Promise<File>;
|
|
17
|
+
|
|
18
|
+
export default imageCompression;
|
|
19
|
+
}
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type definitions for tesseract.js
|
|
3
|
+
*/
|
|
4
|
+
|
|
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
|
+
|
|
127
|
+
export interface WorkerOptions {
|
|
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
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export interface RecognizeResult {
|
|
138
|
+
data: Page // Use the detailed Page interface
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export interface DetectResult {
|
|
142
|
+
data: {
|
|
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
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export type ImageLike =
|
|
158
|
+
| HTMLImageElement
|
|
159
|
+
| HTMLCanvasElement
|
|
160
|
+
| File
|
|
161
|
+
| string
|
|
162
|
+
| Buffer
|
|
163
|
+
| ImageData // Common image types
|
|
164
|
+
|
|
165
|
+
export interface Worker {
|
|
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
|
|
242
|
+
}
|
|
243
|
+
|
|
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 }
|
|
280
|
+
}
|