From 294ced6ed91b0aac1c524adb08bc22cef05d5a92 Mon Sep 17 00:00:00 2001 From: Rizumu Ayaka Date: Tue, 1 Oct 2024 21:51:37 +0800 Subject: [PATCH] feat: devices --- app/components/Scan.vue | 61 ++++++++++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 13 deletions(-) diff --git a/app/components/Scan.vue b/app/components/Scan.vue index 6e55359..3929f72 100644 --- a/app/components/Scan.vue +++ b/app/components/Scan.vue @@ -11,22 +11,44 @@ const props = withDefaults(defineProps<{ height: 512, }) -const results = defineModel>('results', { default: new Set() }) +const { devices } = useDevicesList({ + requestPermissions: true, + constraints: { + audio: false, + video: true, + }, +}) -const shutterCount = ref(0) +const cameras = computed(() => devices.value.filter(i => i.kind === 'videoinput')) +const selectedCamera = ref(cameras.value[0]?.deviceId) -const stream = await navigator.mediaDevices.getUserMedia({ - audio: false, - video: { - width: props.width, - height: props.height, - }, +watchEffect(() => { + if (!selectedCamera.value) + selectedCamera.value = cameras.value[0]?.deviceId }) +const results = defineModel>('results', { default: new Set() }) + +const shutterCount = ref(0) + +const error = ref('') const video = shallowRef() -onMounted(() => { - video.value!.srcObject = stream - video.value!.play() +onMounted(async () => { + try { + const stream = await navigator.mediaDevices.getUserMedia({ + audio: false, + video: { + width: props.width, + height: props.height, + deviceId: selectedCamera.value, + }, + }) + video.value!.srcObject = stream + video.value!.play() + } + catch (e) { + error.value = e + } useIntervalFn( () => scanFrame(), () => props.speed, @@ -49,6 +71,19 @@ async function scanFrame() {