HarmonyOS鸿蒙Next中使用PhotoPicker组件访问图片/视频后需要获取exif信息,但是无法获取GPS等信息
HarmonyOS鸿蒙Next中使用PhotoPicker组件访问图片/视频后需要获取exif信息,但是无法获取GPS等信息
以下是我的代码:
/*获取图片的exif信息*/
async getExif(): Promise<Exif | null> {
try {
let file = fs.openSync(this.path, fs.OpenMode.READ_ONLY);
const fd : number = file.fd; // 获取需要被处理的图片的fd
const imageSourceApi : image.ImageSource = image.createImageSource(fd);
const keys = [
// 基础信息
image.PropertyKey.IMAGE_DESCRIPTION,
image.PropertyKey.MAKE,
image.PropertyKey.MODEL,
image.PropertyKey.ORIENTATION,
image.PropertyKey.X_RESOLUTION,
image.PropertyKey.Y_RESOLUTION,
image.PropertyKey.RESOLUTION_UNIT,
image.PropertyKey.SOFTWARE,
image.PropertyKey.DATE_TIME,
image.PropertyKey.OFFSET_TIME,
// GPS
image.PropertyKey.GPS_VERSION_ID,
image.PropertyKey.GPS_LATITUDE_REF,
image.PropertyKey.GPS_LATITUDE,
image.PropertyKey.GPS_LONGITUDE_REF,
image.PropertyKey.GPS_LONGITUDE,
image.PropertyKey.GPS_ALTITUDE_REF,
image.PropertyKey.GPS_ALTITUDE,
image.PropertyKey.GPS_STATUS,
image.PropertyKey.GPS_MAP_DATUM,
// 拍摄参数
image.PropertyKey.EXPOSURE_TIME,
image.PropertyKey.F_NUMBER,
image.PropertyKey.EXPOSURE_PROGRAM,
image.PropertyKey.ISO_SPEED_RATINGS,
image.PropertyKey.SENSITIVITY_TYPE,
image.PropertyKey.EXIF_VERSION,
image.PropertyKey.DATE_TIME_ORIGINAL,
image.PropertyKey.DATE_TIME_DIGITIZED,
image.PropertyKey.SHUTTER_SPEED,
image.PropertyKey.APERTURE_VALUE,
image.PropertyKey.EXPOSURE_BIAS_VALUE,
image.PropertyKey.MAX_APERTURE_VALUE,
image.PropertyKey.METERING_MODE,
image.PropertyKey.LIGHT_SOURCE,
image.PropertyKey.FLASH,
image.PropertyKey.FOCAL_LENGTH,
image.PropertyKey.COLOR_SPACE,
image.PropertyKey.PIXEL_X_DIMENSION,
image.PropertyKey.PIXEL_Y_DIMENSION,
image.PropertyKey.WHITE_BALANCE,
image.PropertyKey.DIGITAL_ZOOM_RATIO,
image.PropertyKey.SCENE_CAPTURE_TYPE,
image.PropertyKey.GAIN_CONTROL,
image.PropertyKey.CONTRAST,
image.PropertyKey.SATURATION,
image.PropertyKey.SHARPNESS,
image.PropertyKey.BODY_SERIAL_NUMBER,
image.PropertyKey.LENS_SPECIFICATION
]
const result = await imageSourceApi.getImageProperties(keys)
console.log("照片的exif信息:" + JSON.stringify(result))
return null
}
以下是result输出日志,gps信息是0,而且相机品牌也无法获取。
{
"ImageDescription": null,
"Make": null,
"Model": null,
"Orientation": "Top-left",
"XResolution": "96",
"YResolution": "96",
"ResolutionUnit": "Inch",
"Software": null,
"DateTime": null,
"OffsetTime": null,
"GPSVersionID": "0.0.0.0",
"GPSLatitudeRef": "N",
"GPSLatitude": " 0, 0, 0.00",
"GPSLongitudeRef": "E",
"GPSLongitude": " 0, 0, 0.00",
"GPSAltitudeRef": "Sea level",
"GPSAltitude": "0.00",
"GPSStatus": null,
"GPSMapDatum": null,
"ExposureTime": "0 sec.",
"FNumber": "f/0.0",
"ExposureProgram": "Not defined",
"ISOSpeedRatings": "0",
"SensitivityType": null,
"ExifVersion": "Exif Version 2.3",
"DateTimeOriginal": null,
"DateTimeDigitized": null,
"ShutterSpeedValue": null,
"ApertureValue": "1.60 EV (f/1.7)",
"ExposureBiasValue": "0.00 EV",
"MaxApertureValue": "1.60 EV (f/1.7)",
"MeteringMode": "Unknown",
"LightSource": null,
"Flash": "Flash did not fire",
"FocalLength": "0.0 mm",
"ColorSpace": "sRGB",
"PixelXDimension": null,
"PixelYDimension": null,
"WhiteBalance": "Auto white balance",
"DigitalZoomRatio": null,
"SceneCaptureType": null,
"GainControl": null,
"Contrast": null,
"Saturation": null,
"Sharpness": null,
"BodySerialNumber": null,
"LensSpecification": null
}
以下是照片的相关信息:
更多关于HarmonyOS鸿蒙Next中使用PhotoPicker组件访问图片/视频后需要获取exif信息,但是无法获取GPS等信息的实战教程也可以访问 https://www.itying.com/category-93-b0.html
1 回复
更多关于HarmonyOS鸿蒙Next中使用PhotoPicker组件访问图片/视频后需要获取exif信息,但是无法获取GPS等信息的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,使用PhotoPicker组件获取图片/视频后,若无法直接获取EXIF中的GPS信息,可以通过以下步骤解决:
- 使用Image组件加载图片:通过
Image
组件加载图片后,使用Image.Source
获取图片的URI。 - 解析EXIF信息:使用
ExifInterface
类解析图片的EXIF信息。确保在build.gradle
中添加ohos.exif
依赖。 - 获取GPS信息:通过
ExifInterface
的getAttribute
方法获取GPS相关的标签,如TAG_GPS_LATITUDE
和TAG_GPS_LONGITUDE
。
示例代码:
ExifInterface exif = new ExifInterface(imageUri);
String latitude = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
String longitude = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
确保权限已正确配置,如ohos.permission.LOCATION
。