Javascript 如何启用浏览器以访问摄像头?
嗨,目前我正在使用Chrome浏览器的摄像头API。但我的浏览器不支持navigation.getUserMedia 嗨,分享一些代码 这是我的HTML页面:Javascript 如何启用浏览器以访问摄像头?,javascript,css,html,Javascript,Css,Html,嗨,目前我正在使用Chrome浏览器的摄像头API。但我的浏览器不支持navigation.getUserMedia 嗨,分享一些代码 这是我的HTML页面: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>getUserMedi</title> <link rel="stylesheet" href="style.css"&g
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>getUserMedi</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="video-container">
<video id="camera-stream" width="500" autoplay></video>
</div>
<script src="camera.js">
</script>
</body>
</html>
这里是camera.js文件:
window.onload = function() {
// Normalize the various vendor prefixed versions of getUserMedia.
navigator.getUserMedia = (navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia);
if (navigator.getUserMedia) {
// Request the camera.
navigator.getUserMedia(
// Constraints
{
video: true
},
// Success Callback
function(localMediaStream) {
// Get a reference to the video element on the page.
var vid = document.getElementById('camera-stream');
// Create an object URL for the video stream and use this
// to set the video source.
vid.src = window.URL.createObjectURL(localMediaStream);
},
// Error Callback
function(err) {
// Log the error to the console.
console.log('The following error occurred when trying to use
getUserMedia: ' + err);
}
);
} else {
alert('Sorry, your browser does not support getUserMedia');
}
};
这里我的相机无法打开。浏览器也不支持,请澄清这家伙的问题。首先,
导航。getUserMedia已被弃用,大多数浏览器不再支持它
Mozilla.org网站上推荐的新API是navigator.mediaDevices.getUserMedia
请通过下面的URL阅读更多内容
首先,导航.getUserMedia
已被弃用,大多数浏览器不再支持它
Mozilla.org网站上推荐的新API是navigator.mediaDevices.getUserMedia
请通过下面的URL阅读更多内容
这正按预期工作。我只需要删除错误回调函数中控制台日志中的换行符。刚刚看到getUserMedia()
不再适用于不安全的源代码。看一看就知道了。所以,也许这就是为什么我删除了chrome中不安全的origin.jsfiddle–DavidDomain,但我没有得到接受的输出。你得到了什么错误?你能给出一些可能的答案吗?这是预期的工作。我只需要删除错误回调函数中控制台日志中的换行符。刚刚看到getUserMedia()
不再适用于不安全的源代码。看一看就知道了。所以,也许这就是为什么我删除了chrome中不安全的origin.jsfiddle–DavidDomain,但是我没有得到接受的输出。你得到了什么错误?你能给出一些可能的答案吗。
window.onload = function() {
// Normalize the various vendor prefixed versions of getUserMedia.
navigator.getUserMedia = (navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia);
if (navigator.getUserMedia) {
// Request the camera.
navigator.getUserMedia(
// Constraints
{
video: true
},
// Success Callback
function(localMediaStream) {
// Get a reference to the video element on the page.
var vid = document.getElementById('camera-stream');
// Create an object URL for the video stream and use this
// to set the video source.
vid.src = window.URL.createObjectURL(localMediaStream);
},
// Error Callback
function(err) {
// Log the error to the console.
console.log('The following error occurred when trying to use
getUserMedia: ' + err);
}
);
} else {
alert('Sorry, your browser does not support getUserMedia');
}
};