Skip to content

Commit

Permalink
Find all camera devices before render
Browse files Browse the repository at this point in the history
  • Loading branch information
gkatrakazas committed Mar 19, 2024
1 parent 7920416 commit 8271ac3
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions src/components/QRCodeScanner/QRCodeScanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const QRScanner = ({ onClose }) => {
const [qrDetected, setQrDetected] = useState(false);
const [boxSize, setBoxSize] = useState(null);
const [zoomLevel, setZoomLevel] = useState(1);
const [hasCameraPermission, setHasCameraPermission] = useState(false);
const { t } = useTranslation();

const handleZoomChange = (event) => {
Expand All @@ -40,25 +41,40 @@ const QRScanner = ({ onClose }) => {
};

useEffect(() => {
navigator.mediaDevices.enumerateDevices()
.then(mediaDevices => {
const videoDevices = mediaDevices.filter(({ kind }) => kind === "videoinput");
setDevices(videoDevices);

// Find and prioritize the back camera if it exists
const backCameraIndex = videoDevices.findIndex(device => device.label.toLowerCase().includes('back'));
if (backCameraIndex !== -1) {
setCurrentDeviceIndex(backCameraIndex);
}

setCameraReady(true);
navigator.mediaDevices.getUserMedia({ video: true })
.then(stream => {
setHasCameraPermission(true);
stream.getTracks().forEach(track => track.stop()); // Stop using the camera
})
.catch(error => {
console.error("Error accessing camera:", error);
setCameraReady(false);
console.error("Camera access denied:", error);
});
}, []);


useEffect(() => {
if (hasCameraPermission) {
// Now enumerate devices after permission has been granted
navigator.mediaDevices.enumerateDevices()
.then(mediaDevices => {
const videoDevices = mediaDevices.filter(({ kind }) => kind === "videoinput");
setDevices(videoDevices);

const backCameraIndex = videoDevices.findIndex(device => device.label.toLowerCase().includes('back'));
if (backCameraIndex !== -1) {
setCurrentDeviceIndex(backCameraIndex);
} else {
setCurrentDeviceIndex(0); // Default to the first camera if no back camera is found
}

setCameraReady(true);
})
.catch(error => {
console.error("Error enumerating devices:", error);
});
}
}, [hasCameraPermission]);

const switchCamera = () => {
if (devices.length > 1) {
const newIndex = (currentDeviceIndex + 1) % devices.length;
Expand Down

0 comments on commit 8271ac3

Please sign in to comment.