Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ async function recognize(base64, lang, options) {
requestPath += '/v1/chat/completions';
}
if (!customPrompt) {
customPrompt = "Just recognize the text in the image. Do not offer unnecessary explanations.";
}else{
customPrompt = "Recognize and transcribe all text visible in the image. Then, on a new line after \"========\", describe and explain the contents of the image, including any relevant visual elements, context, or meaning conveyed.";
} else {
customPrompt = customPrompt.replaceAll("$lang", lang);
}

Expand All @@ -26,6 +26,45 @@ async function recognize(base64, lang, options) {
'Authorization': `Bearer ${apiKey}`
}

// 解码base64图片数据
const imgData = atob(base64);

// 如果图片大小小于1MB,则直接使用原始图片
let compressedBase64;
if (imgData.length <= 1024 * 1024) {
compressedBase64 = base64;
} else {
// 创建Image对象
const img = new Image();
img.src = `data:image/png;base64,${base64}`;
await new Promise((resolve) => {
img.onload = resolve;
});

// 压缩图片
const maxSize = 1400;
const [width, height] = [img.width, img.height];
const ratio = maxSize / Math.max(width, height);
const newWidth = Math.floor(width * ratio);
const newHeight = Math.floor(height * ratio);

const canvas = document.createElement('canvas');
canvas.width = newWidth;
canvas.height = newHeight;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, newWidth, newHeight);

// 将压缩后的图片转换为base64,并确保大小不超过1MB
let quality = 0.8;
let compressedDataUrl;
do {
compressedDataUrl = canvas.toDataURL('image/jpeg', quality);
quality -= 0.05;
} while (compressedDataUrl.length > 1024 * 1024 && quality > 0.1);

compressedBase64 = compressedDataUrl.split(',')[1];
}

const body = {
model,
messages: [
Expand All @@ -44,7 +83,7 @@ async function recognize(base64, lang, options) {
{
"type": "image_url",
"image_url": {
"url": `data:image/png;base64,${base64}`,
"url": `data:image/jpeg;base64,${compressedBase64}`,
"detail": "high"
},
},
Expand Down