HTML 5调用摄像头并进行拍照

时间:2021-12-30 12:54:01

一般情况下,DOMContentLoaded事件要在window.onload之前执行,当DOM树构建完成的时候就会执行DOMContentLoaded事件,而window.onload是在页面载入完成的时候,才执行,这其中包括图片等元素。大多数时候我们只是想在DOM树构建完成后,绑定事件到元素,我们并不需要图片元素,加上有时候加载外域图片的速度非常缓慢。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
video {
	border: 1px solid #ccc;
	display: block;
	margin: 0 0 20px 0;
	float:left;
}
#canvas {
	margin-top: 20px;
	border: 1px solid #ccc;
	display: block;
}
</style>
</head>
<body>
	<video id="video" width="500" height="400" autoplay></video>
	<canvas id="canvas" width="500" height="400"></canvas>
	<button id="snap">拍照</button>
	<script type="text/javascript">
		var context = canvas.getContext("2d");
		//当DOM树构建完成的时候就会执行DOMContentLoaded事件
		window.addEventListener("DOMContentLoaded", function() {
			//获得Canvas对象
			var canvas = document.getElementById("canvas");
			//获得video摄像头区域
			var video = document.getElementById("video");
			var videoObj = {
				"video" : true
			};
			var errBack = function(error) {
				console.log("Video capture error: ", error.code);
			};
			//获得摄像头并显示到video区域
			if (navigator.getUserMedia) { // Standard
				navigator.getUserMedia(videoObj, function(stream) {
					video.src = stream;
					video.play();
				}, errBack);
			} else if (navigator.webkitGetUserMedia) { // WebKit-prefixed
				navigator.webkitGetUserMedia(videoObj, function(stream) {
					video.src = window.webkitURL.createObjectURL(stream);
					video.play();
				}, errBack);
			} else if (navigator.mozGetUserMedia) { // Firefox-prefixed
				navigator.mozGetUserMedia(videoObj, function(stream) {
					video.src = window.URL.createObjectURL(stream);
					video.play();
				}, errBack);
			}
		}, false);
		// 触发拍照动作
		document.getElementById("snap").addEventListener("click", function() {
			context.drawImage(video, 0, 0, 640, 480);
		});
	</script>
</body>
</html>
HTML 5调用摄像头并进行拍照