| orientation | 描述 |
|---|---|
| 3 | iphone横屏拍摄,此时home键在左侧,图片相对于原始位置旋转了180度 |
| 6 | iphone竖屏拍摄,此时home键在下方(正常拿手机的方向),图片相对于原始位置逆时针旋转可90度 |
| 8 | iphone竖屏拍摄,此时home键在上方,图片相对于原始位置顺时针旋转了90度 |
switch (orientation) {
case 6:
case 8:
cvs.width = height;
cvs.height = width;
break;
}
var context=cvs.getContext("2d");
switch(orientation){
//iphone横屏拍摄,此时home键在左侧
case 3:
// 180度向左旋转
context.translate(width, height);
context.rotate(Math.PI);
break;
//iphone竖屏拍摄,此时home键在下方(正常拿手机的方向)
case 6:
context.rotate(0.5 * Math.PI);
context.translate(0, -height);
break;
//iphone竖屏拍摄,此时home键在上方
case 8:
// 逆时针旋转90度
context.rotate(-0.5 * Math.PI);
context.translate(-width, 0);
break;
}
然后再上传图片,发现在IOS下图片已经正常了。
下面给出完整代码:
$('input[type=file]').change(function(e) {
var file = this.files[0];
var mime_type=file.type;
var orientation=0;
if (file && /^image\//i.test(file.type)) {
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");
//解决ios图片旋转问题
switch(orientation){
//iphone横屏拍摄,此时home键在左侧
case 3:
// 180度向左旋转
context.translate(width, height);
context.rotate(Math.PI);
break;
//iphone竖屏拍摄,此时home键在下方(正常拿手机的方向)
case 6:
context.rotate(0.5 * Math.PI);
context.translate(0, -height);
break;
//iphone竖屏拍摄,此时home键在上方
case 8:
// 逆时针旋转90度
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("只能识别图片!")
}
});
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。