项目场景:
项目场景:使用Android,IOS,H5,微信小程序获取图片拍摄日期,获取图片拍摄位置
问题描述
提示:Android,H5,与微信小程序使用exif都没有问题,但是IOS不行
EXIF.getData() 方法执行了,但是回调没有执行
IOS中接根据图片获取拍摄日期跟地址的代码:
entry.file(
function(file) {
var reader = new plus.io.FileReader();
reader.onloadend = function(e) {
var base64 = e.target.result;
var name = tmpimg.substring(tmpimg.lastIndexOf("/") + 1);
var img = {
path: tmpimg,
name: name,
src: base64,
};
EXIF.getData(img, function() {
let tagj = EXIF.getTag(this, "GPSLongitude");
let tagw = EXIF.getTag(this, "GPSLatitude");
dateTime = EXIF.getTag(this, "DateTime");
let longitude = "",
latitude = "";
if (tagj && tagw) {
longitude = tagj[0] + tagj[1] / 60 + tagj[2] / 60 / 60;
latitude = tagw[0] + tagw[1] / 60 + tagw[2] / 60 / 60;
latLon = latitude + "$" + longitude;
}
console.log("app经纬度", latLon, dateTime);
});
};
reader.readAsDataURL(file);
},
function(e) {
console.log("读写出现异常: " + e.message);
// throw "Could not load image";
}
);
原因分析:
提示:上传的图片必须是原图并且是自己手机拍摄的,并且IOS拍摄需要开启隐私位置权限给相机
这些都给打开了,还是不行,以为是ios reader的兼容性问题,发现不是兼容性问题,那就肯定是EXIF的问题了
解决方案:
提示:这里填写该问题的具体解决方案:
通过查找EXIF代码,开启EXIF的debug模式,找到readThumbnailImage 方法里 IOS中 thumbTags[‘blob’] = new Blob([new Uint8Array(dataView.buffer, tOffset, tLength)], {type: ‘image/jpeg’}); 报错了 导致没有输出结果。thumbTags 数据里有图片的经纬度与拍摄日期索性就把Blob 在IOS里没让执行。算一个曲线救国的办法,因为我不知道这个blob文件在后续有什么作用,现在是不报错了 哈哈哈哈哈哈
function readThumbnailImage(dataView, tiffStart, firstIFDOffset, bigEnd){
// get the IFD1 offset
var IFD1OffsetPointer = getNextIFDOffset(dataView, tiffStart+firstIFDOffset, bigEnd);
if (!IFD1OffsetPointer) {
// console.log('******** IFD1Offset is empty, image thumb not found ********');
return {};
}
else if (IFD1OffsetPointer > dataView.byteLength) { // this should not happen
// console.log('******** IFD1Offset is outside the bounds of the DataView ********');
return {};
}
// console.log('******* thumbnail IFD offset (IFD1) is: %s', IFD1OffsetPointer);
var thumbTags = readTags(dataView, tiffStart, tiffStart + IFD1OffsetPointer, IFD1Tags, bigEnd)
// EXIF 2.3 specification for JPEG format thumbnail
// If the value of Compression(0x0103) Tag in IFD1 is '6', thumbnail image format is JPEG.
// Most of Exif image uses JPEG format for thumbnail. In that case, you can get offset of thumbnail
// by JpegIFOffset(0x0201) Tag in IFD1, size of thumbnail by JpegIFByteCount(0x0202) Tag.
// Data format is ordinary JPEG format, starts from 0xFFD8 and ends by 0xFFD9. It seems that
// JPEG format and 160x120pixels of size are recommended thumbnail format for Exif2.1 or later.
if (thumbTags['Compression']) {
// console.log('Thumbnail image found!');
switch (thumbTags['Compression']) {
case 6:
// console.log('Thumbnail image format is JPEG');
if (thumbTags.JpegIFOffset && thumbTags.JpegIFByteCount) {
// extract the thumbnail
var tOffset = tiffStart + thumbTags.JpegIFOffset;
var tLength = thumbTags.JpegIFByteCount;
if (debug) console.log("thumbTags",thumbTags)
// 修改第三方 ios获取Blob文件失败 安卓可以
if(uni.getSystemInfoSync().platform == 'ios'){
thumbTags['blob'] = "ios 报错";
}else{
thumbTags['blob'] = new Blob([new Uint8Array(dataView.buffer, tOffset, tLength)], {
type: 'image/jpeg'
});
}
}
break;
case 1:
console.log("Thumbnail image format is TIFF, which is not implemented.");
break;
default:
console.log("Unknown thumbnail image format '%s'", thumbTags['Compression']);
}
}
else if (thumbTags['PhotometricInterpretation'] == 2) {
console.log("Thumbnail image format is RGB, which is not implemented.");
}
return thumbTags;
}