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中通过render函数给子组件设置ref操作
Nov 17 Vue.js
vuex页面刷新导致数据丢失的解决方案
Dec 10 Vue.js
Vue——解决报错 Computed property &quot;****&quot; was assigned to but it has no setter.
Dec 19 Vue.js
vue下拉刷新组件的开发及slot的使用详解
Dec 23 Vue.js
浅析vue中的nextTick
Dec 28 Vue.js
利用Vue实现简易播放器的完整代码
Dec 30 Vue.js
Vue中inheritAttrs的使用实例详解
Dec 31 Vue.js
vue 中 get / delete 传递数组参数方法
Mar 23 Vue.js
Vue过滤器(filter)实现及应用场景详解
Jun 15 Vue.js
vue3引入highlight.js进行代码高亮的方法实例
Apr 08 Vue.js
解决vue中provide inject的响应式监听
Apr 19 Vue.js
el-table-column 内容不自动换行的解决方法
Aug 14 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
浅析PHP中Collection 类的设计
2013/06/21 PHP
PHP变量赋值、代入给JavaScript中的变量
2015/06/29 PHP
php版微信自动登录并获取昵称的方法
2016/09/23 PHP
Tinymce+jQuery.Validation使用产生的BUG
2010/03/29 Javascript
jQuery 瀑布流 浮动布局(一)(延迟AJAX加载图片)
2012/05/23 Javascript
jquery阻止冒泡事件使用模拟事件
2013/09/06 Javascript
用html+css+js实现的一个简单的图片切换特效
2014/05/28 Javascript
jQuery实现ichat在线客服插件
2014/12/29 Javascript
JS+CSS实现可拖动的弹出提示框
2015/02/16 Javascript
JavaScript动态添加style节点的方法
2015/06/09 Javascript
JS运动相关知识点小结(附弹性运动示例)
2016/01/08 Javascript
jQuery元素属性操作实例(设置、获取及删除元素属性)
2016/09/08 Javascript
jQuery实现的简单悬浮层功能完整实例
2017/01/23 Javascript
js实现模糊匹配功能
2017/02/15 Javascript
JavaScript中变量、指针和引用功能与操作示例
2018/08/04 Javascript
React优化子组件render的使用
2019/05/12 Javascript
prettier自动格式化去换行的实现代码
2020/08/25 Javascript
[50:20]DOTA2上海特级锦标赛主赛事日 - 5 总决赛Liquid VS Secret第四局
2016/03/06 DOTA
探寻python多线程ctrl+c退出问题解决方案
2014/10/23 Python
python实现爬虫下载美女图片
2015/07/14 Python
Python实现邮件的批量发送的示例代码
2018/01/23 Python
每天迁移MySQL历史数据到历史库Python脚本
2018/04/13 Python
利用Anaconda简单安装scrapy框架的方法
2018/06/13 Python
pandas 将索引值相加的方法
2018/11/15 Python
PyQt5 窗口切换与自定义对话框的实例
2019/06/20 Python
基于python-opencv3的图像显示和保存操作
2019/06/27 Python
python+mysql实现个人论文管理系统
2019/10/25 Python
python简单的三元一次方程求解实例
2020/04/02 Python
英国奢侈皮具品牌:Aspinal of London
2018/09/02 全球购物
阿拉伯书店:Jamalon
2019/07/24 全球购物
博士研究生自我鉴定范文
2013/12/04 职场文书
迷你西餐厅创业计划书范文
2013/12/31 职场文书
招股说明书范本
2014/05/06 职场文书
优秀党员主要事迹材料
2015/11/04 职场文书
忠诚教育学习心得体会
2016/01/23 职场文书
Windows 64位 安装 mysql 8.0.28 图文教程
2022/04/19 MySQL