Unable to take photo from webcam using HTML5 and getUserMedia() in Google Chrome on first page load

主宰稳场 提交于 2019-12-05 10:35:32

LIVE DEMO

Works well in Chrome and FF.

(function() {

  var streaming = false,
      video        = document.querySelector('#video'),
      canvas       = document.querySelector('#canvas'),
      photo        = document.querySelector('#photo'),
      startbutton  = document.querySelector('#startbutton'),
      width = 320,
      height = 0;

  navigator.getMedia = ( navigator.getUserMedia ||
                         navigator.webkitGetUserMedia ||
                         navigator.mozGetUserMedia ||
                         navigator.msGetUserMedia);

  navigator.getMedia(
    {
      video: true,
      audio: false
    },
    function(stream) {
      if (navigator.mozGetUserMedia) {
        video.mozSrcObject = stream;
      } else {
        var vendorURL = window.URL || window.webkitURL;
        video.src = vendorURL.createObjectURL(stream);
      }
      video.play();
    },
    function(err) {
      console.log("An error occured! " + err);
    }
  );

This code I found here: LINK DEVELOPER MOZILLA

UPDATE: I updated my Live Demo to JSFiddle because getUserMedia() is no longer works on insecure origins. To use this feature, you should consider switching your application to a secure origin, such as HTTPS. See The Chromium Projects for more details.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!