html5+exif.js+canvas實現手機端照片上傳預覽、壓縮、旋轉功能(轉)

html5+canvas進行移動端手機照片上傳時,發現iOS手機上傳豎拍照片會逆時針旋轉90度,橫拍照片無此問題;Android手機沒這個問題。javascript

所以解決這個問題的思路是:獲取到照片拍攝的方向角,對非橫拍的ios照片進行角度旋轉修正。html

利用exif.js讀取照片的拍攝信息,詳見  http://code.ciaoca.com/JavaScript/exif-js/html5

這裏主要用到Orientation屬性。java

Orientation屬性說明以下:jquery

 

旋轉角度 參數
1
順時針90° 6
逆時針90° 8
180° 3

 

html5頁面:ios

 

[html]  view plain  copy
 
 print?
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <meta charset="utf-8">  
  5.     <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />  
  6.     <title>圖片上傳</title>  
  7.     <script type="text/javascript" src="js/jquery-1.8.3.js"></script>  
  8.     <script type="text/javascript" src="js/uploadPicture/mobileBUGFix.mini.js" ></script>  
  9.     <script type="text/javascript" src="js/uploadPicture/uploadImage.js" ></script>  
  10.         <script type="text/javascript" src="js/exif.js" ></script>  
  11. </head>  
  12. <body>  
  13.     <div style="height: 50px; line-height: 50px;text-align: center;border-bottom: 1px solid #171E28;">  
  14.             上傳圖片:  
  15.             <input type="file" accept="image/*" id="uploadImage" capture="camera" onchange="selectFileImage(this);" />  
  16.         </div>  
  17.         <div style="margin-top: 10px;">  
  18.             <img alt="preview" src="" id="myImage"/>  
  19.         </div>  
  20. </body>  
  21. </html>  


本身寫的js:web

 

 

[javascript]  view plain  copy
 
 print?
  1. function selectFileImage(fileObj) {  
  2.     var file = fileObj.files['0'];  
  3.     //圖片方向角 added by lzk  
  4.     var Orientation = null;  
  5.       
  6.     if (file) {  
  7.         console.log("正在上傳,請稍後...");  
  8.         var rFilter = /^(image\/jpeg|image\/png)$/i; // 檢查圖片格式  
  9.         if (!rFilter.test(file.type)) {  
  10.             //showMyTips("請選擇jpeg、png格式的圖片", false);  
  11.             return;  
  12.         }  
  13.         // var URL = URL || webkitURL;  
  14.         //獲取照片方向角屬性,用戶旋轉控制  
  15.         EXIF.getData(file, function() {  
  16.            // alert(EXIF.pretty(this));  
  17.             EXIF.getAllTags(this);   
  18.             //alert(EXIF.getTag(this, 'Orientation'));   
  19.             Orientation = EXIF.getTag(this, 'Orientation');  
  20.             //return;  
  21.         });  
  22.           
  23.         var oReader = new FileReader();  
  24.         oReader.onload = function(e) {  
  25.             //var blob = URL.createObjectURL(file);  
  26.             //_compress(blob, file, basePath);  
  27.             var image = new Image();  
  28.             image.src = e.target.result;  
  29.             image.onload = function() {  
  30.                 var expectWidth = this.naturalWidth;  
  31.                 var expectHeight = this.naturalHeight;  
  32.                   
  33.                 if (this.naturalWidth > this.naturalHeight && this.naturalWidth > 800) {  
  34.                     expectWidth = 800;  
  35.                     expectHeight = expectWidth * this.naturalHeight / this.naturalWidth;  
  36.                 } else if (this.naturalHeight > this.naturalWidth && this.naturalHeight > 1200) {  
  37.                     expectHeight = 1200;  
  38.                     expectWidth = expectHeight * this.naturalWidth / this.naturalHeight;  
  39.                 }  
  40.                 alert(expectWidth+','+expectHeight);  
  41.                 var canvas = document.createElement("canvas");  
  42.                 var ctx = canvas.getContext("2d");  
  43.                 canvas.width = expectWidth;  
  44.                 canvas.height = expectHeight;  
  45.                 ctx.drawImage(this, 0, 0, expectWidth, expectHeight);  
  46.                 alert(canvas.width+','+canvas.height);  
  47.                   
  48.                 var base64 = null;  
  49.                 var mpImg = new MegaPixImage(image);  
  50.                     mpImg.render(canvas, {  
  51.                         maxWidth: 800,  
  52.                         maxHeight: 1200,  
  53.                         quality: 0.8,  
  54.                         orientation: Orientation  
  55.                     });  
  56.                       
  57.                 base64 = canvas.toDataURL("image/jpeg", 0.8);  
  58.                   
  59.                 //uploadImage(base64);  
  60.                 $("#myImage").attr("src", base64);  
  61.             };  
  62.         };  
  63.         oReader.readAsDataURL(file);  
  64.     }  
  65. }  

用到的第三方js文件:mobileBUGFix.mini.jscanvas

 


測試demo下載地址:測試

http://download.csdn.net/detail/linlzk/9127441this

相關文章
相關標籤/搜索