国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 连平县| 巧家县| 普定县| 龙井市| 四子王旗| 湖口县| 明光市| 大英县| 黑水县| 阜康市| 南通市| 电白县| 延津县| 靖安县| 迭部县| 荔浦县| 宽城| 嘉黎县| 融水| 伽师县| 夏邑县| 崇州市| 金华市| 蚌埠市| 炎陵县| 高州市| 任丘市| 云梦县| 沁阳市| 舞阳县| 惠州市| 东光县| 镇康县| 阿城市| 诏安县| 伊吾县| 乌苏市| 阿瓦提县| 定安县| 南昌市| 三亚市|