vue项目解决原生input标签上传图片安卓机图片翻转问题

vue项目解决原生input标签上传图片安卓机图片翻转问题

最近在做一个移动端项目,其中有上传九张图片的一个模块,然后发现在安卓手机上上传图片或者拍照会出现图片翻转的问题.苹果手机正常.

解决原理:利用exif-js来实现
链接: [link]http://code.ciaoca.com/javascript/exif-js/.
这个链接中有demo示例可以尝试
大概原理通过引入这个js获取上传图片的角度信息,然后根据角度值在通过canvas来实现翻转图片以达到图片角度正常

代码片

 上传
 <input type="file" multiple="multiple" accept="image/*" placeholder="相册" @change="onRead($event)">
 <input type="file" multiple="multiple" accept="image/*" capture="camera" placeholder="拍照" @change="onRead($event)">
 预览
<div v-show="fileList.length<9" v-for="(item,index) in fileList">
     <img :src="item">
     <i @click="delImg(index)">X</i>
</div>
methods:{
    delImg: function delImg(index) {
        this.fileList.splice(index, 1);
    },
     //相册拍照功能 fileList九张图片
     onRead(e, source) {
         var fileList = e.target.files;
         if (this.fileList.length < 9) {
             for (var i = 0; i < fileList.length; i++) {
              if (i + this.fileList.length > 8) {
                  //Toast('不允许超过九张张片'); 
              }
              var file = fileList[i];
              this.imgReverse(file, e); 
             }
         }
     },
     //处理图片翻转 仅处理安卓机 安卓机一般Orientation == 6
     imgReverse(file, e) {
         var that = this
         EXIF.getData(file, ()=> {
             var Orientation = EXIF.getTag(file, "Orientation");
             var reader = new FileReader();
             reader.readAsDataURL(file);
             var imgUrl = null;
             reader.onload = function (e) {
                 var image = new Image(),
                     canvas = document.createElement("canvas"),
                     ctx = canvas.getContext("2d");
                 image.src = e.target.result;
                 imgUrl = e.target.result;
                 image.onload = function () {
                     var w = image.naturalWidth,
                         h = image.naturalHeight;
                     if (w > 1000) {
                     h = h * 1000 / w;
                     w = 1000;
                     } 
                     // 6、顺时针90   8、逆时针90   3、180度
                     if (Orientation == 6) {
                         //IOS不旋转 安卓旋转
                         if (that.isiOS()) {
                             canvas.width = w;
                             canvas.height = h;
                             ctx.drawImage(image, 0, 0, image.naturalWidth, image.naturalHeight, 0, 0, w, h);
                         } else {
                             canvas.width = h;
                             canvas.height = w;
                             ctx.rotate(Math.PI / 2);
                             ctx.drawImage(image, 0, 0, image.naturalWidth, image.naturalHeight, 0, -h, w, h);
                         }
                     } else if (Orientation == 3) {
                         canvas.width = w;
                         canvas.height = h;
                         ctx.rotate(Math.PI);
                         ctx.drawImage(image, 0, 0, image.naturalWidth, image.naturalHeight, -w, -h, w, h);
                     } else if (Orientation == 8) {
                         canvas.width = h;
                         canvas.height = w;
                         ctx.rotate(3 * Math.PI / 2);
                         ctx.drawImage(image, 0, 0, image.naturalWidth, image.naturalHeight, -w, 0, w, h);
                     } else {
                         canvas.width = w;
                         canvas.height = h;
                         ctx.drawImage(image, 0, 0, image.naturalWidth, image.naturalHeight, 0, 0, w, h);
                     }
                     var data = canvas.toDataURL(file.type, 1);
                     that.fileList.push(data);//fileList 显示的图片
                 };
             };
             reader.onerror = function (e) {
                 console.log("读取出错");
             };
             reader.onabort = function (e) {
                 console.log("读取中断");
             };
             reader.onloadend = function (e) {
                 console.log("读取结束");
             };
         });
     },
     // 判断是都为ios机型
     isiOS() {
         var isiOS;
         var u = navigator.userAgent;
         isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
         return isiOS;
     },
     //把图片从file转为base64 有需要加  this.toBase64(e.srcElement.result,e.type);
     toBase64(base64, type) {
         var localData = base64.replace(/\r|\n/g, "").replace("data:image/jpg", "data:image/jpeg").replace("data:image/png", "data:image/jpeg");
         var wxdata = localData.replace("data:image/jpeg;base64,", "").replace("data:image/jpg;base64,", "");
         var it = {
             type: type,
             base64: wxdata
         };
         console.log(it);
     },
     // 将base64 图片转化为file格式 有需要加  this.base64ToFile(data)
     base64ToFile(data) {
         var bytes = atob(data.base64); //var bytes = base64;
         var bytesCode = new ArrayBuffer(bytes.length); // 转换为类型化数组
         var byteArray = new Uint8Array(bytesCode); // 将base64转换为ascii码
         for (var i = 0; i < bytes.length; i++) {
             byteArray[i] = bytes.charCodeAt(i);
         } // 生成Blob对象(文件对象)
         var name = data.name ? data.name : "default.png";
         var file = new File([bytesCode], name, {
             type: "image/png"
         });
         return file;
     }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,关于如何解决vue项目上传图片旋转的问题,可以考虑使用exif-js库来读取图片的Exif信息,并根据旋转角度进行旋转处理,最后再上传图片。具体代码可以参考下面的示例: ``` // 安装 exif-js 库 npm install exif-js --save // 引入 exif-js 库 import EXIF from 'exif-js'; // 假设选择的图片file 对象 const file = ...; // 读取图片的 Exif 信息 EXIF.getData(file, function() { // 获取旋转角度信息 const orientation = EXIF.getTag(this, "Orientation"); // 根据旋转角度进行旋转处理 let canvas = document.createElement('canvas'); let ctx = canvas.getContext('2d'); let img = document.createElement('img'); img.onload = function() { if ([5,6,7,8].indexOf(orientation) > -1) { canvas.width = img.height; canvas.height = img.width; } else { canvas.width = img.width; canvas.height = img.height; } switch (orientation) { case 2: ctx.transform(-1, 0, 0, 1, img.width, 0); break; case 3: ctx.transform(-1, 0, 0, -1, img.width, img.height); break; case 4: ctx.transform(1, 0, 0, -1, 0, img.height); break; case 5: ctx.transform(0, 1, 1, 0, 0, 0); break; case 6: ctx.transform(0, 1, -1, 0, img.height, 0); break; case 7: ctx.transform(0, -1, -1, 0, img.height, img.width); break; case 8: ctx.transform(0, -1, 1, 0, 0, img.width); break; default: ctx.transform(1, 0, 0, 1, 0, 0); } ctx.drawImage(img, 0, 0); const rotatedDataUrl = canvas.toDataURL('image/jpeg'); // 上传旋转后的图片 // ... }; img.src = URL.createObjectURL(file); }); ``` 上述代码中,通过使用exif-js库获取图片的旋转角度信息,利用canvas进行旋转处理,最后上传旋转后的图片。 希望可以帮到您!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值