It keeps using the front facing camera instead of the back camera This is my code: I added the facingMode: {exact:”environment”}, but it doesn’t work
JavaScript
x
32
32
1
const constraints = {
2
video: true,
3
facingMode: { exact: 'environment' }
4
};
5
6
if ('mediaDevices' in navigator && 'getUserMedia' in navigator.mediaDevices) {
7
console.log("Let's get this party started")
8
}
9
navigator.mediaDevices.getUserMedia(constraints).
10
then((stream) => {video.srcObject = stream});
11
function displayImage()
12
{
13
const selectedFile = document.getElementById('fileinput')
14
//var image =document.getElementById('output')
15
//image.src = URL.createObjectURL(selectedFile.files[0]);
16
//selectedFile.files[0]
17
const img = new Image()
18
img.src = URL.createObjectURL(selectedFile.files[0])
19
canvas.width = video.videoWidth
20
canvas.height = video.videoHeight
21
video.style.display="none"
22
canvas.style.display ="inline"
23
console.log(img)
24
console.log("image uploaded")
25
26
img.onload = function() {
27
canvas.getContext('2d').drawImage(img, 0, 0,video.videoWidth,video.videoHeight);
28
console.log('the image is drawn');
29
}
30
31
}
32
Advertisement
Answer
Your constraints are not set correctly.
facingMode
is a member of the video
constraint, so it should be
JavaScript
1
8
1
const constraints = {
2
video: {
3
facingMode: {
4
exact: "environment"
5
}
6
}
7
};
8
Live Fiddle to be ran from a device with a back camera.