-
Notifications
You must be signed in to change notification settings - Fork 0
/
wow3.html
275 lines (224 loc) · 6.96 KB
/
wow3.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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<!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>
</head>
<body>
<h1 id="tit">两个视频合成</h1>
<div id="mod_player" style="opacity: 0;z-index:9; position:absolute; top:470px;left:20px;overflow: hidden;width:60px">
<video id="vv" width=60 height=30 controls="controls" xloop="loop" webkit-playsinline x-webkit-airplay="true" data-playing-loadingad="0" preload="none" src="/images/huabanxie.mp4"></video>
</div>
<div style="margin-top: 10px;z-index:1">
<canvas width=352 height=360 id="thecanvas" style="border: 1px #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>
<canvas width=300 height=240 id="precanvas" style="border: 1px #666666 solid;"></canvas>
</div>
<div style="display:none;">
<video id="vhead" width=416 height=240 controls="controls" xloop="loop" webkit-playsinline x-webkit-airplay="true" data-playing-loadingad="0" preload="none" src="/images/mysong.mp4"></video>
<canvas width=416 height=240 id="shoot" style="border: 1px #666666 solid;"></canvas>
</div>
<div id="saveImg"></div>
</body>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>
<script src="/javascripts/jquery.facedetection.js"></script>
<script>
var $e = function (a){
return "string"==typeof a ? document.getElementById(a) : a;
}
$('#ahead').faceDetection({
complete: function (faces) {
console.log(faces);
}
});
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 < 1600;
}
function checkPosition(imageData){
var min_x = 9999,
min_y = 9999,
max_x = 0,
max_y = 0;
var min_y_x = 0,
max_y_x = 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;
min_y_x = x;
}
if (x > max_x) max_x = x;
if (y > max_y) {
max_y = y;
max_y_x = x;
}
}
//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,
rotate : Math.atan((min_y_x - max_y_x)/(max_y - min_y))*1.0,
imageData: imageData,
};
}
return null;
}
window.onload = function() {
if (window.location.href.indexOf("show") > 0){
$("div").show();
}
var v = $e("vv");
v.load();
v.play();
//v.muted = true;
var vhead = $e("vhead");
vhead.load();
//vhead.play();
vhead.muted = true;
//v.play();
var pre_can = $e("precanvas");
var pre_ctx = pre_can.getContext("2d");
var videoWidth = 352;
var videoHeight = 360;
var xx = 120,yy=70,ww=150,hh=110;
//设置旋转点到画布中心
pre_ctx.translate(pre_can.width/2, pre_can.height/2);
//旋转画布,为了修正图片方向
pre_ctx.rotate(Math.PI/2);
pre_ctx.translate(ww/2 - pre_can.height/2, pre_can.width/2 - hh/2);
//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('pause',function() {
vhead.pause();
});
//渲染画面
v.addEventListener('play',function() {
vhead.play();
var i = window.setInterval(function(){
var start = new Date();
//底层MV
ctx.drawImage(v, 0, 0, videoWidth, videoHeight);
if (filter == 1){
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);
var imgData = canvas.toDataURL("image/png");
var img = new Image();
img.src = imgData;
//console.log(re.rotate *180/Math.PI);
//pre_ctx.fillStyle = "rgba(125, 46, 138, 0.5)";
//pre_ctx.fillRect(0, 0, ww, hh);
//旋转画布,为了修正图片方向
pre_ctx.rotate(re.rotate);
pre_ctx.drawImage(vhead, xx, yy, ww, hh, -ww/2, -hh/2, ww, hh);
//再切换回来
pre_ctx.rotate(-re.rotate);
ctx.drawImage(pre_can, 0, 0, hh, ww, re.x, re.y, re.width, re.height );
ctx.drawImage(img, 0, 0, videoWidth, videoHeight);
//console.log(re.x + "," + re.y + "[" + re.width + "," + re.height + "]");
//console.log(((new Date()) - start));
//window.a = new Image();
//a.src = 'http://192.168.31.238?' + (+start) + "=" + ((new Date()) - start);
}
try{}catch(e){
alert("视频被重定向了,无法跨域读取:" + e);
clearInterval(i);
}
}
}, 65);
});
//截图
var imgCount = 0;
var shoot = $e("shoot");
var shoot_ctx = shoot.getContext("2d");
$("#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 = true;
$("#stop").on("click", function(){
if (playStatus){
this.innerHTML = "播放";
//vhead.pause();
v.pause();
}
else{
this.innerHTML = "暂停";
v.play();
//vhead.play();
}
playStatus = !playStatus;
});
$("#myTurn").on("click", function(){
filter = 1;
});
$("#reset").on("click", function(){
filter = 0;
});
$("#tit").dblclick(function(){
$("div").show();
});
};
</script>
</html>