From 19d7ffa5f98877a022ca2439c8d5c3bf52a61ddf Mon Sep 17 00:00:00 2001 From: lehhair <82690391+lehhair@users.noreply.github.com> Date: Tue, 13 Aug 2024 13:16:07 +0800 Subject: [PATCH] Update main.js --- main.js | 45 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/main.js b/main.js index 02d9047..2162986 100644 --- a/main.js +++ b/main.js @@ -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); } @@ -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: [ @@ -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" }, },