如何解决 TypeError: undefined is not an object (evaluating 'navigator.mediaDevices.getUserMedia')

时间:2021-07-19 11:55:56

标签: javascript html

这是代码

const qr = window.qrcode;

const video = document.createElement("video");
const canvasElement = document.getElementById("qr-canvas");
const canvas = canvasElement.getContext("2d");



let scanning = false;

qr.callback = res => {
  if (res) {
    location.replace(res);
    scanning = false;
    video.srcObject.getTracks().forEach(track => {
      track.stop();
    });
    canvasElement.hidden = true;
    btnScanQR.hidden = false;
  }
};


  navigator.mediaDevices
    .getUserMedia({ video: { facingMode: "environment" } })
    .then(function(stream) {
      scanning = true;
      canvasElement.hidden = false;
      video.setAttribute("playsinline", true); // required to tell iOS safari we don't want fullscreen
      video.srcObject = stream;
      video.play();
      tick();
      scan();
    });


function tick() {
  canvasElement.height = video.videoHeight;
  canvasElement.width = video.videoWidth;
  canvas.drawImage(video, 0, 0, canvasElement.width, canvasElement.height);

  scanning && requestAnimationFrame(tick);
}

function scan() {
  try {
    qr.decode();
  } catch (e) {
    setTimeout(scan, 300);
  }
}

我用 xampp 尝试了这个,我没有问题,但是用 Altvista 托管它给了我这个错误,这在 safari、chrome、windows 和 macOS 中都会发生。 使用此代码,我正在尝试为 QR 码制作扫描仪,它应该向我显示相机设备的流,但它什么也不做

0 个答案:

没有答案