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" >
    </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>

  

原文地址:https://www.cnblogs.com/yihuite-zch/p/11447429.html

时间: 2024-08-28 00:39:38

vue 的图片上传,压缩,exif图片信息识别(手机拍摄横板问题)的相关文章

图片上传压缩 Thinkphp

图片上传压缩 待完成 import('ORG.Net.UploadFile'); $upload = new UploadFile(); $upload->maxSize = 4194304; $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg'); $upload->savePath = 'Public/Mxun/vote/'; //略所图 $upload->thumb = true; $upload->thumbTy

Vue 做图片上传

Vue图片单图或者多图上传代码如下: <template> <div class="upimgBox"> <div class="container"> <div class="upload_warp_left" @click="fileClick" > <div class="addNote">+</div> </div>

Java图片上传压缩处理

所需要的jar包在:\jdk1.7.0_25\jre\lib\rt.jar里面 package util; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import javax.ima

vue实现图片上传

项目中用到的是上传头像,前端通过input[type="file"]来选择图片,给后端传递一个formData格式的数据,然后上传.代码如下: 我写了个组件,参数如下: uploadType: 上传类型 width: 图片显示的宽度 height: 图片显示的高度 imgUrl: 如果之前有图片,图片的路径地址 getImgUrl: 在组件里上传成功之后,会得到图片路径的相关参数,该方法在父组件里面调用来获取子组件里返回的图片路径参数,这个事件要看需求,在父组件里需不需要上传之后返回的

图片上传压缩校正

import Exif from 'exif-js'; /* eslint-disable func-names */ function ImageProcess(file, callback) { let Orientation; // 去获取拍照时的信息,解决拍出来的照片旋转问题 Exif.getData(file, function () { Orientation = Exif.getTag(this, 'Orientation'); }); // console.log("Orient

图片上传压缩方法,测试过,失真度能接受

public Bitmap revitionImageSize(String path) throws IOException { BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(path))); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; /

vue 实现图片上传与预览,以及清除图片

vue写原生的上传图片并预览以及清除图片的效果,下面是demo,其中里面有vue获取input框value值的方法以及vue中函数之间的调用 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>图片demo</title> <link rel="stylesheet" href="../../css/font-a

使用django + Pillow进行图片上传压缩格式保存时出错的处理

安装Pillow之前先安装好python-dev libzip-dev libjpeg8-dev libfreetype6-dev libpng12-dev 否则在用PIL(Pillow)进行上传图片压缩格式保存时会出错: encoder jpeg not available decoder zip not available sudo apt-get install python-dev libzip-dev libjpeg8-dev libfreetype6-dev libpng12-dev

图片上传压缩

<script type="text/javascript"> var img = { Imgsource: [], } $(function () { $(".addimg").click(function () { $("#uploadFile").click(); }); $('#uploadFile').localResizeIMG({ //width: 100, quality: 0.7, success: function