fix .jpg
This commit is contained in:
parent
6f07e15a78
commit
aed5362e86
1 changed files with 3 additions and 37 deletions
|
|
@ -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 เปลี่ยนรูปภาพ*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue