From 9efac056e93aa7e3ba358814e30570148cfbb6f9 Mon Sep 17 00:00:00 2001 From: waruneeauy Date: Thu, 7 May 2026 16:22:58 +0700 Subject: [PATCH 1/8] fix ios live mode --- src/views/HomeView.vue | 13 +++++++++++++ src/views/MapView.vue | 10 ++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/views/HomeView.vue b/src/views/HomeView.vue index ab7257f..8067899 100644 --- a/src/views/HomeView.vue +++ b/src/views/HomeView.vue @@ -890,6 +890,8 @@ watch( :resolution="{ width: photoWidth, height: photoHeight }" ref="camera" :autoplay="false" + :playsinline="true" + :facingMode="'user'" :style="!img ? 'display: block' : 'display: none'" /> @@ -1004,6 +1006,8 @@ watch( }" ref="camera" :autoplay="false" + :playsinline="true" + :facingMode="'user'" :style="!img ? 'display: block' : 'display: none'" /> @@ -1513,6 +1517,15 @@ watch( height: 100%; } +/* iOS-specific video fixes */ +.card-container video, +.card-container-xs video { + width: 100%; + height: 100%; + object-fit: cover; + -webkit-object-fit: cover; +} + .bg-topOut { background: rgb(39, 50, 56); background: linear-gradient( diff --git a/src/views/MapView.vue b/src/views/MapView.vue index 340308b..1b4c40f 100644 --- a/src/views/MapView.vue +++ b/src/views/MapView.vue @@ -335,6 +335,8 @@ onMounted(async () => { :resolution="{ width: photoWidth, height: photoHeight }" ref="camera" :autoplay="false" + :playsinline="true" + :facingMode="'user'" :style="!img ? 'display: block' : 'display: none'" /> @@ -569,4 +571,12 @@ onMounted(async () => { width: 100%; height: 100%; } + +/* iOS-specific video fixes */ +.card-container video { + width: 100%; + height: 100%; + object-fit: cover; + -webkit-object-fit: cover; +} From b11e660bcf0435bde9414c7dd5b5ef3acb167474 Mon Sep 17 00:00:00 2001 From: waruneeauy Date: Thu, 7 May 2026 16:47:35 +0700 Subject: [PATCH 2/8] fix(checkin): improve camera functionality and fix iOS Live Mode issue - Add playsinline prop to fix iOS Live Mode photo capture issue - Change default camera to front camera (facingMode: 'user') - Improve photo quality: change from PNG to JPEG with quality 0.8 - Fix memory leak: add URL.revokeObjectURL() cleanup - Add error handling for camera operations (openCamera, capturePhoto, refreshPhoto) - Add timestamp to photo filename for uniqueness Co-Authored-By: Claude Opus 4.7 --- src/views/HomeView.vue | 94 ++++++++++++++++++++++++++++-------------- src/views/MapView.vue | 76 +++++++++++++++++++++++----------- 2 files changed, 114 insertions(+), 56 deletions(-) diff --git a/src/views/HomeView.vue b/src/views/HomeView.vue index 8067899..dc4e8da 100644 --- a/src/views/HomeView.vue +++ b/src/views/HomeView.vue @@ -347,18 +347,23 @@ async function openCamera() { } if (!isPermissionCameraDenied.value) { - // change camera device - if (cameraIsOn.value) { - camera.value?.stop() - } else { - await camera.value?.start() - const devices: any = await camera.value?.devices(['videoinput']) - if (devices) { - availableCameras.value = devices - await changeCamera() + try { + // change camera device + if (cameraIsOn.value) { + camera.value?.stop() + } else { + await camera.value?.start() + const devices: any = await camera.value?.devices(['videoinput']) + if (devices) { + availableCameras.value = devices + await changeCamera() + } } + cameraIsOn.value = !cameraIsOn.value + } catch (error) { + console.error('Error opening camera:', error) + messageError($q, error, 'ไม่สามารถเปิดกล้องได้ กรุณาลองใหม่อีกครั้ง') } - cameraIsOn.value = !cameraIsOn.value } else { messageError( $q, @@ -428,26 +433,49 @@ async function switchCamera() { /** function ถ่ายรูป*/ async function capturePhoto() { - const imageBlob: any = await camera.value?.snapshot( - { width: photoWidth.value, height: photoHeight.value }, - 'image/png', - 0.5 - ) - if (!imageBlob) return - const fileName = 'photo.jpg' - //ไฟล์รูป - const file = new File([imageBlob], fileName, { type: 'image/png' }) - fileImg.value = file - //แสดงรูป - camera.value?.stop() - const url = URL.createObjectURL(imageBlob) - img.value = url + try { + const imageBlob: any = await camera.value?.snapshot( + { width: photoWidth.value, height: photoHeight.value }, + 'image/jpeg', + 0.8 + ) + if (!imageBlob) { + messageError($q, '', 'ไม่สามารถถ่ายรูปได้ กรุณาลองใหม่อีกครั้ง') + return + } + const fileName = `photo_${Date.now()}.jpg` + //ไฟล์รูป + const file = new File([imageBlob], fileName, { type: 'image/jpeg' }) + fileImg.value = file + + // ยกเลิก URL เก่าก่อนสร้างใหม่ (ป้องกัน memory leak) + if (img.value) { + URL.revokeObjectURL(img.value) + } + + //แสดงรูป + camera.value?.stop() + const url = URL.createObjectURL(imageBlob) + img.value = url + } catch (error) { + console.error('Error capturing photo:', error) + messageError($q, error, 'ไม่สามารถถ่ายรูปได้ กรุณาลองใหม่อีกครั้ง') + } } /** function เปลี่ยนรูปภาพ*/ -function refreshPhoto() { - img.value = undefined - camera.value?.start() +async function refreshPhoto() { + try { + // ยกเลิก URL เก่า (ป้องกัน memory leak) + if (img.value) { + URL.revokeObjectURL(img.value) + img.value = undefined + } + await camera.value?.start() + } catch (error) { + console.error('Error refreshing photo:', error) + messageError($q, error, 'ไม่สามารถเปิดกล้องได้ กรุณาลองใหม่อีกครั้ง') + } } /** ref validate*/ @@ -627,7 +655,11 @@ async function onClickConfirm() { try { showLoader() cameraIsOn.value = false - img.value = undefined + // ยกเลิก URL เก่า (ป้องกัน memory leak) + if (img.value) { + URL.revokeObjectURL(img.value) + img.value = undefined + } modalTime.value = false if (!statusCheckin.value) { statusCheckin.value = true @@ -665,7 +697,9 @@ const inQueue = ref(false) // ฟังก์ชันสำหรับรีเซ็ตรูปและหยุดกล้อง function resetCameraAndImage() { + // ยกเลิก URL เก่า (ป้องกัน memory leak) if (img.value) { + URL.revokeObjectURL(img.value) img.value = undefined } if (cameraIsOn.value && camera.value) { @@ -885,14 +919,12 @@ watch(
- @@ -998,7 +1030,6 @@ watch(
- diff --git a/src/views/MapView.vue b/src/views/MapView.vue index 1b4c40f..5f7edc9 100644 --- a/src/views/MapView.vue +++ b/src/views/MapView.vue @@ -133,14 +133,19 @@ const photoHeight = ref(350) /** function เปิดกล้อง */ async function openCamera() { - // change camera device - if (cameraIsOn.value) { - camera.value?.stop() - } else { - await camera.value?.start() - changeCamera() + try { + // change camera device + if (cameraIsOn.value) { + camera.value?.stop() + } else { + await camera.value?.start() + changeCamera() + } + cameraIsOn.value = !cameraIsOn.value + } catch (error) { + console.error('Error opening camera:', error) + messageError($q, error, 'ไม่สามารถเปิดกล้องได้ กรุณาลองใหม่อีกครั้ง') } - cameraIsOn.value = !cameraIsOn.value } /** change camera device */ @@ -152,25 +157,49 @@ async function changeCamera() { /** function ถ่ายรูป*/ async function capturePhoto() { - const imageBlob: any = await camera.value?.snapshot( - { width: photoWidth.value, height: photoHeight.value }, - 'image/png', - 0.5 - ) - const fileName = 'photo.png' - //ไฟล์รูป - const file = new File([imageBlob], fileName, { type: 'image/png' }) - fileImg.value = file - //แสดงรูป - camera.value?.stop() - const url = URL.createObjectURL(imageBlob) - img.value = url + try { + const imageBlob: any = await camera.value?.snapshot( + { width: photoWidth.value, height: photoHeight.value }, + 'image/jpeg', + 0.8 + ) + if (!imageBlob) { + messageError($q, '', 'ไม่สามารถถ่ายรูปได้ กรุณาลองใหม่อีกครั้ง') + return + } + const fileName = `photo_${Date.now()}.jpg` + //ไฟล์รูป + const file = new File([imageBlob], fileName, { type: 'image/jpeg' }) + fileImg.value = file + + // ยกเลิก URL เก่าก่อนสร้างใหม่ (ป้องกัน memory leak) + if (img.value) { + URL.revokeObjectURL(img.value) + } + + //แสดงรูป + camera.value?.stop() + const url = URL.createObjectURL(imageBlob) + img.value = url + } catch (error) { + console.error('Error capturing photo:', error) + messageError($q, error, 'ไม่สามารถถ่ายรูปได้ กรุณาลองใหม่อีกครั้ง') + } } /** function เปลี่ยนรูปภาพ*/ -function refreshPhoto() { - img.value = undefined - camera.value?.start() +async function refreshPhoto() { + try { + // ยกเลิก URL เก่า (ป้องกัน memory leak) + if (img.value) { + URL.revokeObjectURL(img.value) + img.value = undefined + } + await camera.value?.start() + } catch (error) { + console.error('Error refreshing photo:', error) + messageError($q, error, 'ไม่สามารถเปิดกล้องได้ กรุณาลองใหม่อีกครั้ง') + } } /** ref validate*/ @@ -337,7 +366,6 @@ onMounted(async () => { :autoplay="false" :playsinline="true" :facingMode="'user'" - :style="!img ? 'display: block' : 'display: none'" /> From 9fde2b3984d84e8f58f491d6619a1ec2ea9ecdd2 Mon Sep 17 00:00:00 2001 From: waruneeauy Date: Fri, 8 May 2026 11:13:39 +0700 Subject: [PATCH 3/8] Fix mobile photo capture fallbacks Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/views/HomeView.vue | 276 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 244 insertions(+), 32 deletions(-) diff --git a/src/views/HomeView.vue b/src/views/HomeView.vue index dc4e8da..f5f3b52 100644 --- a/src/views/HomeView.vue +++ b/src/views/HomeView.vue @@ -89,7 +89,7 @@ const formattedM = ref() const formattedH = ref() const formattedHH = ref() const formattedA = ref() -const clockInterval = ref() +const clockInterval = ref | undefined>() /** function อัพเดทเวลา*/ function updateClock() { @@ -118,6 +118,7 @@ const useLocation = ref('') //กรณีเลือกนอกสถ const fileImg = ref() //รูปถ่ายสถานที่ const remark = ref('') //ข้อความหมายเหตุที่ต้องการระบุเพิ่ม const checkInId = ref('') //Id ลงเวลา check-in ล่าสุดที่ยังไม่ลงเวลาออก +const MIN_CAPTURE_FILE_SIZE_BYTES = 5 * 1024 /** * funciton เรียกพิกัดละติจูด พิกัดลองติจูด @@ -219,7 +220,123 @@ const availableCameras = ref([]) const currentCameraIndex = ref(0) const currentCameraType = ref<'front' | 'back' | 'unknown'>('unknown') -const intervalId = ref(undefined) // ต้องใช้ตัวแปรเก็บค่า interval +/** + * Use the native still-photo picker only on real iOS/iPadOS devices. + * Desktop mobile emulation can spoof the iPhone user-agent, so rely on + * platform + touch capability instead of user-agent alone. + */ +const isIOSDevice: boolean = (() => { + const platform = navigator.platform || '' + const hasTouchPoints = navigator.maxTouchPoints > 1 + + return ( + /iPhone|iPad|iPod/.test(platform) || + (platform === 'MacIntel' && hasTouchPoints) + ) +})() + +const isAndroidDevice: boolean = /Android/i.test(navigator.userAgent) +const preferNativePhotoCapture = ref(false) +const useNativePhotoCapture = computed( + () => isIOSDevice || preferNativePhotoCapture.value +) +const centeredPreviewImageStyle = Object.freeze({ + objectFit: 'cover', + objectPosition: 'center center', +}) + +/** Ref for the hidden file input used as native camera fallback */ +const nativePhotoInput = ref(null) + +function clearSelectedPhoto() { + if (img.value) { + URL.revokeObjectURL(img.value) + img.value = undefined + } + fileImg.value = undefined +} + +async function canDecodeImageBlob(blob: Blob) { + if (!blob.type.startsWith('image/')) { + return false + } + + const imageUrl = URL.createObjectURL(blob) + + try { + const image = await new Promise((resolve, reject) => { + const element = new Image() + element.onload = () => { + if (element.naturalWidth > 0 && element.naturalHeight > 0) { + resolve(element) + } else { + reject(new Error('Image has invalid dimensions')) + } + } + element.onerror = () => reject(new Error('Image decode failed')) + element.src = imageUrl + }) + + return image.naturalWidth > 0 && image.naturalHeight > 0 + } catch (error) { + console.error('Error decoding captured image:', error) + return false + } finally { + URL.revokeObjectURL(imageUrl) + } +} + +async function isValidCapturedImage(blob: Blob) { + if (blob.size < MIN_CAPTURE_FILE_SIZE_BYTES) { + return false + } + + return canDecodeImageBlob(blob) +} + +async function assignSelectedPhoto(file: File) { + clearSelectedPhoto() + fileImg.value = file + img.value = URL.createObjectURL(file) +} + +function openNativePhotoCapture() { + if (cameraIsOn.value) { + camera.value?.stop() + cameraIsOn.value = false + } + + if (nativePhotoInput.value) { + nativePhotoInput.value.value = '' + nativePhotoInput.value.click() + } +} + +/** + * Called when the user selects / captures a photo via the native file-input + * fallback. Mirrors the same `img` + `fileImg` state that capturePhoto() + * sets so the upload flow is identical. + */ +async function onNativePhotoSelected(event: Event) { + const input = event.target as HTMLInputElement + const file = input.files?.[0] + input.value = '' + if (!file) return + + if (!(await isValidCapturedImage(file))) { + clearSelectedPhoto() + messageError( + $q, + '', + 'ไม่สามารถอ่านไฟล์รูปภาพได้ กรุณาถ่ายรูปใหม่อีกครั้ง' + ) + return + } + + await assignSelectedPhoto(file) +} + +const intervalId = ref | undefined>(undefined) /** * เริ่มจาก onMounted #1 เช็ค status คิว @@ -346,17 +463,34 @@ async function openCamera() { return } + // iOS Safari fallback: getUserMedia can open native video-recording UI and + // canvas.drawImage may produce a blank blob. Use the hidden file input + // with capture="user" instead — this opens the native camera in photo mode. + if (useNativePhotoCapture.value) { + openNativePhotoCapture() + return + } + if (!isPermissionCameraDenied.value) { try { - // change camera device if (cameraIsOn.value) { camera.value?.stop() } else { + // Keep the initial stream aligned with the Camera component's front-camera constraint. await camera.value?.start() const devices: any = await camera.value?.devices(['videoinput']) if (devices) { availableCameras.value = devices - await changeCamera() + + // Avoid an extra stop/start cycle here; on iOS it can override the initial camera selection. + const activeId = camera.value?.currentDeviceID() + const activeIdx = activeId + ? devices.findIndex((d: any) => d.deviceId === activeId) + : -1 + currentCameraIndex.value = activeIdx >= 0 ? activeIdx : 0 + currentCameraType.value = identifyCameraType( + devices[currentCameraIndex.value]?.label || '' + ) } } cameraIsOn.value = !cameraIsOn.value @@ -439,24 +573,30 @@ async function capturePhoto() { 'image/jpeg', 0.8 ) - if (!imageBlob) { + if (!imageBlob || !(await isValidCapturedImage(imageBlob))) { + if (isAndroidDevice) { + preferNativePhotoCapture.value = true + camera.value?.stop() + cameraIsOn.value = false + clearSelectedPhoto() + messageError( + $q, + '', + 'ไม่สามารถอ่านรูปจากกล้องของอุปกรณ์ได้ กรุณาถ่ายใหม่อีกครั้ง ระบบจะเปลี่ยนไปใช้โหมดถ่ายภาพของอุปกรณ์' + ) + return + } + messageError($q, '', 'ไม่สามารถถ่ายรูปได้ กรุณาลองใหม่อีกครั้ง') return } const fileName = `photo_${Date.now()}.jpg` //ไฟล์รูป const file = new File([imageBlob], fileName, { type: 'image/jpeg' }) - fileImg.value = file - - // ยกเลิก URL เก่าก่อนสร้างใหม่ (ป้องกัน memory leak) - if (img.value) { - URL.revokeObjectURL(img.value) - } + await assignSelectedPhoto(file) //แสดงรูป camera.value?.stop() - const url = URL.createObjectURL(imageBlob) - img.value = url } catch (error) { console.error('Error capturing photo:', error) messageError($q, error, 'ไม่สามารถถ่ายรูปได้ กรุณาลองใหม่อีกครั้ง') @@ -466,10 +606,11 @@ async function capturePhoto() { /** function เปลี่ยนรูปภาพ*/ async function refreshPhoto() { try { - // ยกเลิก URL เก่า (ป้องกัน memory leak) - if (img.value) { - URL.revokeObjectURL(img.value) - img.value = undefined + clearSelectedPhoto() + + if (useNativePhotoCapture.value) { + openNativePhotoCapture() + return } await camera.value?.start() } catch (error) { @@ -552,6 +693,22 @@ async function confirm() { return } + if (!fileImg.value || !(await isValidCapturedImage(fileImg.value))) { + if (isAndroidDevice) { + preferNativePhotoCapture.value = true + camera.value?.stop() + cameraIsOn.value = false + } + clearSelectedPhoto() + disabledBtn.value = false + messageError( + $q, + '', + 'รูปภาพไม่สมบูรณ์ กรุณาถ่ายรูปใหม่อีกครั้งก่อนลงเวลา' + ) + return + } + showLoader() const isLocation = workplace.value === 'in-place' //*true คือ ณ สถานที่ตั้ง, false คือ นอกสถานที่ตั้ง const locationName = workplace.value === 'in-place' ? '' : useLocation.value @@ -655,11 +812,7 @@ async function onClickConfirm() { try { showLoader() cameraIsOn.value = false - // ยกเลิก URL เก่า (ป้องกัน memory leak) - if (img.value) { - URL.revokeObjectURL(img.value) - img.value = undefined - } + clearSelectedPhoto() modalTime.value = false if (!statusCheckin.value) { statusCheckin.value = true @@ -697,11 +850,7 @@ const inQueue = ref(false) // ฟังก์ชันสำหรับรีเซ็ตรูปและหยุดกล้อง function resetCameraAndImage() { - // ยกเลิก URL เก่า (ป้องกัน memory leak) - if (img.value) { - URL.revokeObjectURL(img.value) - img.value = undefined - } + clearSelectedPhoto() if (cameraIsOn.value && camera.value) { camera.value.stop() cameraIsOn.value = false @@ -735,7 +884,12 @@ const isPermissionCameraDenied = ref(false) // ตัวแปรสำ async function requestCamera() { try { - await navigator.mediaDevices.getUserMedia({ video: true }) + // Probe camera permission with the same front-camera constraint, then release the stream immediately. + const stream = await navigator.mediaDevices.getUserMedia({ + video: { facingMode: 'user' }, + audio: false, + }) + stream.getTracks().forEach((track) => track.stop()) } catch (err) { isPermissionCameraDenied.value = true } @@ -760,7 +914,8 @@ onMounted(async () => { // เรียกแผนที่เฉพาะเมื่อยอมรับ privacy แล้ว if (privacyStore.isAccepted) { mapRef.value?.requestLocationPermission() - requestCamera() + // iOS uses the file-input fallback and does not need a getUserMedia probe + if (!useNativePhotoCapture.value) requestCamera() } }) @@ -802,7 +957,8 @@ watch( (newVal) => { if (newVal) { mapRef.value?.requestLocationPermission() - requestCamera() + // iOS uses the file-input fallback and does not need a getUserMedia probe + if (!useNativePhotoCapture.value) requestCamera() } } ) @@ -929,7 +1085,26 @@ watch(
- + + +
+ +
@@ -1043,7 +1218,27 @@ watch(
- + + +
+ +
@@ -1497,6 +1692,21 @@ watch( + + +