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

摘要:
在使用html5+canvas的移动终端上上传手机照片时,发现iOS手机上传垂直照片并逆时针旋转90度,但水平照片没有这个问题;Android手机没有这个问题。因此,解决这个问题的思路是获得照片的拍摄方向角度,并校正非水平ios照片的角度旋转。使用exif。js来读取照片拍摄信息,请参见http://code.ciaoca.com/JavaScript/exif-js/此处主要使用“方向”属性。Orientation属性描述如下:旋转角度参数0°1 90°顺时针6 90°逆时针8180°3html5页面:[html]viewplaincopyprint?DOCTYPE html˃图像上传“text/javascript”src=“js/uploadPicture/uploadImage.js”˃上传图片:自编js:[javascript]viewplaincopyprint?

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

免责声明:文章转载自《html5+exif.js+canvas实现手机端照片上传预览、压缩、旋转功能(转)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇各主流浏览器内核介绍Timing path下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

iOS 开发之照片框架详解之二 —— PhotoKit 详解(下)

本文链接:http://kayosite.com/ios-development-and-detail-of-photo-framework-part-three.html 这里接着前文《iOS 开发之照片框架详解之二 —— PhotoKit 详解(上)》,主要是干货环节,列举了如何基于 PhotoKit 与 AlAssetLibrary 封装出通用的方法...

ios开发某个页面横不过来屏幕的时候

某一个页面需要横屏,其他的页面任然保持竖屏需要以下关键的几个步骤: 1.修改系统代理方法的返回值 1 -(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window...

Android中RadioGroup的初始化和简单的使用

一简介: RadioGroup作为一个单选按钮组,可以设置为性别选择男或则女,地址选择等等,作为一个android入门级选手,就简单的说一下RadioGroup组中RadioButton的布局和初始化操作,以及禁用整个RadioGroup。 二具体介绍: 布局: <RadioGroup androi...

人脸识别手机端APK分享 | 极速体验人脸识别功能 创建一个简单的人脸识别手机APP程序

1.前言 虹软公司提供免费离线人脸识别,对于开发者提供了比较友好、完整的可配置demo。但是如需直接体验功能,还是要花一点时间去完成项目编译、配置等一系列工作,对于初学者、不怎么熟悉整个项目的人来说可能会踩不少坑。 本文是基于虹软人脸识别SDK V3.0 Android Java的demo,封装后输出的一个简单的的APK程序,直接安装到手机即可体验功能,...

IOS5,6,7不同版的适配. 强制旋转和自动旋转.

改变Orientation的三种途径 这里, 咱们主要理清一下: 到底有哪些设置可以改变屏幕旋转特性. 这样: 出现任何问题我们都可以从这几个途径中发现原因. 灵活应付产品经理的各种需求. 首先我们得知道: 当手机的重力感应打开的时候, 如果用户旋转手机, 系统会抛发UIDeviceOrientationDidChangeNotification ...

监听iOS检测屏幕旋转状态,不需开启屏幕旋转

-(void)rotation_icon:(float)n{ UIButton*history_btn=[self.viewviewWithTag:<#(NSInteger)#>][self.viewviewWithTagName:@"home_history"]; UIButton*cam_btn=[self.viewviewWithT...