minimalistic_scanner / select_back_camera.js
Glainez's picture
Update select_back_camera.js
6040ae6 verified
raw
history blame
686 Bytes
async function selectLastCamera() {
const devices = await navigator.mediaDevices.enumerateDevices();
const videoDevices = devices.filter(device => device.kind === 'videoinput');
if(videoDevices.length > 0) {
const lastDeviceId = videoDevices[videoDevices.length - 1].deviceId;
const constraints = {
video: { deviceId: { exact: lastDeviceId } }
};
try {
const stream = await navigator.mediaDevices.getUserMedia(constraints);
// Use the stream for your video element
video_source.srcObject = stream;
video_source.play();
} catch(error) {
console.error('Error accessing the camera', error);
}
}
}
selectLastCamera();