-
Notifications
You must be signed in to change notification settings - Fork 37
/
step4.html
76 lines (64 loc) · 1.86 KB
/
step4.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>监控系统</title>
</head>
<body>
<video width="640" height="480" autoplay></video>
<canvas width="640" height="480"></canvas>
<canvas width="640" height="480"></canvas>
<script>
var video = document.querySelector('video');
var canvas = document.querySelectorAll('canvas')[0];
var canvasForDiff = document.querySelectorAll('canvas')[1];
// video捕获摄像头画面
navigator.webkitGetUserMedia({
video: true
}, success, error);
function success(stream) {
video.src = window.URL.createObjectURL(stream);
video.play();
}
function error(err) {
alert('video error: ' + err)
}
//canvas
var context = canvas.getContext('2d'),
diffCtx = canvasForDiff.getContext('2d');
//将第二个画布混合模式设为“差异”
diffCtx.globalCompositeOperation = 'difference';
var preFrame, //前一帧
curFrame; //当前帧
//捕获并保存帧内容
function captureAndSaveFrame(){
preFrame = curFrame;
context.drawImage(video, 0, 0, 640, 480);
curFrame = canvas.toDataURL(); //转为base64并保存
}
//绘制base64图像到画布上
function drawImg(src, ctx){
ctx = ctx || diffCtx;
var img = new Image();
img.src = src;
ctx.drawImage(img, 0, 0, 640, 480);
}
//渲染前后两帧差异
function renderDiff(){
if(!preFrame || !curFrame) return; console.log(9);
diffCtx.clearRect(0, 0, 640, 480);
drawImg(preFrame);
drawImg(curFrame);
}
//定时捕获
function timer(delta){
setTimeout(function(){
captureAndSaveFrame();
renderDiff();
timer(delta)
}, delta || 500);
}
timer();
</script>
</body>
</html>