<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>web_rtc_1
</title>
<style>
video,canvas{
border: 1px solid gray;
width: 480px;
height: 320px;
}
</style>
</head>
<body>
<video autoplay></video>
<canvas></canvas>
<button id="capture">Capture
</button>
<script>
function hasUserMedia() {
return !!(navigator.getUserMedia)
}
if (hasUserMedia()){
var video = document.querySelector('video'),
canvas = document.querySelector('canvas'),
streaming = false;
navigator.getUserMedia(
{video: true, audio: false},
function (stream){
try {
video.srcObject = stream;
} catch (error) {
video.src = window.URL.createObjectURL(stream);
}
streaming = true;
},function (error) {
console.log("Raised an error when capturing:",error);
});
document.querySelector('#capture').addEventListener('click',function (event) {
console.log("点击了capture");
if (streaming) {
canvas.width = video.clientWidth;
canvas.height = video.clientHeight;
var context = canvas.getContext('2d');
context.drawImage(video,0,0,480,320);
}
});
}else {
alert("对不起,您的浏览器不支持getUserMedia.");
}
</script>
</body>
</html>
转载请注明原文地址: https://lol.8miu.com/read-16813.html