4

I'm currently working on my little project to Web Scan QR Code. When I use it on my Desktop, the webcam works fine and it can scan the QR Code but I have a problem when I use it on my smartphones. Both Rear & Front camera won't work on smartphones.

My code:

<script>

  let scanner = new Instascan.Scanner(
  {
    video: document.getElementById('preview')
  }
);

scanner.addListener('scan', function(content) {
  alert('The Content is : ' + content);
  window.open(content, "_blank");
});



 Instascan.Camera.getCameras().then(cameras => {
      if (cameras.length > 0) {
        scanner.start(cameras[0]);
      } else {
        console.error("There is no camera available");
      }
    });


</script>

So, I even tried to change the index to 1 but it still doesn't work:

if (cameras.length > 0) {
        scanner.start(cameras[1]);

How do I enable the camera on the smartphones? Your help is very appreciated.

Timisorean
  • 1,388
  • 7
  • 20
  • 30

1 Answers1

-1
       if (cameras.length > 1) {
            scanner.start(cameras[1]);

        }
        else if (cameras.length > 0) {
            scanner.start(cameras[0]);
        } else {
            console.error('No cameras found.');
        }
  • 3
    Welcome to Stack Overflow. Code without any explanation are rarely helpful. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please edit your question and explain how it answers the specific question being asked. See [How to Answer](https://stackoverflow.com/help/how-to-answer). – Sfili_81 Jun 17 '21 at 12:23