zoukankan      html  css  js  c++  java
  • 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] 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:

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


    测试demo下载地址:

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

  • 相关阅读:
    各种读取速度
    索引倒排
    清空mysql数据
    java随机读取文件
    移动文件
    输出字符串数组
    背包问题
    使用bloomfilter
    使用hash拆分文件
    判断文件的编码格式
  • 原文地址:https://www.cnblogs.com/ginikeer/p/6295517.html
Copyright © 2011-2022 走看看