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 || '-' }} 经度:{{
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>