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
                                }
                            }

你可能感兴趣的:(JS获取照片拍摄的角度属性,用于旋转控制)