-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.js
39 lines (33 loc) · 1.3 KB
/
1.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
async function sendImageToApi(imageData) {
const configUrl = 'config.json';
let config = null;
try {
const response = await fetch(configUrl);
config = await response.json();
} catch (error) {
console.error('Error loading config:', error);
throw new Error('Failed to load configuration.');
}
const requestUrl = "https://aip.baidubce.com/rest/2.0/ocr/v1/webimage";
const accessToken = config.access_token;
const requestUrlWithToken = `${requestUrl}?access_token=${accessToken}`;
const params = { image: imageData };
const headers = { 'Content-Type': 'application/x-www-form-urlencoded' };
try {
const response = await fetch(requestUrlWithToken, {
method: 'POST',
headers: headers,
body: new URLSearchParams(params)
});
if (!response.ok) throw new Error(`HTTP error! Status: ${response.status}`);
const data = await response.json();
if (data.words_result && Array.isArray(data.words_result)) {
return data.words_result.map(item => item.words).join('\n');
} else {
return '未检测到文字';
}
} catch (error) {
console.error('Error sending image to API:', error);
throw new Error('出现错误。');
}
}