zoukankan      html  css  js  c++  java
  • vue 的图片上传,压缩,exif图片信息识别(手机拍摄横板问题)

    1.file表单组件监听change获取file文件

    2.创建fileReader 获取base64位图片路由

    3.实例一个Image 将base64位图片路由对其进行赋值,创建canvas 通过drawImage方法对其进行等比例缩放复制,实现图片压缩

    4.引入 exif-js 模块 获取旋转角度信息,非1的,对canvas进行旋转,重绘,再通过toDataURL 获取旋转矫正后的图片64位

    5.调用回调函数进行图片上传

    //upload 
    import EXIF from "exif-js";
    //可能会用到 base64位转blob function dataURLToBlob(dataurl){ var arr = dataurl.split(','); var mime = arr[0].match(/:(.*?);/)[1]; var bstr = atob(arr[1]); var n = bstr.length; var u8arr = new Uint8Array(n); while(n--){ u8arr[n] = bstr.charCodeAt(n); } return new Blob([u8arr], {type:mime}); } function rotateImg(img, direction,canvas) { //最小与最大旋转方向,图片旋转4次后回到原方向 var min_step = 0; var max_step = 3; //var img = document.getElementById(pid); if (img == null)return; var height = canvas.height; var width = canvas.width; //var step = img.getAttribute('step'); var 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); } //旋转角度以弧度值为参数 var degree = step * 90 * Math.PI / 180; var ctx = canvas.getContext('2d'); switch (step) { case 0: canvas.width = width; canvas.height = height; ctx.drawImage(img, 0, 0,width,height); break; case 1: canvas.width = height; canvas.height = width; ctx.rotate(degree); ctx.drawImage(img, 0, -height,width,height); break; case 2: canvas.width = width; canvas.height = height; ctx.rotate(degree); ctx.drawImage(img, -width, -height,width,height); break; case 3: canvas.width = height; canvas.height = width; ctx.rotate(degree); ctx.drawImage(img, -width, 0,width,height); break; } } export const upload=(file,callback)=>{//传入file文件 //对图片旋转处理 let Orientation = null; //获取照片方向角属性,用户旋转控制 EXIF.getData(file, function() { EXIF.getAllTags(this); Orientation = EXIF.getTag(this, 'Orientation'); }); let oReader = new FileReader(); oReader.onload = function(e) { var image = new Image(); image.src = e.target.result; debugger; image.onload = function() { var expectWidth = this.naturalWidth; var expectHeight = this.naturalHeight; if (this.naturalWidth > this.naturalHeight && this.naturalWidth > 800) { expectWidth = 800; expectHeight = expectWidth * this.naturalHeight / this.naturalWidth; } else if (this.naturalHeight > this.naturalWidth && this.naturalHeight > 1200) { expectHeight = 1200; expectWidth = expectHeight * this.naturalWidth / this.naturalHeight; } var canvas = document.createElement("canvas"); var ctx = canvas.getContext("2d"); canvas.width = expectWidth; canvas.height = expectHeight; ctx.drawImage(this, 0, 0, expectWidth, expectHeight); var base64 = null; if(Orientation != "" && Orientation != 1){ switch(Orientation){ case 6://需要顺时针(向左)90度旋转 // alert('需要顺时针(向左)90度旋转'); rotateImg(this,'left',canvas); break; case 8://需要逆时针(向右)90度旋转 // alert('需要顺时针(向右)90度旋转'); rotateImg(this,'right',canvas); break; case 3://需要180度旋转 // alert('需要180度旋转'); rotateImg(this,'right',canvas);//转两次 rotateImg(this,'right',canvas); break; } } base64 = canvas.toDataURL("image/jpeg", 0.8); callback(base64) }; }; oReader.readAsDataURL(file); }

     使用示例

    <template>
      <div class="hello">
        <h1>exif 图片数码信息</h1>
        <input type="file" name="pic" ref="file" accept="image/*" @change="uploadFile"/>
        <div id="output">
          <img :src="src" alt="">
        </div>
      </div>
    </template>
    
    <script>
    import {upload} from "../assets/tool/upload";
    export default {
      name: 'exif',
      data(){
        return {
          src:''
        }
      },
      methods:{
        uploadFile(e){
          //获取file文件
          let files=e.target.files || e.dataTransfer.files;
          if(files.length<=0)
            return;
          let file=files[0];
          let vm=this;
          upload(file,function(base64){vm.src=base64})
        },
    
      },
      mounted() {
      }
    }
    </script>
    

      

  • 相关阅读:
    三行代码搞定微信登录集成
    iptables命令
    Linux(centos)系统各个目录的作用详解 推荐
    Linux下Apache服务的查看和启动
    Linux使用退格键时出现^H ^?解决方法
    小程序:最难点For的wx:key
    linux 通过wol远程开机【转】
    linux wake on lan功能通过ethtool配置【转】
    设计模式小议:state【转】
    TCP/IP详解学习笔记(4)-ICMP协议,ping和Traceroute【转】
  • 原文地址:https://www.cnblogs.com/yihuite-zch/p/11447429.html
Copyright © 2011-2022 走看看