使用HTML5拍照示例代码

2022-10-20,,,

演示地址: html5拍照演示
首先,我们看看html代码结构,当然,这部分的dom内容应该是在用户允许使用其摄像头事件出发后,动态加载生成的。
注意: 我们采用的是 640x480的分辨率,如果采用js动态生成,那么您是可以灵活控制分辨率的。

复制代码代码如下:
<!--
声明: 此div应该在允许使用webcam,网络摄像头之后动态生成
宽高: 640 *480,当然,可以动态控制啦!
-->
<!--
ideally these elements aren't created until it's confirmed that the
client supports video/camera, but for the sake of illustrating the
elements involved, they are created with markup (not javascript)
-->
<video id="video" width="640" height="480" autoplay></video>
<button id="snap">snap photo</button>
<canvas id="canvas" width="640" height="480"></canvas>

javascript
只要上面的html元素创建完成,那么javascript部分将简单的超乎你想象的简单:

复制代码代码如下:
// 设置事件监听,dom内容加载完成,和jquery的$.ready() 效果差不多。
window.addeventlistener("domcontentloaded", function() {
// canvas 元素将用于抓拍
var canvas = document.getelementbyid("canvas"),
context = canvas.getcontext("2d"),
// video 元素,将用于接收并播放摄像头 的数据流
video = document.getelementbyid("video"),
videoobj = { "video": true },
// 一个出错的回调函数,在控制台打印出错信息
errback = function(error) {
if("object" === typeof window.console){
console.log("video capture error: ", error.code);
}
};
// put video listeners into place
// 针对标准的浏览器
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);
}
// 对拍照按钮的事件监听
document.getelementbyid("snap").addeventlistener("click", function() {
// 画到画布上
context.drawimage(video, 0, 0, 640, 480);
});
}, false);

最后,记得讲您的网页放到web服务器下面,然后通过http协议来访问哦。
另外,需要浏览器版本较新,并且支持html5的相关新特性才可以。
译者不算称职啦,没有按原文来翻译。使用的浏览器是chrome 28。
最后,贴上完整的代码,比较呆板。

复制代码代码如下:
<!doctype html>
<html>
<head>
<title> 浏览器webcamera </title>
<meta name="generator" content="editplus">
<meta name="author" content="renfufei@qq.com">
<meta name="description" content="inveted by: http://davidwalsh.name/browser-camera">
<script>
// 设置事件监听,dom内容加载完成,和jquery的$.ready() 效果差不多。
window.addeventlistener("domcontentloaded", function() {
// canvas 元素将用于抓拍
var canvas = document.getelementbyid("canvas"),
context = canvas.getcontext("2d"),
// video 元素,将用于接收并播放摄像头 的数据流
video = document.getelementbyid("video"),
videoobj = { "video": true },
// 一个出错的回调函数,在控制台打印出错信息
errback = function(error) {
if("object" === typeof window.console){
console.log("video capture error: ", error.code);
}
};
// put video listeners into place
// 针对标准的浏览器
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);
}
// 对拍照按钮的事件监听
document.getelementbyid("snap").addeventlistener("click", function() {
// 画到画布上
context.drawimage(video, 0, 0, 640, 480);
});
}, false);
</script>
</head>
<body>
<div>
<!--
声明: 此div应该在允许使用webcam,网络摄像头之后动态生成
宽高: 640 *480,当然,可以动态控制啦!
-->
<!--
ideally these elements aren't created until it's confirmed that the
client supports video/camera, but for the sake of illustrating the
elements involved, they are created with markup (not javascript)
-->
<video id="video" width="640" height="480" autoplay></video>
<button id="snap">snap photo</button>
<canvas id="canvas" width="640" height="480"></canvas>
</div>
</body>
</html>

《使用HTML5拍照示例代码.doc》

下载本文的Word格式文档,以方便收藏与打印。