I am using navigate.getUserMedia() method to capture video on my mobile and do further processing on it. But as of now it is capturing the video using the front camera. How do i make it to access the rear facing camera??
Below is some sample code which i am using in my application:
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia; if (navigator.getUserMedia){ navigator.getUserMedia({video: true}, successCallback, errorCallback);
Thanks in advance
FYI There is now a single function you can use: navigator. mediaDevices. getUserMedia() as per the specs: The official definition for the getUserMedia() method, and the one which developers are encouraged to use, is now at MediaDevices.
getUserMedia() Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers. The MediaDevices . getUserMedia() method prompts the user for permission to use a media input which produces a MediaStream with tracks containing the requested types of media.
To get the rear camera, you can use the MediaConstraint:video:facingMode property. Available values are 'user' (front camera), and 'environment' (back camera).
This example on simpl.info demonstrates the use of MediaStreamTrack.getSources
to select from multiple video sources.
https://simpl.info/getusermedia/sources/
I can confirm that this works in Chrome 32.
You can use facingMode
to choose "user" or "environment" for front or back camera respectively. Not sure about browser support but it works on Android Chrome 58.
Use
navigator.getUserMedia({video: { facingMode: { exact: "environment" } } }, successCallback, errorCallback);
or, to allow fallback to some other camera
navigator.getUserMedia({video: { facingMode: "environment" } }, successCallback, errorCallback);
instead of
navigator.getUserMedia({video: true}, successCallback, errorCallback);
From https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With