0

I want to make QR reader feature in my website. I got several javascript plugin that can do this. I already run the script on localhost in my computer, work very well. Then I try to run the script in my smartphone by access my laptop IP address (how I do it. The page is load well in smartphone but can't detect camera. here QR scanner plugins that I have try :

daniel
  • 31
  • 1
  • 1
  • 6
  • this is security settings problem. solution is here : [stackoverflow](https://stackoverflow.com/questions/52759992/how-to-access-camera-and-microphone-in-chrome-without-https?fbclid=IwAR1A2iexQJbzH6yfBZnKvEAqx5hDnr5uvwETlsvsCZ0pF3HCyqAoP5VfHDk) – daniel Nov 18 '20 at 03:22

1 Answers1

0

I’ve been coding a barcode scanner javascript file for over a year now so I’ve had this issue arise. Have you tried adding something like this in your JS? This will detect your media devices. I attached a generic get user media script that is extremely similar to what i use. You may have to change some of the script verbiage dependent on what you named your elements but this should give you what you’re looking for.

// Older browsers might not implement mediaDevices at all, so we set an empty object first
if (navigator.mediaDevices === undefined) {
  navigator.mediaDevices = {};
}

// Some browsers partially implement mediaDevices. We can't just assign an object
// with getUserMedia as it would overwrite existing properties.
// Here, we will just add the getUserMedia property if it's missing.
if (navigator.mediaDevices.getUserMedia === undefined) {
  navigator.mediaDevices.getUserMedia = function(constraints) {

    // First get ahold of the legacy getUserMedia, if present
    var getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia;

    // Some browsers just don't implement it - return a rejected promise with an error
    // to keep a consistent interface
    if (!getUserMedia) {
      return Promise.reject(new Error('getUserMedia is not implemented in this browser'));
    }

    // Otherwise, wrap the call to the old navigator.getUserMedia with a Promise
    return new Promise(function(resolve, reject) {
      getUserMedia.call(navigator, constraints, resolve, reject);
    });
  }
}

navigator.mediaDevices.getUserMedia({ audio: true, video: true })
.then(function(stream) {
  var video = document.querySelector('video');
  // Older browsers may not have srcObject
  if ("srcObject" in video) {
    video.srcObject = stream;
  } else {
    // Avoid using this in new browsers, as it is going away.
    video.src = window.URL.createObjectURL(stream);
  }
  video.onloadedmetadata = function(e) {
    video.play();
  };
})
.catch(function(err) {
  console.log(err.name + ": " + err.message);
});