'Instascan select camara to scan qr

My website reads QR CODES using the mobile phone. I have used that library

https://github.com/schmich/instascan

<div class="select">
    <label for="videoSource">Video source: </label><select id="videoSource"></select>
</div>

<video autoplay muted playsinline></video>

<script async src="js/main.js"></script>

Because the mobile phone has many camaras I want to let the user to select which camara wants for scanning the qr code.

main.js:

https://simpl.info/getusermedia/sources/ https://github.com/samdutton/simpl/blob/gh-pages/getusermedia/sources/js/main.js

'use strict';

var videoElement = document.querySelector('video');
var videoSelect = document.querySelector('select#videoSource');

videoSelect.onchange = getStream;

getStream().then(getDevices).then(gotDevices);

function getDevices() {
  // AFAICT in Safari this only gets default devices until gUM is called :/
  return navigator.mediaDevices.enumerateDevices();
}

function gotDevices(deviceInfos) {
  window.deviceInfos = deviceInfos; // make available to console
  console.log('Available input and output devices:', deviceInfos);
  for (const deviceInfo of deviceInfos) {
    const option = document.createElement('option');
    option.value = deviceInfo.deviceId;
    if (deviceInfo.kind === 'videoinput') {
      option.text = deviceInfo.label || `Camera ${videoSelect.length + 1}`;
      videoSelect.appendChild(option);
    }
  }
}

function getStream() {
  if (window.stream) {
    window.stream.getTracks().forEach(track => {
      track.stop();
    });
  }

  const videoSource = videoSelect.value;
  const constraints = {
    video: {deviceId: videoSource ? {exact: videoSource} : undefined}
  };
  return navigator.mediaDevices.getUserMedia(constraints).
    then(gotStream).catch(handleError);
}

function gotStream(stream) {
  window.stream = stream; // make stream available to console
  videoSelect.selectedIndex = [...videoSelect.options].
    findIndex(option => option.text === stream.getVideoTracks()[0].label);
  videoElement.srcObject = stream;
}

function handleError(error) {
  console.error('Error: ', error);
}

Then I want to start the scanner to the camara the user has selected because I want to obtaint the QR-CODE:

 <script type="text/javascript">
          let scanner = new Instascan.Scanner({ video: document.getElementById('preview'),mirror: false });
          scanner.addListener('scan', function (content) {
                alert("qr result " + content);


          });
          Instascan.Camera.getCameras().then(function (cameras) {
                if (cameras.length > 0) {

                  scanner.start(cameras[0]); //I want to scanner.start the camara that the user has selected.
                } else {
                  console.error('No cameras found.');
                }
          }).catch(function (e) {
                console.error(e);
          });
    </script> 


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source