html - Accessing device camera using javascript - Stack Overflow

I want to access my device camera using javascript . But this code only works in firefox and that too o

I want to access my device camera using javascript . But this code only works in firefox and that too on desktop . I want to access my camera on other browsers as well as mobile devices.

function start()
 {
  var video = document.getElementById('video'),
  vendorUrl = window.URL;
  navigator.mediaDevices.getUserMedia({
        video: true,
        audio: true
    })
.then(function(stream) {
  video.src = vendorUrl.createObjectURL(stream);
          video.play();
})
.catch(function(err) {
alert("no");
});

};

I want to access my device camera using javascript . But this code only works in firefox and that too on desktop . I want to access my camera on other browsers as well as mobile devices.

function start()
 {
  var video = document.getElementById('video'),
  vendorUrl = window.URL;
  navigator.mediaDevices.getUserMedia({
        video: true,
        audio: true
    })
.then(function(stream) {
  video.src = vendorUrl.createObjectURL(stream);
          video.play();
})
.catch(function(err) {
alert("no");
});

};
Share Improve this question asked Mar 25, 2018 at 7:59 Yeshwant BhattYeshwant Bhatt 311 silver badge4 bronze badges 1
  • Possible duplicate of how to make getUserMedia() work on all browsers – Sebastian Simon Commented Mar 25, 2018 at 8:06
Add a ment  | 

2 Answers 2

Reset to default 3

  (function() {
    'use strict';
    var video = document.querySelector('video')
      , canvas;

    
    function takeSnapshot() {
      var img = document.querySelector('img') || document.createElement('img');
      var context;
      var width = video.offsetWidth
        , height = video.offsetHeight;

      canvas = canvas || document.createElement('canvas');
      canvas.width = width;
      canvas.height = height;

      context = canvas.getContext('2d');
      context.drawImage(video, 0, 0, width, height);

      img.src = canvas.toDataURL('image/png');
      document.body.appendChild(img);
    }

    
    if (navigator.mediaDevices) {
      // access the web cam
      navigator.mediaDevices.getUserMedia({video: true})
     
        .then(function(stream) {
         
		  video.srcObject = stream
          video.addEventListener('click', takeSnapshot);
        })
      
        .catch(function(error) {
          document.body.textContent = 'Could not access the camera. Error: ' + error.name;
        });
    }
  })();
video, img {
      max-width:100%;
    }
<video autoplay></video>

Copy it to your local and run

Hope this helps you

Mobile browsers need a valid click event to start playing video, so video.play() will not work on mobile until it receives this. Just add a play icon over the video (only on mobile) to induce the user to click, and bind video.play() to this click event.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745085367a4610374.html

相关推荐

  • html - Accessing device camera using javascript - Stack Overflow

    I want to access my device camera using javascript . But this code only works in firefox and that too o

    22小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信