-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathocrRouter.js
More file actions
225 lines (200 loc) · 7.57 KB
/
Copy pathocrRouter.js
File metadata and controls
225 lines (200 loc) · 7.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
const express = require('express');
const router = express.Router();
const multiparty = require('multiparty');
const fs = require('fs');
const afs = require('fs-extra');
const path = require('path');
const tesr = require('node-tesr');
// 设置Tesseract数据目录
process.env.TESSDATA_PREFIX = path.join(__dirname, 'tessdata');
// 图片OCR识别接口
router.post('/imgOcr', async (req, res) => {
try {
// 清空临时文件夹
try {
await afs.emptyDirSync(path.join(__dirname, 'tempImgs'));
console.log('清空tempImgs成功');
} catch (error) {
console.log('清空tempImgs失败:', error);
}
// 创建表单解析对象
let form = new multiparty.Form();
// 设置文件存储路径
form.uploadDir = path.join(__dirname, 'tempImgs');
// 解析表单
form.parse(req, async (err, fields, files) => {
try {
// 参数验证
if (!fields) {
return res.status(400).json({
code: -11,
msg: '参数错误, 请检查参数是否正确',
data: {}
});
}
// 验证文件是否存在
if (!files || !files.file || !files.file[0]) {
return res.status(400).json({
code: -1,
msg: '请上传file图片资源(form-data格式)',
data: {}
});
}
// 图片大小限制 (3MB)
if (files.file[0].size > 1024 * 1024 * 3) {
return res.status(400).json({
code: -2,
msg: '被检测图片最大3M',
data: {}
});
}
// 验证图片类型
let imgReg = /\S+\.(png|jpeg|jpg)$/g;
let originImgName = files.file[0].originalFilename || files.file[0].path;
if (!imgReg.test(originImgName)) {
return res.status(400).json({
code: -3,
msg: '仅仅支持(png、jpeg、jpg)类型图片检测',
data: {}
});
}
// 语言设置
let langArr = ['chi_sim', 'eng'];
let userLang = fields.lang || [];
if (userLang[0] && !langArr.includes(userLang[0])) {
userLang[0] = langArr[0];
}
let lang = userLang[0] || 'chi_sim';
// 确认文件路径存在
if (!files || !files.file[0] || !files.file[0].path || !fs.existsSync(files.file[0].path)) {
return res.status(400).json({
code: -777,
msg: 'OCR识别失败(file path is bad)',
data: {}
});
}
// 执行OCR识别
tesr(files.file[0].path, { l: lang, psm: 3 }, async function(err, data) {
try {
// 识别完成后清空临时文件夹
await afs.emptyDirSync(path.join(__dirname, 'tempImgs'));
console.log('清空tempImgs成功');
} catch (error) {
console.log('清空tempImgs失败:', error);
}
if (err) {
return res.status(500).json({
code: -4,
msg: 'OCR识别失败',
data: {}
});
}
// 返回识别结果
res.json({
code: 0,
msg: 'OCR识别成功',
data: {
text: data
}
});
});
} catch (error) {
console.log('OCR处理异常:', error);
return res.status(500).json({
code: -91,
msg: 'OCR识别失败',
data: {}
});
}
});
} catch (error) {
console.log('OCR接口异常:', error);
try {
await afs.emptyDirSync(path.join(__dirname, 'tempImgs'));
console.log('清空tempImgs成功2');
} catch (error) {
console.log('清空tempImgs失败2:', error);
}
return res.status(500).json({
code: -9999,
msg: 'OCR识别失败',
data: {}
});
}
});
// Base64图片OCR识别接口
router.post('/base64Ocr', async (req, res) => {
try {
const { base64Image, lang } = req.body;
// 参数验证
if (!base64Image || typeof base64Image !== 'string') {
return res.status(400).json({
code: -1,
msg: '请提供有效的base64图片',
data: {}
});
}
// 验证base64格式
if (!base64Image.match(/^data:image\/(jpeg|png|jpg);base64,/)) {
return res.status(400).json({
code: -21,
msg: '请传入有效的Base64格式的图片,只支持jpeg、png、jpg格式的图片',
data: {}
});
}
// 清空临时文件夹
try {
await afs.emptyDirSync(path.join(__dirname, 'tempImgs'));
} catch (error) {
console.log('清空tempImgs失败:', error);
}
// 创建临时文件
const tempImgPath = path.join(__dirname, 'tempImgs', `temp_${Date.now()}.jpg`);
// 提取base64数据
const base64Data = base64Image.replace(/^data:image\/(jpeg|png|jpg);base64,/, '');
// 写入临时文件
fs.writeFileSync(tempImgPath, Buffer.from(base64Data, 'base64'));
// 设置语言
let ocrLang = lang || 'chi_sim';
if (!['chi_sim', 'eng'].includes(ocrLang)) {
ocrLang = 'chi_sim';
}
// 执行OCR识别
tesr(tempImgPath, { l: ocrLang, psm: 3 }, async function(err, data) {
try {
// 识别完成后清空临时文件夹
await afs.emptyDirSync(path.join(__dirname, 'tempImgs'));
} catch (error) {
console.log('清空tempImgs失败:', error);
}
if (err) {
return res.status(500).json({
code: -4,
msg: 'OCR识别失败',
data: {}
});
}
// 返回识别结果
res.json({
code: 0,
msg: 'OCR识别成功',
data: {
text: data
}
});
});
} catch (error) {
console.log('Base64 OCR接口异常:', error);
try {
await afs.emptyDirSync(path.join(__dirname, 'tempImgs'));
} catch (error) {
console.log('清空tempImgs失败:', error);
}
return res.status(500).json({
code: -9999,
msg: 'OCR识别失败',
data: {}
});
}
});
module.exports = router;