HarmonyOS 在添加图片exif信息修改经纬度异常,修改其他可以正常

在使用image.ImageSource对象实现exif中经纬度信息修改异常,但是修改image.PropertyKey.IMAGE_WIDTH是正常的,请问如何修改或者添加经纬度信息?

HarmonyOS
2025-01-10 07:26:57
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Excelsior_abit

需要注意两点:

1、使用buffer创建的ImageSource调用modifyImageProperties会导致buffer内容覆盖,目前buffer创建的ImageSource不支持调用此接口,请改用fd或path创建的ImageSource。

2、GPS_LATITUDE参数是这种"39, 54, 20"格式。

参考以下demo:

async getFileInfo(filePathString: string) {
  console.info(' filePathString:' + filePathString);
  let context = getContext(this) as common.UIAbilityContext;
  let resFile = fs.openSync(filePathString, fs.OpenMode.READ_ONLY)
  const dateStr = (new Date().getTime()).toString()
  // 临时文件目录
  let newPath = context.cacheDir + `/${dateStr + resFile.name}`;
  // 转化路径
  fs.copyFileSync(resFile.fd, newPath);
  // 新的路径
  let realUri = 'file://' + newPath;
  console.info(' realUri:' + realUri);
  let file = fs.openSync(realUri, fs.OpenMode.READ_ONLY);
  console.info('file fd:' + file.fd);
  const imageSource: image.ImageSource = image.createImageSource(file.fd);
  let keys = [image.PropertyKey.GPS_LATITUDE, image.PropertyKey.GPS_LONGITUDE];
  imageSource.getImageProperties(keys).then((data) => {
    console.info(' 修改前经纬度:' + JSON.stringify(data));
  }).catch((err: BusinessError) => {
    console.error(JSON.stringify(err));
  });
  let keyValues: Record<PropertyKey, string|null> = {
    [image.PropertyKey.GPS_LATITUDE]: "39.21, 54.52, 20.65",
    [image.PropertyKey.GPS_LONGITUDE]: "10.21,24.85,36.96"
  };
  let checkKey = [image.PropertyKey.GPS_LATITUDE, image.PropertyKey.GPS_LONGITUDE];
  imageSource.modifyImageProperties(keyValues).then(() => {
    imageSource.getImageProperties(checkKey).then((data) => {
      console.info(' 修改后经纬度:' + JSON.stringify(data));
    }).catch((err: BusinessError) => {
      console.error(JSON.stringify(err));
    });
  }).catch((err: BusinessError) => {
    console.error(JSON.stringify(err));
  });
}

参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-image-V5#modifyimageproperties12

分享
微博
QQ
微信
回复
2025-01-10 10:42:20
相关问题
HarmonyOS 图片exif信息获取异常
1632浏览 • 1回复 待解决
HarmonyOS 有关经纬度问题
1075浏览 • 1回复 待解决
HarmonyOS 坐标系经纬度转换
1741浏览 • 1回复 待解决
如何获取经纬度示例代码
2149浏览 • 1回复 待解决
HarmonyOS map kit 获取地图中心经纬度
1489浏览 • 1回复 待解决
HarmonyOS 请求头信息修改
906浏览 • 1回复 待解决