技术的进步使我们遇到了html5。下面这个简单粗暴的demo就是来完成这些功能的。直接看代码:
<!DOCTYPE html>
<html>
<head>
<title>html5调用摄像头实现拍照</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<video id="video" autoplay=""style='width:640px;height:480px'></video>
<button id="paizhao">拍照</button>
<canvas id="canvas" width="640" height="480"></canvas>
<script type="text/javascript">
var video=document.getElementById("video");
var context=canvas.getContext("2d");
var errocb=function(){
console.log("sth srong");
}
if(navigator.getUserMedia){
navigator.getUserMedia({"video":true},function(stream){
video.src=stream;
video.play();
},errocb);
}else if(navigator.webkitGetUserMedia){
navigator.webkitGetUserMedia({"video":true},function(stream){
video.src=window.webkitURL.createObjectURL(stream);
video.play();
},errocb);
}
document.getElementById("paizhao").addEventListener("click",function(){
context.drawImage(video,0,0,640,480);
});
</script>
</body>
</html>
虽然丑陋,但是整理整理还是可以使用的!
如有不懂,请加qq群:135430763共同学习!