vue使用exif获取图片旋转,压缩的示例代码


Posted in Vue.js onDecember 11, 2020
<template>
 <div>
  <input type="file" id="upload" accept="image" @change="upload" />
 </div>
</template>
<script>
export default {
 data() {
  return {
   picValue:{},
   headerImage:''
  };
 },
 components: {},
 methods: {
  upload(e) {
   console.log(e);
   let files = e.target.files || e.dataTransfer.files;
   if (!files.length) return;
   this.picValue = files[0];
   this.imgPreview(this.picValue);
  },
  imgPreview(file) {
   let self = this;
   let Orientation;
   //去获取拍照时的信息,解决拍出来的照片旋转问题
   self.Exif.getData(file, function() {
    Orientation = self.Exif.getTag(this, 'Orientation');
   });
   // 看支持不支持FileReader
   if (!file || !window.FileReader) return;

   if (/^image/.test(file.type)) {
    // 创建一个reader
    let reader = new FileReader();
    // 将图片2将转成 base64 格式
    reader.readAsDataURL(file);
    // 读取成功后的回调
    reader.onloadend = function() {
     let result = this.result;
     let img = new Image();
     img.src = result;
     //判断图片是否大于100K,是就直接上传,反之压缩图片
     if (this.result.length <= 100 * 1024) {
      self.headerImage = this.result;
      self.postImg();
     } else {
      img.onload = function() {
       let data = self.compress(img, Orientation);
       self.headerImage = data;
       self.postImg();
      };
     }
    };
   }
  },
  compress(img, Orientation) {
   let canvas = document.createElement('canvas');
   let ctx = canvas.getContext('2d');
   //瓦片canvas
   let tCanvas = document.createElement('canvas');
   let tctx = tCanvas.getContext('2d');
   let initSize = img.src.length;
   let width = img.width;
   let height = img.height;
   //如果图片大于四百万像素,计算压缩比并将大小压至400万以下
   let ratio;
   if ((ratio = (width * height) / 4000000) > 1) {
    console.log('大于400万像素');
    ratio = Math.sqrt(ratio);
    width /= ratio;
    height /= ratio;
   } else {
    ratio = 1;
   }
   canvas.width = width;
   canvas.height = height;
   //    铺底色
   ctx.fillStyle = '#fff';
   ctx.fillRect(0, 0, canvas.width, canvas.height);
   //如果图片像素大于100万则使用瓦片绘制
   let count;
   if ((count = (width * height) / 1000000) > 1) {
    console.log('超过100W像素');
    count = ~~(Math.sqrt(count) + 1); //计算要分成多少块瓦片
    //      计算每块瓦片的宽和高
    let nw = ~~(width / count);
    let nh = ~~(height / count);
    tCanvas.width = nw;
    tCanvas.height = nh;
    for (let i = 0; i < count; i++) {
     for (let j = 0; j < count; j++) {
      tctx.drawImage(
       img,
       i * nw * ratio,
       j * nh * ratio,
       nw * ratio,
       nh * ratio,
       0,
       0,
       nw,
       nh
      );
      ctx.drawImage(tCanvas, i * nw, j * nh, nw, nh);
     }
    }
   } else {
    ctx.drawImage(img, 0, 0, width, height);
   }
   //修复ios上传图片的时候 被旋转的问题
   if (Orientation != '' && Orientation != 1) {
    switch (Orientation) {
     case 6: //需要顺时针(向左)90度旋转
      this.rotateImg(img, 'left', canvas);
      break;
     case 8: //需要逆时针(向右)90度旋转
      this.rotateImg(img, 'right', canvas);
      break;
     case 3: //需要180度旋转
      this.rotateImg(img, 'right', canvas); //转两次
      this.rotateImg(img, 'right', canvas);
      break;
    }
   }
   //进行最小压缩
   let ndata = canvas.toDataURL('image/jpeg', 0.1);
   tCanvas.width = tCanvas.height = canvas.width = canvas.height = 0;
   return ndata;
  },
  rotateImg(img, direction, canvas) {
   //最小与最大旋转方向,图片旋转4次后回到原方向
   const min_step = 0;
   const max_step = 3;
   if (img == null) return;
   //img的高度和宽度不能在img元素隐藏后获取,否则会出错
   let height = img.height;
   let width = img.width;
   let 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);
   }
   //旋转角度以弧度值为参数
   let degree = (step * 90 * Math.PI) / 180;
   let 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;
   }
  },
  postImg() {
   //这里写接口

//打印的图片base64

   console.log('this.headerImage',this.headerImage);
   //接口 axios
  }
 }
};
</script>

要先运行

npm install exif-js --save

然后在main.js中添加

import Exif from 'exif-js'
Vue.use(Exif)
Vue.prototype.Exif = Exif

以上就是vue使用exif获取图片旋转,压缩的示例代码的详细内容,更多关于vue 图片旋转,压缩的资料请关注三水点靠木其它相关文章!

Vue.js 相关文章推荐
vue祖孙组件之间的数据传递案例
Dec 07 Vue.js
Vue-router中hash模式与history模式的区别详解
Dec 15 Vue.js
vue3使用vue-count-to组件的实现
Dec 25 Vue.js
vue 实现基础组件的自动化全局注册
Dec 25 Vue.js
vue动态设置路由权限的主要思路
Jan 13 Vue.js
如何在vue中使用HTML 5 拖放API
Jan 14 Vue.js
如何在vue 中使用柱状图 并自修改配置
Jan 21 Vue.js
vue.js实现点击图标放大离开时缩小的代码
Jan 27 Vue.js
Vue3中toRef与toRefs的区别
Mar 24 Vue.js
vue3使用vuedraggable实现拖拽功能
Apr 06 Vue.js
vue选项卡切换的实现案例
Apr 11 Vue.js
Vue操作Storage本地化存储
Apr 29 Vue.js
Vue 实现一个简单的鼠标拖拽滚动效果插件
Dec 10 #Vue.js
vuex页面刷新导致数据丢失的解决方案
Dec 10 #Vue.js
详解vue-cli项目在IE浏览器打开报错解决方法
Dec 10 #Vue.js
Vue+element-ui添加自定义右键菜单的方法示例
Dec 08 #Vue.js
vue添加自定义右键菜单的完整实例
Dec 08 #Vue.js
vue中如何自定义右键菜单详解
Dec 08 #Vue.js
基于vue与element实现创建试卷相关功能(实例代码)
Dec 07 #Vue.js
You might like
ThinkPHP 5.x远程命令执行漏洞复现
2019/09/23 PHP
thinkphp 5框架实现登陆,登出及session登陆状态检测功能示例
2019/10/10 PHP
收集的一些Array及String原型对象的扩展实现代码
2010/12/05 Javascript
Javascript创建自定义对象 创建Object实例添加属性和方法
2012/06/04 Javascript
jquery选择checked在ie8普通模式下的问题
2014/02/12 Javascript
Bootstrap中的Dropdown下拉菜单更改为悬停(hover)触发
2016/08/31 Javascript
微信小程序 http请求详细介绍
2016/10/09 Javascript
JS中跨页面调用变量和函数的方法(例如a.js 和 b.js中互相调用)
2016/11/01 Javascript
详解基于vue-router的动态权限控制实现方案
2017/09/28 Javascript
移动web开发之touch事件实例详解
2018/01/17 Javascript
JavaScript Math对象和调试程序的方法分析
2019/05/13 Javascript
vue实现文件上传读取及下载功能
2020/11/17 Javascript
vue获取data数据改变前后的值方法
2019/11/07 Javascript
[00:37]DOTA2上海特级锦标赛 Secert 战队宣传片
2016/03/03 DOTA
python的turtle库使用详解
2019/05/10 Python
pygame实现贪吃蛇游戏(上)
2019/10/29 Python
Pytorch 使用不同版本的cuda的方法步骤
2020/04/02 Python
详解numpy.ndarray.reshape()函数的参数问题
2020/10/13 Python
Pycharm创建文件时自动生成文件头注释(自定义设置作者日期)
2020/11/24 Python
python pygame 愤怒的小鸟游戏示例代码
2021/02/25 Python
纯CSS3实现的阴影效果
2014/12/24 HTML / CSS
Amaze UI 文件选择域的示例代码
2020/08/26 HTML / CSS
sleep()方法和wait()方法的区别是什么
2012/11/17 面试题
简历自我评价怎么写好呢?
2014/01/04 职场文书
优秀通讯员事迹材料
2014/01/28 职场文书
一体化教学实施方案
2014/05/10 职场文书
个人存款证明书
2014/10/18 职场文书
2014年学校教学工作总结
2014/12/06 职场文书
经验交流材料格式
2014/12/30 职场文书
医院办公室主任岗位职责
2015/04/01 职场文书
元宵节晚会主持词
2015/07/01 职场文书
小学感恩主题班会
2015/08/12 职场文书
导游词之无锡唐城
2019/12/12 职场文书
pycharm debug 断点调试心得分享
2021/04/16 Python
Smart 2 车辆代号 HC11 全新谍照曝光
2022/04/21 数码科技
Mysql的Table doesn't exist问题及解决
2022/12/24 MySQL