- 安装 exif 依赖包:
npm install exif-js --save
- 使用到的函数:
export const getExifFromFile = async (image: any) => {
return new Promise(resolve => {
EXIF.getData(image, () => {
const gpsLatitude = EXIF.getTag(image, 'GPSLatitude');
const gpsLongitude = EXIF.getTag(image, 'GPSLongitude');
const dateTime = EXIF.getTag(image, 'DateTime');
let shootTime = null;
let latitude = null;
let longitude = null;
if (gpsLatitude && gpsLongitude && dateTime) {
latitude = changeToDu(gpsLatitude[0], gpsLatitude[1], gpsLatitude[2]);
longitude = changeToDu(gpsLongitude[0], gpsLongitude[1], gpsLongitude[2]);
shootTime = setCharAt(EXIF.getTag(image, 'DateTime'), [4, 7], '-');
}
resolve({ shootTime, latitude, longitude });
});
});
};
export const changeToDu = (h: any, m: any, s: any) => {
const f = parseFloat(m) + parseFloat((s / 60).toString());
return parseFloat((f / 60).toString()) + parseFloat(h);
};
export const setCharAt = (str: string, indexList: Array<number>, chr: string) => {
let returnStr: string = str;
for (let i = 0; i < indexList.length; i++) {
if (indexList[i] < returnStr.length) {
returnStr = returnStr.substr(0, indexList[i]) + chr + returnStr.substr(indexList[i] + 1);
}
}
return returnStr;
};