-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcan.html
More file actions
312 lines (266 loc) · 8.19 KB
/
Copy pathcan.html
File metadata and controls
312 lines (266 loc) · 8.19 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<!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,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no,minimal-ui" />
<title>人脸识别 & canvas应用</title>
<head>
<style>
img {width:200px;}
div {margin:10px;}
.notice {
padding:5px;
border: dotted 2px #999999;
width: 300px;
background: #eeeeee;
}
.face {
position: absolute;
border: 2px solid #FFF;
}
</style>
</head>
<body>
<div class="notice">
该demo包括:
<ul style="margin:0px">
<li>读取本地图片base64信息</li>
<li>读取图片Exif信息,自动旋转图片</li>
<li>人脸识别,找出图片中的脸部区域</li>
<li>将多个图片绘制进canvas</li>
<li>将canvas输出到图片</li>
</ul>
</div>
<div>
选择照片:<input type="file" id="file" />
</div>
<div>
<img id="logo" src="/images/font.png" />
</div>
<div id="tips" style="color: #666666;"></div>
<div>
<canvas width=300 height=276 id="thecanvas" style="border: 1px #999999 solid;"></canvas>
</div>
<div>
<canvas width=1200 height=1200 id="precanvas" style="border: 1px #999999 solid;"></canvas>
</div>
<div>
<button id="saveImageBtn">Save Image</button>
<button id="downloadImageBtn">Download Image</button>
</div>
<div>
<pre id="imgExif">
</pre>
</div>
<div>
<img src id="can2img" alt="canvas to image" />
</div>
</body>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>
<script src="./javascripts/exif.js"></script>
<script src="/javascripts/jquery.facedetection.js"></script>
<script>
var $e = function (a){
return "string"==typeof a ? document.getElementById(a) : a;
}
var canvas = $e("thecanvas");
var ctx = canvas.getContext("2d");
window.onerror = function (msg,url,l){
var txt = "Error: " + msg + "\n";
txt+="URL: " + url + "\n";
txt+="Line: " + l + "\n\n";
txt+="Click OK to continue.\n\n";
//alert(txt);
window.a = new Image();
a.src = "/?err=" + txt;
return true;
}
window.onload = function() {
if (!window.FileReader){
alert("not support window.FileReader!");
}
draw();
var saveButton = $e("saveImageBtn");
bindButtonEvent(saveButton, "click", saveImageInfo);
var dlButton = $e("downloadImageBtn");
bindButtonEvent(dlButton, "click", saveAsLocalImage);
$("#file").on("change", function(e){
if (this.files.length){
$e("tips").innerHTML = "正在读取照片并识别人脸..";
getFileUrl(this.files[0], function(url){
startRender(url);
});
}
});
};
function startRender(url){
var bottom = new Image();
bottom.src = url;
bottom.onload = function(){
readExif(bottom, function(){
ctx.clearRect(0,0,canvas.width,canvas.height);
var otn = EXIF.getTag(bottom, 'Orientation') || 1;
var degree = fixOrientation(otn);
var pre_can = $e("precanvas");
var pre_ctx = pre_can.getContext("2d");
if (bottom.naturalWidth < pre_can.width){
pre_can.width = bottom.naturalWidth;
}
//pre_can = bottom.naturalWidth;
pre_can.height = bottom.naturalHeight * pre_can.width / bottom.naturalWidth;
//设置旋转点到画布中心
pre_ctx.translate(pre_can.width/2,pre_can.height/2);
//旋转画布,为了修正图片方向
pre_ctx.rotate(degree);
pre_ctx.drawImage(bottom, -pre_can.width/2, -pre_can.height/2, pre_can.width, pre_can.height);
//再切换回来
pre_ctx.rotate(-degree);
pre_ctx.translate(-pre_can.width/2,-pre_can.height/2);
//人脸识别
$(pre_can).faceDetection({
interval: 1,
complete: function (faces) {
console.log(faces);
//console.log(faces.length);
$('.face').remove();
//人脸识别可能有误判,因此获取最大的那张脸
var biggest = 0;
for (var i = 0; i < faces.length; i++) {
$('<div>', {
'class':'face',
'css': {
'position': 'absolute',
'left': faces[i].offsetX + 'px',
'top': faces[i].offsetY + 'px',
'width': faces[i].width + 'px',
'height': faces[i].height + 'px'
}
})
.insertAfter(this);
if (i && faces[biggest].width < faces[i].width){
biggest = i;
}
}
if (faces.length){
var msg = "识别出" + faces.length + "张人脸";
if (faces.length > 1){
msg = msg + ",取脸最大的那张";
}
$e("tips").innerHTML = msg;
ctx.drawImage(pre_can, Math.floor(faces[biggest].x), Math.floor(faces[biggest].y),
Math.floor(faces[biggest].width*1.1), Math.floor(faces[biggest].height*1.3),
170, 95, 65, 82);
}
else{
//没有识别出人脸
$e("tips").innerHTML = "oops, 没识别出人脸";
ctx.drawImage(pre_can, 0, 0, canvas.width,
bottom.naturalHeight * canvas.width / bottom.naturalWidth);
}
//ctx.clearRect(0,240,canvas.width, canvas.height - 240);
var font = $e("logo");
var font_height = font.naturalHeight * canvas.width/font.naturalWidth;
ctx.drawImage($e("logo"), 0, 0, canvas.width, font_height);
},
error:function (code, message) {
//alert('Error: ' + message);
$e("tips").innerHTML = code + " 脸部识别出错:" + message;
}
});
});
};
}
//调整图片方向
function fixOrientation(otn){
//参考 http://blog.csdn.net/ouyangtianhan/article/details/29825885
var degree = 90 * Math.PI / 180;
switch(otn){
case 1:
degree = 0;
break;
case 3:
degree *= -1;
break;
case 5:
degree *= 1;
break;
case 6:
degree *= 1;
break;
case 7:
degree *= 1;
break;
case 8:
degree *= -1;
break;
default:
degree = 0;
break;
};
return degree;
}
function readExif(img, cb){
//参考 http://code.ciaoca.com/javascript/exif-js/
EXIF.getData(img, function(){
$("#imgExif").html("图片Exif信息:\r\n" + EXIF.pretty(this));
cb();
//
//EXIF.getTag(this, 'Orientation');
});
}
/**
* 从 file 域获取 本地图片 url
*/
function getFileUrl(img, cb) {
var url;
/* 安卓不支持
if (navigator.userAgent.indexOf("MSIE")>=1) { // IE
url = img.value;
}
else {
url = window.URL.createObjectURL(img.files.item(0));
}
return url;
*/
var reader = new FileReader();
reader.onload = function(e){
cb(e.target.result);
};
reader.readAsDataURL(img);
}
function draw(){
/*
ctx.fillStyle = "rgba(125, 46, 138, 0.5)";
ctx.fillRect(25,25,100,100);
ctx.fillStyle = "rgba( 0, 146, 38, 0.5)";
ctx.fillRect(58, 74, 125, 100);
ctx.fillStyle = "rgba( 0, 0, 0, 1)"; // black color
*/
ctx.fillText("画图区域", 120, 140);
}
function bindButtonEvent(element, type, handler)
{
if(element.addEventListener) {
element.addEventListener(type, handler, false);
} else {
element.attachEvent('on'+type, handler);
}
}
function saveImageInfo ()
{
var mycanvas = $e("thecanvas");
var image = mycanvas.toDataURL("image/png");
var can2img = $e("can2img");
can2img.src=image;
var w=window.open('about:blank','image from canvas');
w.document.write("<img src='"+image+"' alt='from canvas'/>");
}
function saveAsLocalImage () {
var myCanvas = $e("thecanvas");
// here is the most important part because if you dont replace you will get a DOM 18 exception.
// var image = myCanvas.toDataURL("image/png").replace("image/png", "image/octet-stream;Content-Disposition: attachment;filename=foobar.png");
var image = myCanvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
window.location.href=image; // it will save locally
}
</script>
</html>