国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 伊金霍洛旗| 富裕县| 应用必备| 赞皇县| 抚远县| 吉木萨尔县| 阿瓦提县| 大港区| 绍兴市| 白银市| 祁阳县| 留坝县| 古交市| 莱西市| 柏乡县| 临洮县| 久治县| 玉树县| 霍邱县| 固安县| 隆化县| 三穗县| 丽水市| 鹿泉市| 大方县| 日照市| 富宁县| 秀山| 东城区| 河间市| 五台县| 新和县| 武安市| 尼勒克县| 阜南县| 霞浦县| 洛川县| 许昌县| 左权县| 兴安盟| 高阳县|