首页 > Web开发 > 详细

拍照上传图片方向调整

时间:2020-06-29 20:26:06      阅读:86      评论:0      收藏:0      [点我收藏+]
解决某些手机系统正方向竖着拍方向错乱的问题。

npm install exif-js --save
import EXIF from "exif-js";
      let reader = new FileReader();
      reader.readAsDataURL(file);
      if (file) {
        EXIF.getData(file, function() {
          let ori = EXIF.getTag(this, "Orientation");
          // alert(ori, "手持方向");
          reader.onload = ev => {
            let image = new Image();
            image.src = ev.target.result;
            image.onload = () => {
              let canvas = document.createElement("canvas"),
                ctx = canvas.getContext("2d");
              if (ori == 6 && image.width > image.height) {
                //ios正方向竖着排方向错乱时,旋转90度
                canvas.width = image.height;
                canvas.height = image.width;
                ctx.rotate(Math.PI / 2);
                ctx.drawImage(image, 0, -image.height, image.width, image.height);
              } else {
                canvas.width = image.width / 2;
                canvas.height = image.height / 2;
                ctx.drawImage(image, 0, 0, image.width / 2, image.height / 2);
              }
              resolve(canvas.toDataURL("image/jpeg"));
            };
          };
        });
      }

 

拍照上传图片方向调整

原文:https://www.cnblogs.com/kongwei/p/13209821.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!