浏览器调用电脑摄像头进行拍照
效果图:
实现原理=>
开启摄像头获取流,video加载实时影像,利用canvas绘画video的影像,canvas生成图片,转文件流上传或下载
实现拍照功能的主要三个要素:
1: navigator.mediaDevices.getUserMedia() 获取浏览器摄像头权限(包括录音设备);
2:成功获取权限后,createObjectURL生成本地url赋值给video,开始播放视频;
3:点击拍照时,触发canvas对video的画面进行绘画。
有几种无法拍照的情况:
1:IE浏览器不支持navigator属性
2:访问路径为IP地址时
浏览器有机制,在访问路径不安全时是不可以使用navigator属性的
只有访问地址为localhost,https,file才可以
3:摄像头被占用
下面贴出完整代码:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>拍照</title>
<style>
body{
background: #B3B3B3
}
#cellphone{
height: 575px;
width: 310px;
border-radius: 35px;
border: 1px solid #000;
margin:40px auto;
background: #000;
}
#camera_content{
margin: 20px,3px,0px,3px;
width: 300px;
height: 300px;
margin: auto;
background: #fff
}
.Water_drop{
margin: auto;
width: 166px;
height: 20px;
border: 1px solid #000;
border-top: 0px;
border-bottom-right-radius: 10px;
border-bottom-left-radius: 10px;
padding-top: 16px;
background: #fff
}
.telephone_receiver{
width: 125px;
height: 6px;
border-radius: 3px;
border: 1px solid #000;
margin-left: 5px;
float: left
}
.camera{
width: 8px;
height: 8px;
border-radius: 4px;
border: 1px solid #000;
float: left;
margin-left: 5px
}
.flashlight{
width: 8px;
height: 8px;
border-radius: 4px;
margin-left: 5px;
border: 1px solid #000;
float: left
}
#setting{
width: 308px;
margin-top: 10px;
margin-bottom: 16px
}
#setting i{
display: inline-block;
font-size: 35px;
margin-left: 40px;
color: #D8D8D8
}
#keystroke{
width: 308px;
margin-top: 32px;
}
#Photo_album,#btu{
width: 70px;
height: 70px;
border: 1px solid #A7A7A7;
border-radius: 35px;
margin-left: 27px;
float: left;
background: #fff;
cursor: pointer;
}
#btu{
line-height: 70px;
text-align:center;
font-size: 26px;
font-family: LiSu;
}
#Make_sure{
width: 70px;
height: 45px;
border-radius: 25px;
background: #fff;
cursor: pointer;
float: right;
font-family: LiSu;
margin-right: 20px;
line-height: 45px;
text-align:center;
font-size: 20px;
display: none
}
#Photo_album>img{
width: 70px;
height: 70px;
border-radius: 35px;
}
#btu{
margin-top: 38px;
}
#btu:active{
margin-left: 30px;
margin-top: 41px;
width: 64px;
height: 64px;
border-radius: 32px;
}
</style>
</head>
<body>
<div id="cellphone">
<div class="Water_drop">
<div class="camera"></div>
<div class="telephone_receiver"></div>
<div class="flashlight"></div>
</div>
<div id="setting">
<i>♩</i>
<i>♪</i>
<i>♫</i>
<i>♬</i>
</div>
<div id="camera_content">
<video id="video" style="object-fit:cover;" height="300" width="300" autoplay></video>
<canvas style="display:none;" id="canvas" height="300" width="300"></canvas>
</div>
<div id="keystroke">
<div id="Photo_album">
<img src="1564667419970.jpeg" id="Photo_al" alt="">
</div>
<div id="btu"></div>
<div id="Make_sure">确定</div>
</div>
</div>
<script>
//绑定id元素
var cellphone = document.getElementById("cellphone"),
camera_content = document.getElementById("camera_content"),
video = document.getElementById("video"),
canvas = document.getElementById("canvas"),
content = canvas.getContext("2d"),
Photo_album = document.getElementById("Photo_album"),
Photo_al = document.getElementById("Photo_al"),
btu = document.getElementById("btu"),
Make_sure = document.getElementById("Make_sure"),
Photo_album = document.getElementById("Photo_album"),
imgbox,
iden=true;
//禁用右键
cellphone.oncontextmenu = function(){
return false;
}
//调用摄像头
var constraints = {video: {width: 300,height:300}};
navigator.mediaDevices.getUserMedia(constraints)
.then(function(success){
if("srcObject" in video){
video.srcObject = success
}else{
video.src = window.URL.createObjectURL(success);
}
//流加载完毕后开始播放
video.onloadedmetadata = function () {
video.play();
};
})
.catch(function(err){
var Error = err.toString().split(":")[0];
if(Error=="NotFoundError"){
alert("检测不到摄像头!")
}else if(Error=="NotAllowedError"){
alert("请允许浏览器调用摄像头")
}else{
alert("请查看摄像头是否被占用!")
}
})
//==============按钮功能==============
//拍照按钮 iden=true:拍照 false:重拍
btu.onclick = function(){
if(iden){
Make_sure.style.display = "block"
btu.innerHTML="重拍"
iden=false;
//拍照成功,生成图片
//使用canvas进行绘图
content.drawImage(video,0,0,300,300)
// 将画布转成base64编码
var image = canvas.toDataURL('image/jpg');
//生成一张图片
var img = new Image();
//设置属性和src
img.id = "imgbox";
img.src = image;
video.style.display="none"
//将图片放到html中
camera_content.append(img);
imgbox = document.getElementById("imgbox");
}else{
imgbox.remove();//删除照片
Make_sure.style.display = "none"
btu.innerHTML=""
iden=true;
//重拍
video.style.display="block"
}
}
//确认
Make_sure.onclick = function(){
Photo_al.src = imgbox.src;
imgbox.remove();//删除照片
Make_sure.style.display = "none"
iden=true;
btu.innerHTML=""
video.style.display="block";
}
//==============按钮功能==============
</script>
</body>
</html>