$(
'input[type=file]'
).change(
function
(e) {
var
file =
this
.files[0];
var
mime_type=file.type;
var
orientation=0;
if
(file && /^image\
EXIF.getData(file,
function
(){
orientation=EXIF.getTag(file,
'Orientation'
);
});
var
reader =
new
FileReader();
reader.onloadend =
function
() {
var
width,height;
var
MAX_WH=800;
var
image=
new
Image();
image.onload=
function
() {
if
(image.height > MAX_WH) {
image.width *= MAX_WH / image.height;
image.height = MAX_WH;
}
if
(image.width > MAX_WH) {
image.height *= MAX_WH / image.width;
image.width = MAX_WH;
}
var
quality=80;
var
cvs = document.createElement(
'canvas'
);
cvs.width = width = image.width;
cvs.height =height = image.height;
switch
(orientation) {
case
6:
case
8:
cvs.width = height;
cvs.height = width;
break
;
}
var
context=cvs.getContext(
"2d"
);
switch
(orientation){
case
3:
context.translate(width, height);
context.rotate(Math.PI);
break
;
case
6:
context.rotate(0.5 * Math.PI);
context.translate(0, -height);
break
;
case
8:
context.rotate(-0.5 * Math.PI);
context.translate(-width, 0);
break
;
}
context.drawImage(image, 0, 0,image.width, image.height);
dataURL = cvs.toDataURL(
'image/jpeg'
, quality/100);
...
}
image.src=dataURL;
};
reader.readAsDataURL(file);
}
else
{
alert(
"只能识别图片!"
)
}
});