From 6f07e15a78ca3969628b70877b5c7a81a393fe9e Mon Sep 17 00:00:00 2001 From: "DESKTOP-1R2VSQH\\Lenovo ThinkPad E490" Date: Wed, 2 Apr 2025 10:54:13 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B8=9B=E0=B8=A3=E0=B8=B1=E0=B8=9A=E0=B8=82?= =?UTF-8?q?=E0=B8=99=E0=B8=B2=E0=B8=94=E0=B9=84=E0=B8=9F=E0=B8=A5=E0=B9=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/views/HomeView.vue | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/src/views/HomeView.vue b/src/views/HomeView.vue index 80fdb83..7119344 100644 --- a/src/views/HomeView.vue +++ b/src/views/HomeView.vue @@ -262,13 +262,47 @@ async function capturePhoto() { ) if (!imageBlob) return const fileName = 'photo.png' + + // โหลดรูปเข้าไปใน Canvas + const imgElement = new Image() + imgElement.src = URL.createObjectURL(imageBlob) + await imgElement.decode() + + // ปรับขนาดไม่เกิน 1024px + const maxSize = 1024 + let { width, height } = imgElement + if (width > maxSize || height > maxSize) { + if (width > height) { + height *= maxSize / width + width = maxSize + } else { + width *= maxSize / height + height = maxSize + } + } + + // วาดรูปลง Canvas + const canvas = document.createElement('canvas') + canvas.width = width + canvas.height = height + const ctx = canvas.getContext('2d') + if (ctx) { + ctx.drawImage(imgElement, 0, 0, width, height) + } + + // แปลง Canvas กลับเป็น Blob + const resizedBlob: Blob = await new Promise((resolve) => + canvas.toBlob((blob) => resolve(blob!), 'image/png', 0.8) + ) + //ไฟล์รูป - const file = new File([imageBlob], fileName, { type: 'image/png' }) + const file = new File([resizedBlob], fileName, { type: 'image/png' }) fileImg.value = file //แสดงรูป await camera.value?.stop() - const url = URL.createObjectURL(imageBlob) + const url = URL.createObjectURL(resizedBlob) img.value = url + console.log(file) } /** function เปลี่ยนรูปภาพ*/