vue小工具之照片信息提取(EXIF.js)

10 篇文章 0 订阅
3 篇文章 0 订阅

vue小工具之照片信息提取(EXIF.js)

**Talk is cheap,Give you the code.**

<template>
  <div class="photoAna">
    <div class="leftinput">
      <div class="photobtn">
        <el-button class="searchbtn" icon="el-icon-plus" size="medium" @click="btnSelectPhoto"
          >选择照片</el-button
        >
      </div>
      <div class="drop-area">
        <el-upload
          v-show="!img"
          class="upload-demo"
          :accept="`image/*`"
          :limit="1"
          :show-file-list="false"
          drag
          action=""
          :before-upload="beforeUpload"
        >
          <div class="el-upload__text">请选择照片或将照片拖入此区域…</div>
        </el-upload>
        <div class="img" v-if="img">
          <img :src="img" ref="anaImg" style="width: 100%" alt="" />
          <div class="delete" @click="img = ''"> <i class="el-icon-delete"></i> </div>
        </div>
      </div>
    </div>
    <div class="rightshow" v-loading="loading">
      <div class="title">
        照片基本信息
        <p class="line"></p>
      </div>
      <div class="info" v-if="info">
        <div class="info-item">
          <p class="label">拍摄位置:</p>
          <p class="value"
            >纬度:{{ info.GPSLatitude || '-' }} &nbsp;&nbsp;&nbsp;经度:{{
              info.GPSLongitude || '-'
            }}<span
              v-if="info.hasGPS"
              @click="showMap"
              style="color: rgb(59, 168, 253); margin-left: 20px; cursor: pointer"
              >上图
              <i class="el-icon-map-location" style="font-size: 20px; color: #8e93a4"></i></span
          ></p>
        </div>
        <div class="info-item">
          <p class="label">拍摄时间:</p>
          <p class="value">{{ info.DateTime || '-' }}</p>
        </div>
        <!-- //DateTime PixelXDimension PixelYDimension Make  Model Software GPSLatitude GPSLongitude -->

        <div class="info-item">
          <p class="label">拍摄星期:</p>
          <p class="value">{{ info.Week || '-' }}</p>
        </div>
        <div class="info-item">
          <p class="label">图像宽高:</p>
          <p class="value">{{
            info.PixelXDimension ? `${info.PixelXDimension}*${info.PixelYDimension}` : '-'
          }}</p>
        </div>
        <div class="info-item">
          <p class="label">生产者:</p>
          <p class="value">{{ info.Make || '-' }}</p>
        </div>
        <div class="info-item">
          <p class="label">型号:</p>
          <p class="value">{{ info.Model || '-' }}</p>
        </div>
        <div class="info-item">
          <p class="label">软件:</p>
          <p class="value">{{ info.Software || '-' }}</p>
        </div>
      </div>
    </div>

  </div>
</template>

<script>
import EXIF from 'exif-js';
import { wgs84togcj02tobd09 } from '@/utils/mapTrans';//坐标转换 自己找去!
export default {
  data() {
    return {
      img: '',
      info: null,
      loading: false
    };
  },
  methods: {
    waiting() {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve(true);
        }, 1000);
      });
    },
    beforeUpload(f) {
      let t = f.type.slice(0, 5);
      if (t !== 'image') {
        this.$message.info('请上传图片文件');
        return false;
      }
      this.readFile(f);
      return false;
    },
    readFile(f) {
      this.loading = true;
      this.img = '';
      this.info = null;
      let reader = new FileReader();
      reader.readAsDataURL(f);
      reader.onload = (e) => {
        this.img = e.target.result;
        setTimeout(() => {
          this.anaPhoto();
        }, 3000);
      };
    },
    anaPhoto() {
      let img = this.$refs.anaImg;
      let self = this;
      EXIF.getData(img, function () {
        const imgAllInfo = EXIF.getAllTags(this);
        let hasGPS = imgAllInfo.GPSLatitude && imgAllInfo.GPSLongitude;
        let trancord = {
          lng: 0,
          lat: 0,
        };
        if (hasGPS) {
          let lng = self.formatCords(imgAllInfo.GPSLongitude);
          let lat = self.formatCords(imgAllInfo.GPSLatitude);
          trancord = wgs84togcj02tobd09(lng, lat);
        }
        self.info = {
          hasGPS: hasGPS,
          GPSLatitude: hasGPS ? trancord.lat : '',
          GPSLongitude: hasGPS ? trancord.lng : '',
          DateTime: imgAllInfo.DateTime
            ? imgAllInfo.DateTime.replace(':', '-').replace(':', '-')
            : '',
          Week: imgAllInfo.DateTime ? self.formatWeek(imgAllInfo.DateTime) : '',
          PixelXDimension: imgAllInfo.PixelXDimension || '',
          PixelYDimension: imgAllInfo.PixelYDimension || '',
          Make: imgAllInfo.Make || '',
          Model: imgAllInfo.Model || '',
          Software: imgAllInfo.Software || '',
        };
      });
      this.loading = false;
    },
    formatCords(nums) {
      return nums[0] + nums[1] / 60 + nums[2] / 60 / 60;
    },
    formatWeek(time) {
      let str = new Date(time.replace(':', '-').replace(':', '-')).getDay();
      let w = '一';
      switch (str) {
        case 1:
          w = '一';
          break;
        case 2:
          w = '二';
          break;
        case 3:
          w = '三';
          break;
        case 4:
          w = '四';
          break;
        case 5:
          w = '五';
          break;
        case 6:
          w = '六';
          break;
        case 7:
          w = '七';
          break;
      }
      return '周' + w;
    },
    btnSelectPhoto() {
      let dom = document.getElementsByClassName('el-upload-dragger')[0];
      dom.click();
    },
  },
};
</script>

<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
}
</style>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值