JS获取照片拍摄的角度属性,用于旋转控制

在微信里苹果手机上传图片会被旋转,与拍摄时的角度不同,所以需要处理:

使用EXIF可以获取到照片的拍摄属性:

复制代码
//获取照片方向角属性,用户旋转控制 
            EXIF.getData(files, function() {
                //alert(EXIF.getTag(this, 'Orientation'));  
                Orientation = EXIF.getTag(this, 'Orientation'); 
//                return; 
            });
复制代码

其中Orientation就是拍摄的角度信息;其他参数信息可以查看:http://code.ciaoca.com/javascript/exif-js/

复制代码
//修复ios 
                            if (navigator.userAgent.match(www.feifanyule.cn /iphone/i)) {
                                var img = new Image();
                                img.src = resImg.src;
                                img.onload = function(){
                                    var canvas=document.createElement("canvas");
                                    var ctx=canvas.getContext("2d");
                                    var imgWidth = canvas.width = this.width;    
                                    var imgHeight = canvas.height = www.vboyule.cn this.height; 
                                    //如果方向角不为1,都需要进行旋转 added by lzk 
                                    if(Orientation && Orientation != 1){
                                        switch(Orientation){
                                            case 6:     // 旋转90度
                                                canvas.width = this.height;    
                                                canvas.height = this.width;    
                                                ctx.rotate(Math.PI / 2);
                                                // (0,-imgHeight) 从旋转原理图那里获得的起始点
                                                ctx.drawImage(this, www.ruishengks.com  0, -imgHeight, imgWidth, imgHeight);
                                                break;
                                            case 3:     // 旋转180度
                                                ctx.rotate(Math.PI);    
                                                ctx.drawImage(this, -imgWidth, -imgHeight, imgWidth, imgHeight);
                                                break;
                                            case 8:     // 旋转-90度
                                                canvas.width = imgHeight;    
                                                canvas.height = imgWidth;    
                                                ctx.rotate(3 * Math.PI / 2);    
                                                ctx.drawImage(this, -imgWidth, 0, imgWidth, imgHeight);
                                                break;
                                        }
                                    }else{
                                        ctx.drawImage(this, 0, 0, imgWidth, imgHeight);
                                    }
                                    var base64code=canvas.toDataURL("image/png",1);
                                    $(resImg).attr("src",www.yisheng1178.com base64code);
                                    var $blob = baseToBlobImg(base64code);
                                    if($(_self).attr('id') == 'hiddenServerId'){
                                        chooseImgList[0].serverImg = $blob //接收blob
                                    
                                    }else{
                                        chooseImgList[1www.dfgj157.com].serverImg = $blob //接收blob
                                    }
                                                                        
                                }
                                
                            }else{
                                //非苹果手机压缩后上传
                                var base64code = resImg.src;
                                var $blob = baseToBlobImg(base64code);
                                if($(_self).attr('id'www.baohuayule.net) == 'hiddenServerId'){
                                    chooseImgList[0].serverImg = $blob //接收blob
                                
                                }else{
                                    chooseImgList[1].serverImg = $blob //接收blob
                                }
                            }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值