首页 > 代码库 > PC端 H5实现拍照并上传
PC端 H5实现拍照并上传
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta content="telephone=no" name="format-detection" />
<title>测试</title>
</head>
<body>
<video id="video" width="320" height="240" autoplay></video>
<button id="snap">拍摄</button>
<canvas id="canvas" width="320" height="240"></canvas>
<script>
window.addEventListener("DOMContentLoaded", function() {
var canvas = document.getElementById("canvas"),//调用canvas接口
context = canvas.getContext("2d"),
video = document.getElementById("video"),
videoObj = { "video": true },
errBack = function(error) {//错误处理
console.log("Video capture error: ", error.code);
};
if(navigator.getUserMedia) {//调用html5拍摄接口
navigator.getUserMedia(videoObj, function(stream) {
video.src = http://www.mamicode.com/stream;//摄像机属于视频流,所以当然要输出到html5的video标签中了
video.play();//开始播放
}, errBack);
} else if(navigator.webkitGetUserMedia) { //WebKit内核调用html5拍摄接口
navigator.webkitGetUserMedia(videoObj, function(stream){
video.src = http://www.mamicode.com/window.webkitURL.createObjectURL(stream);//同上
video.play();//同上
}, errBack);
}
else if(navigator.mozGetUserMedia) { //moz内核调用html5拍摄接口
navigator.mozGetUserMedia(videoObj, function(stream){
video.src = http://www.mamicode.com/window.URL.createObjectURL(stream);//同上
video.play();//同上
}, errBack);
}
}, false);
document.getElementById("snap")
.addEventListener("click", function() {//获取拍照按钮绑定事件
var canvans = document.getElementById("canvas"),//调用canvas接口
context = canvas.getContext("2d");
context.drawImage(video, 0, 0, 640, 480);//调用canvas接口的drawImage方法绘制当前video标签中的静态图片,其实就是截图
var imgData = http://www.mamicode.com/canvans.toDataURL();//获取图片的base64格式的数据
//这里就可以写上传服务器代码了
});
</script>
</body>
</html>
PC端 H5实现拍照并上传
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。