-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.js
49 lines (46 loc) · 1.29 KB
/
main.js
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
async function recognize(base64, lang, options) {
const { config, utils } = options;
const { tauriFetch } = utils;
let { apikey, engine } = config;
base64 = `data:image/png;base64,${base64}`;
if (apikey === undefined || apikey.length === 0) {
throw "apikey not found";
}
if (engine === undefined || engine.length === 0) {
engine = "1";
}
let res = await tauriFetch('https://api.ocr.space/parse/image', {
method: "POST",
header: {
apikey,
"content-type": "application/x-www-form-urlencoded"
},
body: {
type: "Form",
payload: {
base64Image: base64,
OCREngine: engine,
language: lang
}
}
})
if (res.ok) {
const { result } = res.data;
const { ErrorMessage, ParsedResults } = result;
if (ErrorMessage) {
throw ErrorMessage;
}
if (ParsedResults) {
let target = "";
for (let i in ParsedResults) {
const { ParsedText } = i;
target += ParsedText;
}
return target;
} else {
throw JSON.stringify(result);
}
} else {
throw JSON.stringify(res);
}
}