-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwow2.html
More file actions
210 lines (172 loc) · 4.99 KB
/
wow2.html
File metadata and controls
210 lines (172 loc) · 4.99 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width,minimum-scale=0.5,maximum-scale=5,user-scalable=yes,minimal-ui" />
<title>视频图像合成</title>
<head>
<style>
img {display:block;}
div {mergin:10px;}
</style>
<!-- <script src="http://libs.baidu.com/backbone/0.9.2/backbone.js"></script> -->
</head>
<body bgcolor="#999999">
<h1 id="tit">视频图像合成</h1>
<div id="mod_player" style="z-index:1;position:absolute">
<video id="vv" width=300 height=500 controls="controls" xloop="loop" playsinline="true" webkit-playsinline="true" x5-video-player-type="h5" data-playing-loadingad="0" preload="none" src="/images/huabanxie.mp4" style="z-index:1"></video>
</div>
<div style="margin-top: 10px;z-index:9;position:absolute">
<canvas width=352 height=360 id="thecanvas" style="border: 5px #666666 solid;"></canvas>
</div>
<div style="margin:20px">
<button id="stop">播放</button>
<button id="saveImageBtn">截图</button>
<button id="reset">查看源视频</button>
<button id="myTurn">还原</button>
</div>
<div style="display:none;">
<img src="/images/myhead.png" id='ahead' />
<canvas width=352 height=360 id="shoot"></canvas>
</div>
<div id="saveImg">
</div>
</body>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>
<script>
var $e = function (a){
return "string"==typeof a ? document.getElementById(a) : a;
}
var canvas = $e("thecanvas");
var ctx = canvas.getContext("2d");
var filter = 1;
function isMatch(r,g,b){
var matchPoint = [15,226,93];//rgb
var absR = matchPoint[0] - r;
var absG = matchPoint[1] - g;
var absB = matchPoint[2] - b;
var dis = absR*absR+absG*absG+absB*absB;
return dis < 900;
}
function checkPosition(imageData){
var min_x = 9999,
min_y = 9999,
max_x = 0,
max_y = 0;
for (var y = 0; y < imageData.height; y++) {
if (max_y && y > (max_y + 5)){
break;
}
for (x = 0;x < imageData.width; x++) {
if (max_x && x > (max_x + 5)){
break;
}
var p = (y * imageData.width + x) * 4;
//一个像素占四个字节,即 p 为当前指针的位置
if (isMatch(imageData.data[p],
imageData.data[p+1],
imageData.data[p+2])){
//match
imageData.data[p+3]=0;
if (x < min_x) min_x = x;
if (y < min_y) min_y = y;
if (x > max_x) max_x = x;
if (y > max_y) max_y = y;
}
//imageData.data[p+3]=0;
}
}
if (max_x){
return {
x : min_x,
y : min_y,
width : max_x - min_x,
height : max_y - min_y,
imageData: imageData,
};
}
return null;
}
window.onload = function() {
if (window.location.href.indexOf("show") > 0){
$("div").show();
}
var v = $e("vv");
v.load();
//v.play();
var shoot = $e("shoot");
var shoot_ctx = shoot.getContext("2d");
var videoWidth = 352;
var videoHeight = 360;
//video事件,先触发play,再触发loadedmetadata
v.addEventListener('loadedmetadata', function(){
return;
//重置高宽
videoWidth = v.videoWidth;
videoHeight = v.videoHeight;
canvas.width = videoWidth;
canvas.height = videoHeight;
shoot.width = videoWidth;
shoot.height = videoHeight;
});
//渲染画面
v.addEventListener('play',function() {
var head = $e("ahead");
var i = window.setInterval(function(){
var start = new Date();
ctx.drawImage(v, 0, 0, videoWidth, videoHeight);
if (filter == 1){
try{
var imageData = ctx.getImageData(0, 0, videoWidth, videoHeight);
var re = checkPosition(imageData);
if (re){
//put a head into the picture
ctx.putImageData(re.imageData, 0, 0);
ctx.drawImage(head, re.x, re.y, re.width, re.height);
}
}catch(e){
alert("视频被重定向了,无法跨域读取");
clearInterval(i);
}
}
}, 65);
});
//截图
var imgCount = 0;
$("#saveImageBtn").on("click", function(){
shoot_ctx.drawImage(canvas, 0, 0, videoWidth, videoHeight);
imgCount++;
shoot_ctx.fillStyle = "rgba(0, 0, 0, 0.5)";
shoot_ctx.fillRect(10,10,50,50);
shoot_ctx.font = "40pt Calibri";
shoot_ctx.fillStyle = "rgba( 255, 255, 255, 0.8)"; // black color
shoot_ctx.fillText("" + imgCount, 22, 50);
var imgData = shoot.toDataURL("image/png");
var img = new Image();
img.src = imgData;
$("#saveImg").append(img);
});
var playStatus = false;
$("#stop").on("click", function(){
if (playStatus){
this.innerHTML = "播放";
v.pause();
}
else{
this.innerHTML = "暂停";
v.play();
}
playStatus = !playStatus;
});
$("#myTurn").on("click", function(){
filter = 1;
});
$("#reset").on("click", function(){
filter = 0;
});
$("#tit").dblclick(function(){
$("div").show();
});
};
</script>
</html>