8

we want to use the back camera for scanning, and select the correct back camera - the main camera, and not the wide lens camera.

newer mobile devices (for example, Samsung galaxy s10) have several front and back cameras. when calling enumerateDevices() we get a list of cameras for the front and the back. we want to select the main camera from the list of back cameras. we can use constraints to select the back camera (facing: "environment"), but we don't know how to tell for each device if the camera is the main camera or a wide lens camera.

navigator.mediaDevices.enumerateDevices().then(function (devices) {
        for(var i = 0; i < devices.length; i ++){
            var device = devices[i];
            if (device.kind === 'videoinput') {
                //any useful device info here?
            }
        }
    });
scanner
  • 293
  • 3
  • 16
  • I'm facing the same exact issue. I'm wondering if you solved this somehow. If you did i would be great full if you share your solution. – mart cube May 24 '20 at 11:50

2 Answers2

0

device objects ordinarily have device.label text describing them. But, this text is device specific.

if you load https://webrtc.github.io/samples/src/content/devices/input-output/ into a browser it will give you a pulldown list of available media sources (cameras, microphones). The labels identify each camera. They vary somewhat from device to device.

On iPhones, it's "Front Camera" and "Back Camera".

Here's a qr code for the page. enter image description here

O. Jones
  • 103,626
  • 17
  • 118
  • 172
-1

It depends on what you mean by "main" back camera. Our use case required a back camera that supported auto-focus (not all do). Check out the constraints you can query for here and make the right call to navigator.mediaDevices.getUserMedia().

For instance:

cam = await navigator.mediaDevices.getUserMedia({video: {facingMode: {exact: "environment"}, focusMode: {ideal: "continuous"}}}});

You mention telephoto, so maybe you're after zoom levels. That's an option, too, but I haven't played with that much myself yet.

Support for the constraint API varies per device and is very finicky. I'd recommend building up fallback options, targeting specific os version / devices with custom queries, and doing tons of experimentation to ensure you get a camera that will work for you as often as possible.

Ken L
  • 1