如何允许自动访问摄像头

How to allow the access automatically to the webcam

本文关键字:访问 摄像头 何允许      更新时间:2023-09-26

我正在尝试开发一个使用以下JS代码访问我的网络摄像头的网页:

(function( $ ){
$.fn.html5_qrcode = function(qrcodeSuccess, qrcodeError, videoError) {
'use strict';
var height = this.height();
var width = this.width();
if (height == null) {
  height = 250;
}
if (width == null) {
  width = 300;
}
var vidTag = '<video id="html5_qrcode_video" width="' + width + 'px" height="' +   height   + 'px"></video>' 
var canvasTag = '<canvas id="qr-canvas" width="' + (width - 2) + 'px" height="' + (height - 2) + 'px" style="display:none;"></canvas>' 
this.append(vidTag);
this.append(canvasTag);

var video = $('#html5_qrcode_video').get(0);
var canvas;
var context; 
var localMediaStream;
$('#qr-canvas').each(function(index, element) {
  canvas = element;
  context = element.getContext('2d');   
});

var scan = function() {
  if (localMediaStream) {
    context.drawImage(video, 0, 0, 307,250);
    try {
      qrcode.decode();
    } catch(e) {
      qrcodeError(e);
    }
    setTimeout(scan, 500);
  } else {
    setTimeout(scan,500);
  }
}//end snapshot function
window.URL = window.URL || window.webkitURL || window.mozURL || window.msURL;
navigator.getUserMedia  = navigator.getUserMedia || navigator.webkitGetUserMedia ||      navigator.mozGetUserMedia || navigator.msGetUserMedia;
var successCallback = function(stream) {
    video.src = (window.URL && window.URL.createObjectURL(stream)) || stream;
    localMediaStream = stream;
    video.play();
    setTimeout(scan,1000);
}
// Call the getUserMedia method with our callback functions
if (navigator.getUserMedia) {
    navigator.getUserMedia({video: true}, successCallback, videoError);
} else {
    console.log('Native web camera streaming (getUserMedia) not supported in this  browser.');
    // Display a friendly "sorry" message to the user
}
qrcode.callback = qrcodeSuccess;
}; // end of html5_qrcode
})( jQuery );

我运行我的应用程序,它的工作,但在此之前,摄像头被打开,我有访问权限窗口,所以我想如果有任何解决方案,让我打开网络摄像头没有问。

很明显,你不能在未经同意的情况下打开别人的摄像头并开始录制。

在本地,您可以通过设置策略来打开特定功能,而无需询问某些供应商(如Chrome)。我在评论

中添加了一些链接