From aed5362e86d759e662f40003128aa095e94f9f51 Mon Sep 17 00:00:00 2001 From: "DESKTOP-1R2VSQH\\Lenovo ThinkPad E490" Date: Wed, 2 Apr 2025 13:29:28 +0700 Subject: [PATCH] fix .jpg --- src/views/HomeView.vue | 40 +++------------------------------------- 1 file changed, 3 insertions(+), 37 deletions(-) diff --git a/src/views/HomeView.vue b/src/views/HomeView.vue index 7119344..10431ec 100644 --- a/src/views/HomeView.vue +++ b/src/views/HomeView.vue @@ -261,48 +261,14 @@ async function capturePhoto() { 0.5 ) 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 fileName = 'photo.jpg' //ไฟล์รูป - const file = new File([resizedBlob], fileName, { type: 'image/png' }) + const file = new File([imageBlob], fileName, { type: 'image/png' }) fileImg.value = file //แสดงรูป await camera.value?.stop() - const url = URL.createObjectURL(resizedBlob) + const url = URL.createObjectURL(imageBlob) img.value = url - console.log(file) } /** function เปลี่ยนรูปภาพ*/