• 移动端 Iphone拍照变横问题的解决


    在移动端的页面需要做用户拍照上传的功能时会有用,苹果即使竖着拍照,上传到网页后它也会变成横的,好像IOS得一个BUG,安卓就没有这个问题。

    要解决这个问题需要引入exif.js这个库,网上随便搜一下就有的下载。

    //对图片旋转处理 added by lzk
    function rotateImg(img, direction,canvas) {
        //alert(img);
        //最小与最大旋转方向,图片旋转4次后回到原方向
        var min_step = 0;
        var max_step = 3;
        //var img = document.getElementById(pid);
        if (img == null)return;
        //img的高度和宽度不能在img元素隐藏后获取,否则会出错
        var height = img.height;
        var width = img.width;
        //var step = img.getAttribute('step');
        var step = 2;
        if (step == null) {
            step = min_step;
        }
        if (direction == 'right') {
            step++;
            //旋转到原位置,即超过最大值
            step > max_step && (step = min_step);
        } else {
            step--;
            step < min_step && (step = max_step);
        }
        //旋转角度以弧度值为参数
        var degree = step * 90 * Math.PI / 180;
        var ctx = canvas.getContext('2d');
        switch (step) {
            case 0:
                canvas.width = width;
                canvas.height = height;
                ctx.drawImage(img, 0, 0);
                break;
            case 1:
                canvas.width = height;
                canvas.height = width;
                ctx.rotate(degree);
                ctx.drawImage(img, 0, -height);
                break;
            case 2:
                canvas.width = width;
                canvas.height = height;
                ctx.rotate(degree);
                ctx.drawImage(img, -width, -height);
                break;
            case 3:
                canvas.width = height;
                canvas.height = width;
                ctx.rotate(degree);
                ctx.drawImage(img, -width, 0);
                break;
        }
    }


    /** *旋转iphone拍照变横后的照片 * * @param {*} o 传入当前的input type=file,一般在change事件里传入this * @param {*} callback 回调函数,照片旋转后执行的事件 * @returns */ function rotatePhoto(o,callback){ var file=o.files[0]; var Orientation = null; if (file) { console.log("正在上传,请稍后..."); var rFilter = /^(image/jpeg|image/png)$/i; // 检查图片格式 if (!rFilter.test(file.type)) { //showMyTips("请选择jpeg、png格式的图片", false); return; } // var URL = URL || webkitURL; //获取照片方向角属性,用户旋转控制 EXIF.getData(file, function() { // alert(EXIF.pretty(this)); EXIF.getAllTags(this); //alert(EXIF.getTag(this, 'Orientation')); Orientation = EXIF.getTag(this, 'Orientation'); //return; }); var oReader = new FileReader(); oReader.onload = function(e) { //var blob = URL.createObjectURL(file); //_compress(blob, file, basePath); var image = new Image(); image.src = e.target.result; image.onload = function() { var expectWidth = this.naturalWidth; var expectHeight = this.naturalHeight; if (this.naturalWidth > this.naturalHeight && this.naturalWidth > 800) { expectWidth = 800; expectHeight = expectWidth * this.naturalHeight / this.naturalWidth; } else if (this.naturalHeight > this.naturalWidth && this.naturalHeight > 1200) { expectHeight = 1200; expectWidth = expectHeight * this.naturalWidth / this.naturalHeight; } var canvas = document.createElement("canvas"); var ctx = canvas.getContext("2d"); canvas.width = expectWidth; canvas.height = expectHeight; ctx.drawImage(this, 0, 0, expectWidth, expectHeight); var u = navigator.userAgent; //修复ios if (u.match(/iphone/i)) { console.log('iphone'); //如果方向角不为1,都需要进行旋转 added by lzk if(Orientation != "" && Orientation != 1){ console.log('旋转处理'); switch(Orientation){ case 6://需要顺时针(向左)90度旋转 console.log('需要顺时针(向左)90度旋转'); rotateImg(this,'left',canvas); break; case 8://需要逆时针(向右)90度旋转 console.log('需要逆时针(向右)90度旋转'); rotateImg(this,'right',canvas); break; case 3://需要180度旋转 console.log('需要180度旋转'); rotateImg(this,'right',canvas);//转两次 rotateImg(this,'right',canvas); break; } } //base64 在外定义为全局变量 //下面base64为得到旋转后的base64图片 base64 = canvas.toDataURL("image/jpeg", 0.8); var type = 'jpeg'; var fixtype = function (type) { type = type.toLocaleLowerCase().replace(/jpg/i, 'jpeg'); var r = type.match(/png|jpeg|bmp|gif/)[0]; return 'image/' + r; }; base64 = base64.replace(fixtype(type), 'image/jpeg'); // saveFile(base64, '111') 此处是如果想要保存当前图片到本地的话; //这里是把已经旋转过的图片路径赋值到img中 callback(base64) } else if (u.indexOf('Android') > -1 || u.indexOf('Adr') > -1) { //如果安卓收到ios拍摄的照片,可以按PC端方式判断 callback(e.target.result) } else{ //修复PC端上上传ios拍出来的图片 if(Orientation != "" && Orientation != 1){ //alert('旋转处理'); switch(Orientation){ case 6://需要顺时针(向左)90度旋转 console.log('需要顺时针(向左)90度旋转'); rotateImg(this,'left',canvas); break; case 8://需要逆时针(向右)90度旋转 console.log('需要逆时针(向右)90度旋转'); rotateImg(this,'right',canvas); break; case 3://需要180度旋转 console.log('需要180度旋转'); rotateImg(this,'right',canvas);//转两次 rotateImg(this,'right',canvas); break; } } base64 = canvas.toDataURL("image/jpeg", 0.8); var type = 'jpeg'; var fixtype = function (type) { type = type.toLocaleLowerCase().replace(/jpg/i, 'jpeg'); var r = type.match(/png|jpeg|bmp|gif/)[0]; return 'image/' + r; }; base64 = base64.replace(fixtype(type), 'image/jpeg'); callback(base64) } }; }; oReader.readAsDataURL(file); } }

    上面两个是基础函数,引入exif.js后,直接复制放进script标签里就行,下面的是调用方法:

    document.getElementById('upload').addEventListener('change',function(){ //这里是你的上传按钮
    
    //这里定义一个函数,用来处理你原本上传完照片后的事情
    var callback = function(src){    //这个src就是旋转后图片的base64地址
    
       //这里是上传旋转图片后,再插入文档进行预览,这里的事件根据实际需求编写
        var img = new Image()
         img.src = src
        img.onload = function(){
        $('.box).append(img)
    }
    }
    
    
    //这里就是旋转照片
    rotatePhoto(this,callback)
    })
  • 相关阅读:
    Codeforces704C. Black Widow
    Codeforces704B. Ant Man
    暑假学习日记2013/8/5
    暑假学习日记2013/7/31
    暑假学习日记2013/7/26
    暑假学习日记2013/7/25
    暑假学习日记2013/7/24
    暑假学习日记2013/7/23
    线性同余方程的解法笔记
    暑假学习日记2013/7/22
  • 原文地址:https://www.cnblogs.com/haqiao/p/9386212.html
Copyright © 2020-2023  润新知