奇怪的需求
parseImgDataInfo 传输 blob对象即可
//newData 传输 blob对象
async parseImgDataInfo(newData) {
try {
const fileBase64 = await onFileBase64(newData);
const exifInfo = await getExifInfo(fileBase64);
this.longitude = exifInfo.lon;
this.latitude = exifInfo.lat;
this.datetime = exifInfo.datetime;
this.mobile= exifInfo.mobile;
return;
} catch (e) {
console.log("e:", e);
}
},
exif.js 包
import EXIF from "@/static/JS/exif.js";
// 文件流转base64
onFileBase64 (eventBlob) {
return new Promise((resolve, reject) => {
if (!!eventBlob) {
let file = eventBlob;
let reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => {
resolve(reader.result)
};
} else {
reject('图片数据为空')
}
})
},
// 查询图片文件流中的信息 H5使用
getExifInfo (imgSrc) {
return new Promise((resolve, reject) => {
if (!imgSrc) {
reject('请先选择图片')
return;
}
let imgData = {
datetime: null,
mobile: null,//设备型号
lon: null,
lat: null,
code: true,
msg: ""
}
const img = new Image();
img.src = imgSrc;
img.onload = () => {
console.log('图片加载');
EXIF.getData(img, () => {
let tags = EXIF.getAllTags(img);
imgData.datetime = tags.DateTimeDigitized;
const lon = EXIF.getTag(img, 'GPSLongitude'); // 经度
const lat = EXIF.getTag(img, 'GPSLatitude'); // 纬度
const make = EXIF.getTag(img, 'Make');
const model = EXIF.getTag(img, 'Model');
if (make && model) {
imgData.mobile = `${make} ${model}`;
}
if (lon && lat) {
// 计算出经纬度并保留6位小数
imgData.lon = (lon[0] + lon[1] / 60 + lon[2] / 3600).toFixed(6);
imgData.lat = (lat[0] + lat[1] / 60 + lat[2] / 3600).toFixed(6);
resolve(imgData)
} else {
imgData.code = false
imgData.msg = '没有地理信息'
resolve(imgData)
}
});
};
})
}