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>

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM