RHarris
RHarris

Reputation: 11187

Using mediaDevices.getUserMedia to access front facing camera on a laptop

I'm developing an internal web application for our organization. The app will be used primarily (if not exclusively) from desktop/laptop machines.

I need to give the user the ability to snap an image of themselves. I've followed the example posted by David Walsh here.

This works fine if the user has a single camera on their computer. However, on machines that have both a front and rear camera (like a Surface Pro 4), this code defaults to the rear facing camera. I tried tweaking the code like so:

navigator.mediaDevices.getUserMedia({video: {facingMode: "user"}, audio: false}).then(...)

However, this made no difference. I'm guessing that was primarily intended for mobile devices.

Is there any way to force the camera to the front if its available or, better yet, enable the user to switch cameras on a laptop device with both a front and rear camera?

Upvotes: 2

Views: 2803

Answers (1)

jib
jib

Reputation: 42450

The facingMode constraint is the right answer according to the spec, and works in Firefox. Chrome just hasn't implemented it yet. However, you can use the adapter.js polyfill to get it.

Try it out here.

Upvotes: 3

Related Questions