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


    在微信里ios手机上传竖拍照片会自动旋转90度,与拍摄时的角度不同,所以需要处理:

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

    API 方法

    名称说明
    EXIF.getData(img, callback)

    获取图像的数据

    能兼容尚未支持提供 EXIF 数据的浏览器获取到元数据。

    EXIF.getTag(img, tag) 获取图像的某个数据
    EXIF.getAllTags(img) 获取图像的全部数据,值以对象的方式返回
    EXIF.pretty(img) 获取图像的全部数据,值以字符串的方式返回
    //获取照片方向角属性,用户旋转控制 
                EXIF.getData(files, function() {
                    //alert(EXIF.getTag(this, 'Orientation'));  
                    Orientation = EXIF.getTag(this, 'Orientation'); 
    //                return; 
                });

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

    注意:这里我的files是获取的文件格式的图片,files[0]获取的。

    Orientation属性说明如下:

    旋转角度 参数
    1
    顺时针90° 6
    逆时针90° 8
    180° 3

    2、判断照片需要旋转多少角度并使用canvas对其旋转处理:

    //修复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
                                    }
                                }

    3、将处理后的图片最后转换成bold类型文件上传:

    /*将base64的图片转换为blod格式上传*/
        function baseToBlobImg(base64code){
            var arr = base64code.split(','), mime = arr[0].match(/:(.*?);/)[1],
                bstr = atob(arr[1]),
                n = bstr.length,
                u8arr = new Uint8Array(n);
            while(n--) {
                u8arr[n] = bstr.charCodeAt(n);
            }
            
            return obj = new Blob([u8arr], {type:mime});
        }  

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

  • 相关阅读:
    MyEclipse或者Eclipse内存溢出问题
    关于分布式事务、两阶段提交协议、三阶提交协议
    Linux查看CPU和内存使用情况(转)
    Linux中设定umask的作用
    Linux解决乱码问题
    Vi编辑器的使用
    Eclipse的SVN插件下载
    Netty 入门初体验
    聊聊spring的那些扩展机制
    FreeMarker入门
  • 原文地址:https://www.cnblogs.com/amor17/p/9013169.html
Copyright © 2020-2023  润新知