jquery.webcam.js实现调用摄像头拍照兼容各个浏览器
1.demo 可直接复制使用,需要在环境里运行。
3.感谢你的阅读
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery-webcam-js</title>
<link href="cs.css" rel="stylesheet" type="text/css">
<script src="http://www.jq22.com/jquery/1.11.1/jquery.min.js"></script>
<script src="jquery.webcam.min.js"></script>
</head>
<body>
<button class="play">拍照</button>
<div id="status">倒计时</div>
<div id="webcam"></div>
</body>
<script>
var w = 320, h = 240;
var pos = 0, ctx = null, saveCB, image = [];
var canvas = document.createElement("canvas");
$("body").append(canvas);
canvas.setAttribute('width', w);
canvas.setAttribute('height', h);
ctx = canvas.getContext("2d");
image = ctx.getImageData(0, 0, w, h);
$("#webcam").webcam({
width: w,
height: h,
mode: "callback",
swffile: "jscam_canvas_only.swf",
onTick: function(remain) {
if (0 == remain) {
$("#status").text("拍照成功!");
} else {
$("#status").text("倒计时"+remain + "秒钟...");
}
},
onSave: function(data){
var col = data.split(";");
var img = image;
for(var i = 0; i < w; i++) {
var tmp = parseInt(col[i]);
img.data[pos + 0] = (tmp >> 16) & 0xff;
img.data[pos + 1] = (tmp >> 8) & 0xff;
img.data[pos + 2] = tmp & 0xff;
img.data[pos + 3] = 0xff;
pos+= 4;
}
if (pos >= 4 * w * h) {
ctx.putImageData(img,0,0);
pos = 0;
Imagedata=canvas.toDataURL().substring(22);
}
},
onCapture: function () {
webcam.save();
},
debug: function (type, string) {
console.log(type + ": " + string);
},
onLoad: function() {
console.log('加载完毕!')
var cams = webcam.getCameraList();
for(var i in cams) {
$("body").append("<p>" + cams[i] + "</p>");
}
}
});
$(".play").click(function(){
webcam.capture(5);
});
</script>
</html>