From d4ae2f56a0178ea196eee0bd5aff88a14840a5f6 Mon Sep 17 00:00:00 2001 From: "DESKTOP-1R2VSQH\\Lenovo ThinkPad E490" Date: Fri, 17 Apr 2026 11:00:18 +0700 Subject: [PATCH 1/3] feat: camera switch button --- src/views/HomeView.vue | 93 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 87 insertions(+), 6 deletions(-) diff --git a/src/views/HomeView.vue b/src/views/HomeView.vue index bfc8e91..055346d 100644 --- a/src/views/HomeView.vue +++ b/src/views/HomeView.vue @@ -187,6 +187,9 @@ const cameraIsOn = ref(false) const img = ref(undefined) const photoWidth = ref(350) const photoHeight = ref(350) +const availableCameras = ref([]) +const currentCameraIndex = ref(0) +const currentCameraType = ref<'front' | 'back' | 'unknown'>('unknown') const intervalId = ref(undefined) // ต้องใช้ตัวแปรเก็บค่า interval @@ -302,6 +305,16 @@ async function stopChecking() { } } +function identifyCameraType(label: string): 'front' | 'back' | 'unknown' { + const lowerLabel = label.toLowerCase() + if (lowerLabel.includes('front') || lowerLabel.includes('user') || lowerLabel.includes('face')) { + return 'front' + } else if (lowerLabel.includes('back') || lowerLabel.includes('environment') || lowerLabel.includes('rear')) { + return 'back' + } + return 'unknown' +} + /** function เปิดกล้อง*/ async function openCamera() { // เช็คสิทธิ์ privacy ก่อนเปิดกล้อง @@ -315,7 +328,11 @@ async function openCamera() { await camera.value?.stop() } else { await camera.value?.start() - await changeCamera() // ต้องรอให้ start() เสร็จก่อน + const devices: any = await camera.value?.devices(['videoinput']) + if (devices) { + availableCameras.value = devices + await changeCamera() + } } cameraIsOn.value = !cameraIsOn.value } else { @@ -329,10 +346,54 @@ async function openCamera() { } /** change camera device*/ -async function changeCamera() { - const devices: any = await camera.value?.devices(['videoinput']) - const device = await devices[0] - camera.value?.changeCamera(device.deviceId) +async function changeCamera(targetCameraType?: 'front' | 'back') { + try { + const devices: any = await camera.value?.devices(['videoinput']) + + if (!devices || devices.length === 0) { + console.warn('No cameras found') + return + } + + availableCameras.value = devices + + if (devices.length === 1 || !targetCameraType) { + const device = devices[0] + await camera.value?.changeCamera(device.deviceId) + currentCameraIndex.value = 0 + currentCameraType.value = identifyCameraType(device.label || '') + return + } + + const matchingCameras = devices.filter((device: any) => + identifyCameraType(device.label || '') === targetCameraType + ) + + if (matchingCameras.length > 0) { + const targetDevice = matchingCameras[0] + await camera.value?.changeCamera(targetDevice.deviceId) + currentCameraIndex.value = devices.indexOf(targetDevice) + currentCameraType.value = targetCameraType + } else { + const nextIndex = (currentCameraIndex.value + 1) % devices.length + const nextDevice = devices[nextIndex] + await camera.value?.changeCamera(nextDevice.deviceId) + currentCameraIndex.value = nextIndex + currentCameraType.value = identifyCameraType(nextDevice.label || '') + } + } catch (error) { + console.error('Error switching camera:', error) + } +} + +/** switch camera device*/ +async function switchCamera() { + if (availableCameras.value.length <= 1) { + return + } + + const targetType: 'front' | 'back' = currentCameraType.value === 'front' ? 'back' : 'front' + await changeCamera(targetType) } /** function ถ่ายรูป*/ @@ -759,6 +820,16 @@ watch( v-if="$q.screen.gt.xs" class="absolute-bottom-right q-ma-md" > + + + \ No newline at end of file From e1962d79bb9eae5dbfba667f3e228f0060b94c04 Mon Sep 17 00:00:00 2001 From: "DESKTOP-1R2VSQH\\Lenovo ThinkPad E490" Date: Fri, 17 Apr 2026 15:19:31 +0700 Subject: [PATCH 2/3] fix: switchCamera --- src/views/HomeView.vue | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/views/HomeView.vue b/src/views/HomeView.vue index 055346d..12788ed 100644 --- a/src/views/HomeView.vue +++ b/src/views/HomeView.vue @@ -392,8 +392,26 @@ async function switchCamera() { return } - const targetType: 'front' | 'back' = currentCameraType.value === 'front' ? 'back' : 'front' - await changeCamera(targetType) + const frontCameras = availableCameras.value.filter((device: any) => + identifyCameraType(device.label || '') === 'front' + ) + const backCameras = availableCameras.value.filter((device: any) => + identifyCameraType(device.label || '') === 'back' + ) + + let targetDevice + if (currentCameraType.value === 'front' && backCameras.length > 0) { + targetDevice = backCameras[0] + } else if (frontCameras.length > 0) { + targetDevice = frontCameras[0] + } else { + const nextIndex = (currentCameraIndex.value + 1) % availableCameras.value.length + targetDevice = availableCameras.value[nextIndex] + } + + await camera.value?.changeCamera(targetDevice.deviceId) + currentCameraIndex.value = availableCameras.value.indexOf(targetDevice) + currentCameraType.value = identifyCameraType(targetDevice.label || '') } /** function ถ่ายรูป*/ From 202318c16914d7d6b4dcf11bec977d5cdb4f4c34 Mon Sep 17 00:00:00 2001 From: waruneeauy Date: Fri, 17 Apr 2026 16:26:39 +0700 Subject: [PATCH 3/3] fix switch camara --- src/views/HomeView.vue | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/src/views/HomeView.vue b/src/views/HomeView.vue index 12788ed..5df3c0c 100644 --- a/src/views/HomeView.vue +++ b/src/views/HomeView.vue @@ -392,25 +392,12 @@ async function switchCamera() { return } - const frontCameras = availableCameras.value.filter((device: any) => - identifyCameraType(device.label || '') === 'front' - ) - const backCameras = availableCameras.value.filter((device: any) => - identifyCameraType(device.label || '') === 'back' - ) - - let targetDevice - if (currentCameraType.value === 'front' && backCameras.length > 0) { - targetDevice = backCameras[0] - } else if (frontCameras.length > 0) { - targetDevice = frontCameras[0] - } else { - const nextIndex = (currentCameraIndex.value + 1) % availableCameras.value.length - targetDevice = availableCameras.value[nextIndex] - } + // สลับแค่ระหว่างกล้อง 2 ตัวแรก (กล้องหน้าและหลังหลัก) + const targetIndex = currentCameraIndex.value === 0 ? 1 : 0 + const targetDevice = availableCameras.value[targetIndex] await camera.value?.changeCamera(targetDevice.deviceId) - currentCameraIndex.value = availableCameras.value.indexOf(targetDevice) + currentCameraIndex.value = targetIndex currentCameraType.value = identifyCameraType(targetDevice.label || '') }