From b918a0c53f8dca7c38668e0a27a873a37e7295e7 Mon Sep 17 00:00:00 2001 From: ssurui Date: Thu, 3 Sep 2026 16:06:28 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=8B=8D=E7=85=A7=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E6=91=84=E5=83=8F=E5=A4=B4=E6=8C=89=E6=A0=87=E7=AD=BE=E7=B2=BE?= =?UTF-8?q?=E7=A1=AE=E5=8C=B9=E9=85=8D=E5=90=8E=E7=BD=AE=EF=BC=8C=E4=B8=8D?= =?UTF-8?q?=E5=86=8D=E4=BE=9D=E8=B5=96=E6=9E=9A=E4=B8=BE=E9=A1=BA=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 之前默认摄像头是"设备列表第一个",依赖操作系统 enumerateDevices() 返回顺序,不可靠。真机测出后置摄像头(朝外拍摄那颗)的 label 是 "Hy-8M(8079)-Camera",改成按标签里的型号数字串精确匹配。 摄像头 label 在 getUserMedia 拿到权限前是空的(浏览器隐私限制), 没法一开始就按标签选,所以流程改成:先用默认设备开流拿到权限, 权限到手后 label 才是真的,这时候如果不是用户/配置显式指定的摄像头, 再检查当前是不是已经是后置、不是就切过去。 真机验证:清空之前手动切换保留下来的摄像头偏好后重新打开,稳定落在 后置摄像头(桌面/Mac mini 视角),不再随机。 Co-Authored-By: Claude Sonnet 5 --- src/renderer/src/components/CameraCapture.vue | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/renderer/src/components/CameraCapture.vue b/src/renderer/src/components/CameraCapture.vue index c173567..3165284 100644 --- a/src/renderer/src/components/CameraCapture.vue +++ b/src/renderer/src/components/CameraCapture.vue @@ -18,6 +18,10 @@ const capturedDataUrl = ref('') const starting = ref(false) const errorMessage = ref('') +// 真机实测确认的后置摄像头(朝外拍摄那颗)标签是 "Hy-8M(8079)-Camera", +// 用型号里的数字串匹配,不依赖完整标签文案(避免语言环境/固件版本差异导致匹配不上) +const REAR_CAMERA_LABEL_HINT = '8079' + let stream: MediaStream | null = null function stopStream(): void { @@ -58,6 +62,10 @@ async function refreshDevices(): Promise { } } +function findRearCamera(): MediaDeviceInfo | undefined { + return devices.value.find((d) => d.label.includes(REAR_CAMERA_LABEL_HINT)) +} + async function switchDevice(deviceId: string): Promise { selectedDeviceId.value = deviceId emit('deviceChange', deviceId) @@ -109,8 +117,16 @@ watch( if (val) { capturedDataUrl.value = '' selectedDeviceId.value = props.initialDeviceId - await refreshDevices() + // 摄像头 label 在拿到权限前是空的(浏览器隐私限制),没法在这一步就按标签 + // 找后置摄像头,先随便开一个拿到权限;startStream 成功后 devices 里才有真实 label。 await startStream(selectedDeviceId.value) + // 用户/配置没有指定具体摄像头时,权限到手后尝试切到确认过的后置摄像头 + if (!props.initialDeviceId) { + const rear = findRearCamera() + if (rear && rear.deviceId !== selectedDeviceId.value) { + await switchDevice(rear.deviceId) + } + } } else { stopStream() }