司项目需要调用摄像头,看了一下html5文档,主要是使用html5的getUserMedia()API,写一个例子来记录具体的使用方法。
<html>
<body>
<!-- 用于展示摄像头视频流 -->
<video id="video" autoplay style="width: 480px;height: 320px"></video>
<div>
<button id="capture" onclick="handleClickCapture()">拍照</button>
</div>
<!-- 展示拍摄的照片 -->
<canvas id="canvas" width="480" height="320"></canvas>
<script>
var video = document.getElementById('video');
var capture = document.getElementById('capture');
var ctx = document.getElementById('canvas').getContext('2d');
/**
* 调用用户媒体设备
* @param constraints 配置信息
* @param success 成功回调函数
* @param error 失败回调函数
*/
function getUserMediaToPhoto(constraints,success,error) {
if(navigator.mediaDevices.getUserMedia){
navigator.mediaDevices.getUserMedia(constraints).then(success).catch(error);
}else if (navigator.webkitGetUserMedia) {
navigator.webkitGetUserMedia(constraints,success,error);
}else if(navigator.mozGetUserMedia){
navigator.mozGetUserMedia(constraints,success,error);
}else if(navigator.getUserMedia){
navigator.getUserMedia(constraints,success,error);
}
}
/**
* 成功回调函数
* @param stream 视频流
*/
function success(stream){
var CompatibleURL = window.URL || window.webkitURL;
try {
video.src = CompatibleURL.createObjectURL(stream);
} catch (e) {
video.srcObject = stream;
}
video.play();
}
/**
* 失败回调
* @param error 错误对象
*/
function error(error) {
console.log('无法访问媒体设备', error);
}
if(navigator.mediaDevices.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.getUserMedia){
getUserMediaToPhoto({video:{width:480,height:320}},success,error);
}else{
alert('不支持访问用户媒体设备');
}
/**
* 拍照按钮点击事件
*/
function handleClickCapture() {
ctx.drawImage(video,0,0,480,320);
}
</script>
</body>
</html>
实现了基本的摄像头调用和拍照,实现思路非常简单,基本上只是在调用api,唯一恶心的地方在于api版本比较多,不得不多做一些判断。具体的api介绍、使用和参数可以查看MediaDevices.getUserMedia()。
何通过js调用本地摄像头呢?获取后如何对视频进行截图呢?在这里跟大家做一个简易的Demo来实现以上几个功能。
以下先通过HTML我们来实现一个简单的布局,包括样式和按钮。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>H5 canvas 调用摄像头进行绘制</title>
<style>
html,body{
width:100%;
height:100%;
padding: 0px;
margin: 0px;
overflow: hidden;
}
#canvas{
width:500px;
height:300px;
}
#video{
width:500px;
height:300px;
}
.btn{
display:inline-block;
text-align: center;
background-color: #333;
color:#eee;
font-size:14px;
padding:5px 15px;
border-radius: 5px;
cursor:pointer;
}
</style>
</head>
<body>
<video id="video" autoplay="true" style="background-color:#ccc;display:none;"></video>
<div style="width:500px;height:300px;margin:30px auto;">
<canvas id="canvas" width="500px" height="300px">您的浏览器不支持H5 ,请更换或升级!</canvas>
<span class="btn" filter="screenshot">截图</span>
<span class="btn" filter="close">暂停</span>
<span class="btn" filter="open">打开</span>
</div>
<div style="width:500px;height:300px;margin:40px auto;" id="show"></div>
</body>
</html>样子差不多是这样的:
hahiahia 空白一片
我们将video进行了隐藏,然后加上了几个按钮,还有canvas以及最底部的图片展示区域(用来存放截图图片)。
这里先贴下核心代码:
navigator.getUserMedia({
video : {width:500,height:300}
},function(stream){
LV.video.srcObject = stream;
LV.video.onloadedmetadata = function(e) {
LV.video.play();
};
},function(err){
alert(err);//弹窗报错
})相关的知识点可以参考:https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia
然后根据页面逻辑实现事件以及其他功能,包括:截图、暂停。
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
var events = {
open : function(){
LV.open();
},
close : function(){
console.log(LV.timer);
clearInterval(LV.timer);
},
screenshot : function(){
//获得当前帧的图像并拿到数据
var image = canvas.toDataURL('jpeg');
document.getElementById('show').innerHTML = '<img src="'+image+'" style="width:500px;height:300px;" />'
}
};
var LV = {
video : document.getElementById('video'),
canvas : document.getElementById('canvas'),
timer : null,
media : null,
open :function(){
if(!LV.timer){
navigator.getUserMedia({
video : {width:500,height:300}
},function(stream){
LV.video.srcObject = stream;
LV.video.onloadedmetadata = function(e) {
LV.video.play();
};
},function(err){
alert(err);//弹窗报错
})
}
if(LV.timer){
clearInterval(LV.timer);
}
//将画面绘制到canvas中
LV.timer = setInterval(function(){
LV.ctx.drawImage(LV.video,0,0,500,300);
},15);
},
init : function(){
LV.ctx = LV.canvas.getContext('2d');
//绑定事件
document.querySelectorAll('[filter]').forEach(function(item){
item.onclick = function(ev){
var type = this.getAttribute('filter');
events[type].call(this,ev);
}
});
return LV;
}
};
LV.init();原谅我放荡不羁的命名 ...
具体已经实现的demo可以点击 https://chrunlee.cn/demos/canvas-video/index.html
自网络
这是大家在做信息类网站的时候经常要用到的一个功能:
理想情况下我们应该先判断你的设备上是否有摄像头或相机,但简单起见,我们在这里直接写出了HTML标记,而不是用JavaScript先判断然后动态生成这些标记
<video id="video" width="300" height="240" autoplay></video>
<button id="snap">拍照</button>
<canvas id="canvas" width="300" height="240"></canvas>
</body>
</html>
$( function() {
try { document.createElement("canvas").getContext("2d"); } catch (e) { alert("not support canvas!") }
var video = document.getElementById("video"),
canvas = document.getElementById("canvas"),
context = canvas.getContext("2d");
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
if (navigator.getUserMedia)
navigator.getUserMedia(
{ "video": true },
function (stream) {
if (video.mozSrcObject !== undefined)video.mozSrcObject = stream;
else video.src = ((window.URL || window.webkitURL || window.mozURL || window.msURL) && window.URL.createObjectURL(stream)) || stream;
video.play();
},
function (error) {
alert("Video capture error: " + error.code);
}
);
else alert("Native device media streaming (getUserMedia) not supported in this browser");
document.getElementById("snap").addEventListener("click", function() {
/*context.drawImage(video, 0, 0, 640, 480);//照片大小*/
context.drawImage(video, 0, 0, canvas.width = video.videoWidth, canvas.height = video.videoHeight)
}, false);
*请认真填写需求信息,我们会在24小时内与您取得联系。