From 46198913b1488642313e5783ed70ceebd3c146de Mon Sep 17 00:00:00 2001 From: 0ry5 Date: Fri, 13 Sep 2024 22:24:04 +0200 Subject: [PATCH] fix(camera): always try cam facing environment --- frontend/src/shared/utils/useScanner.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/frontend/src/shared/utils/useScanner.ts b/frontend/src/shared/utils/useScanner.ts index b76e277..70666b2 100644 --- a/frontend/src/shared/utils/useScanner.ts +++ b/frontend/src/shared/utils/useScanner.ts @@ -103,15 +103,25 @@ export const useScanner = ({ }; const getCameraId = useCallback(async () => { - await navigator.mediaDevices.getUserMedia({ - video: true, + const stream = await navigator.mediaDevices.getUserMedia({ + video: { facingMode: "environment" }, }); + if ( + stream && + stream.getVideoTracks().length && + stream.getVideoTracks()[0].getSettings().deviceId + ) { + return stream.getVideoTracks()[0].getSettings().deviceId; + } + const devices = await navigator.mediaDevices .enumerateDevices() .then((ds) => ds.filter((d) => d.kind === "videoinput")); - const back = devices.filter((d) => d.label === "Back Camera")[0]; + const back = devices.filter((d) => + d.label.toLowerCase().includes("back") + )[0]; return !back ? devices[0].deviceId : back.deviceId; }, []);