获取照片方向角属性,用于旋转控制

时间:2024-01-22 18:45:55

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

使用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(/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 = 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, 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",base64code);
                                    var $blob = baseToBlobImg(base64code);
                                    if($(_self).attr('id') == 'hiddenServerId'){
                                        chooseImgList[0].serverImg = $blob //接收blob
                                    
                                    }else{
                                        chooseImgList[1].serverImg = $blob //接收blob
                                    }
                                                                        
                                }
                                
                            }else{
                                //非苹果手机压缩后上传
                                var base64code = resImg.src;
                                var $blob = baseToBlobImg(base64code);
                                if($(_self).attr('id') == 'hiddenServerId'){
                                    chooseImgList[0].serverImg = $blob //接收blob
                                
                                }else{
                                    chooseImgList[1].serverImg = $blob //接收blob
                                }
                            }

可借鉴CSDN里林志Ke的帖子:利用exif.js解决ios手机上传竖拍照片旋转90度问题