<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Display Webcam Stream</title>
<style>
#container {
margin: 0px auto;
width: 500px;
height: 375px;
border: 10px #333 solid;
}
#videoElement {
width: 500px;
height: 375px;
background-color: #666;
}
</style>
</head>
<body>
<div id="container">
<video autoplay="true" id="videoElement">
</video>
</div>
<script>
var video = document.querySelector("#videoElement");
if (navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({ video: true })
.then(function (stream) {
video.srcObject = stream;
})
.catch(function (err0r) {
console.log("Something went wrong!");
});
}
</script>
</body>
</html>
如果不能调用,请确保网址为https的,只有https的才可以调用成功。
如果要在iframe中调用此页面,打不开摄像头,请在<iframe 中加 allow="camera *; microphone *" 就可以了。
例如<iframe allow="camera *; microphone *" src="opencam.html"></iframe>
最后更新于 2022年4月23日
相关博文
html5调用摄像头代码
