html5+exif.js+canvas实现手机端照片上传预览、压缩、旋转功能

HTML5+canvas进行移动端手机照片上传时,发现iOS手机上传竖拍照片会逆时针旋转90度,横拍照片无此问题;Android手机没这个问题。

因此解决这个问题的思路是:获取到照片拍摄的方向角,对非横拍的ios照片进行角度旋转修正。

利用exif.js读取照片的拍摄信息,详见 http://code.ciaoca.com/JavaScript/exif-js/

这里主要用到Orientation属性。

Orientation属性说明如下:

旋转角度

参数

1

顺时针90°

6

逆时针90°

8

180°

3

 

Html5页面:

[html] viewplain 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" οnchange="selectFileImage(this);" />  
  16.         </div>  
  17.         <div style="margin-top: 10px;">  
  18.             <img alt="preview" src="" id="myImage"/>  
  19.         </div>  
  20. </body>  
  21. </html>  


自己写的js:

[javascript]view plaincopy

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.js


测试demo下载地址:

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


 参考自:http://blog.csdn.net/linlzk/article/details/48654835

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值