Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select the camera while using navigate.getUserMedia()

Tags:

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

like image 600
Vinay C Avatar asked May 29 '13 06:05

Vinay C


People also ask

Can I use Navigator MediaDevices getUserMedia?

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.

How do I use MediaDevices getUserMedia?

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.

How do you access the back camera in HTML?

To get the rear camera, you can use the MediaConstraint:video:facingMode property. Available values are 'user' (front camera), and 'environment' (back camera).


2 Answers

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.

like image 174
freakTheMighty Avatar answered Sep 18 '22 07:09

freakTheMighty


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

like image 37
user1318499 Avatar answered Sep 18 '22 07:09

user1318499