HTML 调用电脑手机摄像头拍照

jxph040365 jxph040365 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
关于调用电脑摄像头拍照怎么不行,
我在网上下载很多,运行都不行,
运行浏览器也没有提示。是用谷歌浏览器。



<!doctype html>
<html>
<head>
<title>html5 capture test</title>
</head>
<body>
<video autoplay></video>
<img src="">
<canvas style="display: none;"></canvas>
<button id="capture">
snapshot</button>

<script type="text/javascript">
var video = document.querySelector('video');
var canvas = document.querySelector('canvas');
var ctx = canvas.getContext('2d');
var localMediaStream = null;

var snapshot = function() {
if (localMediaStream) {
ctx.drawImage(video, 0, 0);
document.querySelector('img').src = canvas.toDataURL('image/webp');
}
};

var sizeCanvas = function() {
setTimeout(function() {
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
img.width = video.videoWidth;
img.height = video.videoHeight;
}, 100);
};

var btnCapture = document.getElementById('capture');
btnCapture.addEventListener('click', snapshot, false);

if (navigator.getUserMedia) {
navigator.getUserMedia(
{ video: true },
function(stream) {
video.src = stream;
localMediaStream = stream;
sizeCanvas();
},
function() {
alert('your browser does not support getUserMedia');
}
);
}
else if (navigator.webkitGetUserMedia) {
navigator.webkitGetUserMedia(
{ video: true },
function(stream) {
video.src = window.URL.createObjectURL(stream);
localMediaStream = stream;
sizeCanvas();
},
function() {
alert('your browser does not support webkitGetUserMedia');
}
);
}
</script>

</body>
</html>


...全文
评论 32
收藏 已收藏
分享
返回顶部
jxph040365
.NET社区 62,271 成员 668,968 内容