| 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;
}
然后再上傳圖片,發(fā)現在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("只能識別圖片!")
}
});
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。