exif.js解决ios手机上传照片后显示为旋转90度问题(兼容ios13.4之前的版本 )

exif.js解决ios手机上传照片后显示为旋转90度问题(兼容ios13.4 )

问题描述:

在做手机移动端app时,发现iOS12.5.1版本(iphone6)上传照片出现顺时针旋转90问题,iphone11倒是正常,最后发现

iOS 13.4 更新后带来的问题

然而我发现大家好像都默认浏览器不会对带 EXIF 信息的图片进行回正,当然之前确实不会。但是自从 iOS 更新到 13.4.1 后,浏览器支持自动回正了。


解决方案:

利用exif.js读取照片的拍摄信息

安装exif.js

1.npm install exif-js --save
2.在对应的vue模块引入:import EXIF from “exif-js”;

在这里插入图片描述

exif.js中 Orientation 属性说明如下:

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

项目中使用:

vue文件中

<script>
import EXIF from "exif-js";
export default {
	methods: {
	    afterRead(file) {
	      var self = this;
	      let Orientation = null;
	      //去获取拍照时的信息,解决拍出来的照片旋转问题 file.file为文件对象
	      EXIF.getData(file.file, function () { 
	        Orientation = EXIF.getTag(this, "Orientation");
	      });
	      var img = new Image(),
	        width = 512, //image resize   压缩后的宽
	        quality = 0.9, //image quality  压缩质量
	        canvas = document.createElement("canvas"),
	        drawer = canvas.getContext("2d");
	      img.src = file.content;
	      img.onload = function () {
	        //利用canvas压缩图片
	        canvas.width = width;
	        canvas.height = width * (img.height / img.width);
	        /**
	         * 自从 iOS 更新到 13.4.1 后,浏览器支持自动回正了
	         * 获取ios 的系统版本号,在13.4版本之前需要旋转,之后就不需要了
	         */
	        var str= navigator.userAgent.toLowerCase(); 
	        var ver=str.match(/cpu iphone os (.*?) like mac os/);
	        // console.log("你当前的Ios系统版本为:"+ver[1].replace(/_/g,"."))
	        console.log(ver[1].replace(/_/g,".")<'13.4')
	        if ((ver[1].replace(/_/g,".")<'13.4') && Orientation && Orientation != 1) {
	          switch (Orientation) {
	            case 6: // 旋转90度
	            console.log(Orientation)
	              drawer.rotate(Math.PI / 2);
	              drawer.drawImage(img, 0, -canvas.height, canvas.width, canvas.height);
	              break;
	            case 3: // 旋转180度
	              drawer.rotate(Math.PI);
	              drawer.drawImage(img, -canvas.width, -canvas.height, canvas.width, canvas.height);
	              break;
	            case 8: // 旋转-90度
	              drawer.rotate((3 * Math.PI) / 2);
	              drawer.drawImage(img, -canvas.width, 0, canvas.width, canvas.height);
	              break;
	          }
	        } else {
	          drawer.drawImage(img, 0, 0, canvas.width, canvas.height);
	        }
	
	        var base64 = canvas.toDataURL("image/*", quality);
	        var pic = base64.split(",")[1]; //图片的base64编码内容
	
      };
      // 此时可以自行将文件上传至服务器
    },
}
// 兼容性处理方法来源于:vue-cropper@0.5.5   仅供参考

checkOrientationImage(img, orientation, width, height) {
      // 如果是 chrome内核版本在81 safari 在 605 以上不处理图片旋转
      // alert(navigator.userAgent)
      if (this.getVersion("chrome")[0] >= 81) {
        orientation = -1;
      } else {
        if (this.getVersion("safari")[0] >= 605) {
          const safariVersion = this.getVersion("version");
          if (safariVersion[0] > 13 && safariVersion[1] > 1) {
            orientation = -1;
          }
        } else {
          //  判断 ios 版本进行处理
          // 针对 ios 版本大于 13.4的系统不做图片旋转
          const isIos = navigator.userAgent
            .toLowerCase()
            .match(/cpu iphone os (.*?) like mac os/);
          if (isIos) {
            let version = isIos[1];
            version = version.split("_");
            if (version[0] > 13 || (version[0] >= 13 && version[1] >= 4)) {
              orientation = -1;
            }
          }
        }
      }

      // alert(`当前处理的orientation${orientation}`)
      let canvas = document.createElement("canvas");
      let ctx = canvas.getContext("2d");
      ctx.save();

      switch (orientation) {
        case 2:
          canvas.width = width;
          canvas.height = height;
          // horizontal flip
          ctx.translate(width, 0);
          ctx.scale(-1, 1);
          break;
        case 3:
          canvas.width = width;
          canvas.height = height;
          //180 graus
          ctx.translate(width / 2, height / 2);
          ctx.rotate((180 * Math.PI) / 180);
          ctx.translate(-width / 2, -height / 2);
          break;
        case 4:
          canvas.width = width;
          canvas.height = height;
          // vertical flip
          ctx.translate(0, height);
          ctx.scale(1, -1);
          break;
        case 5:
          // vertical flip + 90 rotate right
          canvas.height = width;
          canvas.width = height;
          ctx.rotate(0.5 * Math.PI);
          ctx.scale(1, -1);
          break;
        case 6:
          canvas.width = height;
          canvas.height = width;
          //90 graus
          ctx.translate(height / 2, width / 2);
          ctx.rotate((90 * Math.PI) / 180);
          ctx.translate(-width / 2, -height / 2);
          break;
        case 7:
          // horizontal flip + 90 rotate right
          canvas.height = width;
          canvas.width = height;
          ctx.rotate(0.5 * Math.PI);
          ctx.translate(width, -height);
          ctx.scale(-1, 1);
          break;
        case 8:
          canvas.height = width;
          canvas.width = height;
          //-90 graus
          ctx.translate(height / 2, width / 2);
          ctx.rotate((-90 * Math.PI) / 180);
          ctx.translate(-width / 2, -height / 2);
          break;
        default:
          canvas.width = width;
          canvas.height = height;
      }

      ctx.drawImage(img, 0, 0, width, height);
      ctx.restore();
      canvas.toBlob(
        blob => {
          let data = URL.createObjectURL(blob);
          URL.revokeObjectURL(this.imgs);
          this.imgs = data;
        },
        "image/" + this.outputType,
        1
      );
    },

转载于:https://blog.csdn.net/weixin_50535566/article/details/114927099

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个问题的原因是由于不同的设备在拍照后保存图片的方向不同,导致在显示图片时出现了旋转的情况。为了解决这个问题,可以通过以下方法进行处理: 1. 使用 ExifInterface 类 可以通过 ExifInterface 类读取图片的旋转信息,然后将图片进行相应的旋转。具体的代码如下: ```java ExifInterface exif = new ExifInterface(photoPath); int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); Matrix matrix = new Matrix(); if (orientation == ExifInterface.ORIENTATION_ROTATE_90) { matrix.postRotate(90); } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) { matrix.postRotate(180); } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) { matrix.postRotate(270); } Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); ``` 2. 使用系统相机 在调用系统相机拍照时,可以通过设置 Camera.Parameters 属性来控制图片的旋转。具体的代码如下: ```java Camera.Parameters parameters = mCamera.getParameters(); Camera.CameraInfo info = new Camera.CameraInfo(); Camera.getCameraInfo(cameraId, info); int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); int degrees = 0; switch (rotation) { case Surface.ROTATION_0: degrees = 0; break; case Surface.ROTATION_90: degrees = 90; break; case Surface.ROTATION_180: degrees = 180; break; case Surface.ROTATION_270: degrees = 270; break; } int result; if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { result = (info.orientation + degrees) % 360; result = (360 - result) % 360; } else { result = (info.orientation - degrees + 360) % 360; } mCamera.setDisplayOrientation(result); parameters.setRotation(result); ``` 以上两种方法都可以解决Android部分手机拍照后获取的图片被旋转问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值