国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > iOS > 正文

IOS中html5上傳圖片方向問(wèn)題解決方法

2020-07-26 03:28:58
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

用html5編寫圖片裁切上傳,在iphone手機(jī)上可能會(huì)遇到圖片方向錯(cuò)誤問(wèn)題,在此把解決方法和大家分享一下,
用到了html5的 FileReader和Canvas,如果還沒(méi)有接觸的同學(xué),先了解一下其方法。

 //此方法為file input元素的change事件 function change(){  var file = this.files[0];  var orientation;  //EXIF js 可以讀取圖片的元信息 https://github.com/exif-js/exif-js  EXIF.getData(file,function(){    orientation=EXIF.getTag(this,'Orientation');  });  var reader = new FileReader();  reader.onload = function(e) {      getImgData(this.result,orientation,function(data){      //這里可以使用校正后的圖片data了     });   }  reader.readAsDataURL(file);}
// @param {string} img 圖片的base64// @param {int} dir exif獲取的方向信息// @param {function} next 回調(diào)方法,返回校正方向后的base64function getImgData(img,dir,next){ var image=new Image(); image.onload=function(){  var degree=0,drawWidth,drawHeight,width,height;  drawWidth=this.naturalWidth;  drawHeight=this.naturalHeight;  //以下改變一下圖片大小  var maxSide = Math.max(drawWidth, drawHeight);  if (maxSide > 1024) {    var minSide = Math.min(drawWidth, drawHeight);    minSide = minSide / maxSide * 1024;    maxSide = 1024;    if (drawWidth > drawHeight) {      drawWidth = maxSide;      drawHeight = minSide;    } else {      drawWidth = minSide;      drawHeight = maxSide;    }  }  var canvas=document.createElement('canvas');  canvas.width=width=drawWidth;  canvas.height=height=drawHeight;   var context=canvas.getContext('2d');  //判斷圖片方向,重置canvas大小,確定旋轉(zhuǎn)角度,iphone默認(rèn)的是home鍵在右方的橫屏拍攝方式  switch(dir){    //iphone橫屏拍攝,此時(shí)home鍵在左側(cè)    case 3:      degree=180;      drawWidth=-width;      drawHeight=-height;      break;    //iphone豎屏拍攝,此時(shí)home鍵在下方(正常拿手機(jī)的方向)    case 6:      canvas.width=height;      canvas.height=width;       degree=90;      drawWidth=width;      drawHeight=-height;      break;    //iphone豎屏拍攝,此時(shí)home鍵在上方    case 8:      canvas.width=height;      canvas.height=width;       degree=270;      drawWidth=-width;      drawHeight=height;      break;  }  //使用canvas旋轉(zhuǎn)校正  context.rotate(degree*Math.PI/180);  context.drawImage(this,0,0,drawWidth,drawHeight);  //返回校正圖片  next(canvas.toDataURL("image/jpeg",.8)); } image.src=img;}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 阿克苏市| 石泉县| 邻水| 和林格尔县| 米脂县| 泗洪县| 房山区| 罗定市| 南部县| 阿瓦提县| 手机| 关岭| 吴堡县| 婺源县| 恩平市| 鹿泉市| 苏尼特左旗| 云梦县| 新沂市| 库尔勒市| 囊谦县| 宁津县| 外汇| 昭平县| 德惠市| 宁阳县| 旺苍县| 安国市| 安吉县| 社旗县| 岑巩县| 高尔夫| 循化| 惠州市| 天柱县| 大埔县| 博爱县| 克什克腾旗| 江孜县| 南靖县| 敦煌市|