2022-08-08 10:55:27 +08:00
|
|
|
|
<!doctype html>
|
|
|
|
|
<html>
|
|
|
|
|
<head>
|
|
|
|
|
<meta charset="utf-8">
|
|
|
|
|
<title>jQuery-webcam-master</title>
|
2022-08-17 17:21:01 +08:00
|
|
|
|
<!-- <link href="cs.css" rel="stylesheet" type="text/css"> -->
|
2022-08-08 10:55:27 +08:00
|
|
|
|
<script src="jquery.js"></script>
|
|
|
|
|
<script src="jquery.webcam.min.js"></script>
|
2022-10-18 15:00:33 +08:00
|
|
|
|
<!-- <script src="excanvas.js"></script> -->
|
2022-08-11 17:29:34 +08:00
|
|
|
|
</head>
|
|
|
|
|
<body>
|
2022-08-16 16:03:30 +08:00
|
|
|
|
<div id="webcam" style="float: left; clear: both;"></div>
|
2022-08-17 17:21:01 +08:00
|
|
|
|
<!-- <input id="snapBtn" type="button" value="人脸识别" style="float: left; clear: both;"/>
|
|
|
|
|
<img id="base64image" src='' width="450" height="320" style="float: left; clear: both;"/> -->
|
2022-08-08 10:55:27 +08:00
|
|
|
|
<script type="text/javascript">
|
2022-10-18 15:00:33 +08:00
|
|
|
|
var pos = 0, ctx = null, saveCB,w = 380,h= 200, image = new Array();
|
|
|
|
|
var canvas = document.createElement("canvas");//创建画布指定宽度和高度
|
|
|
|
|
canvas.setAttribute('width', 320);
|
|
|
|
|
canvas.setAttribute('height', 240);
|
|
|
|
|
document.body.appendChild(canvas);
|
|
|
|
|
// canvas = window.G_vmlCanvasManager.initElement(canvas);
|
|
|
|
|
ctx = canvas.getContext("2d");//设置画布为2d,未来可能支持3d
|
|
|
|
|
image = ctx.getImageData(0, 0, 320, 240);//截图320*240,即整个画布作为有效区(cutx?)
|
2022-08-11 17:29:34 +08:00
|
|
|
|
$(document).ready(function() {
|
|
|
|
|
$("#webcam").webcam({
|
|
|
|
|
width: w,
|
|
|
|
|
height: h,
|
|
|
|
|
mode: "callback",
|
|
|
|
|
swffile: "jscam_canvas_only.swf", // 这里引入swf文件,注意路径
|
|
|
|
|
onTick: function(remain) {},
|
|
|
|
|
onSave: function(data) {
|
2022-10-18 15:00:33 +08:00
|
|
|
|
var col = data.split(";");//把data切割为数组
|
|
|
|
|
var img = image;
|
|
|
|
|
//绘制图像
|
|
|
|
|
//参数data 只是每行的数据 ,例如320*240 大小的照片,一张完整的照片下来需要240个data,每个data有320个rgb
|
|
|
|
|
for(var i = 0; i < 320; 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;
|
|
|
|
|
}
|
|
|
|
|
//当绘制320*240像素的图片时发给后端php
|
|
|
|
|
if (pos == 4 * 320 * 240) {
|
|
|
|
|
//把图像放到画布上,输出为png格式
|
|
|
|
|
ctx.putImageData(img, 0, 0);
|
|
|
|
|
window.parent.postMessage({"image": canvas.toDataURL("image/png")}, '*');
|
2022-08-11 17:29:34 +08:00
|
|
|
|
image = new Array();
|
|
|
|
|
pos = 0;
|
|
|
|
|
}
|
2022-10-18 15:00:33 +08:00
|
|
|
|
// image.push(data);
|
|
|
|
|
// pos += 4 * 320;
|
|
|
|
|
//post>= 4 * 320(x轴像素) * 240(y轴像素) 表示读取图像数据完毕
|
|
|
|
|
// if (pos >= 4 * 320 * 240) {
|
|
|
|
|
// window.parent.postMessage({"image":image.join('|')}, '*');
|
|
|
|
|
// image = new Array();
|
|
|
|
|
// pos = 0;
|
|
|
|
|
// }
|
2022-08-11 17:29:34 +08:00
|
|
|
|
},
|
|
|
|
|
|
2022-10-18 15:00:33 +08:00
|
|
|
|
onCapture: function(data) {
|
2022-08-11 17:29:34 +08:00
|
|
|
|
webcam.save();
|
|
|
|
|
// Show a flash for example
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
debug: function(type, string) {
|
|
|
|
|
console.log('type:' + type + ',string:' + string);
|
|
|
|
|
// Write debug information to console.log() or a div
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onLoad: function() {
|
|
|
|
|
// Page load
|
2022-08-08 10:55:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-11 17:29:34 +08:00
|
|
|
|
});
|
|
|
|
|
|
2022-08-17 17:21:01 +08:00
|
|
|
|
// $('#snapBtn').on('click', function() {
|
|
|
|
|
// webcam.capture();
|
|
|
|
|
// });
|
2022-08-12 10:24:41 +08:00
|
|
|
|
|
|
|
|
|
function receiveMessageFromParent ( event ) {
|
2022-09-07 16:20:59 +08:00
|
|
|
|
if(event.data == 'releaseCamera'){
|
|
|
|
|
location.reload();
|
|
|
|
|
}else if(event.data = 'capture'){
|
|
|
|
|
webcam.capture();
|
|
|
|
|
}
|
2022-08-17 17:21:01 +08:00
|
|
|
|
//$('#base64image').attr('src', 'data:image/jpg;base64,' + event.data.data);
|
2022-08-12 10:24:41 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
window.addEventListener('message', receiveMessageFromParent, false);
|
2022-08-11 17:29:34 +08:00
|
|
|
|
});
|
2022-08-08 10:55:27 +08:00
|
|
|
|
</script>
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|
|
|
|
|
|