HarmonyOS getImageProperties可以获取picker返回的uri文件的exif信息吗?

getImageProperties可以获取picker返回的uri文件的exif信息吗?

用picker选择了相片,然后使用:

let file = fileIo.openSync(uri, fileIo.OpenMode.READ_ONLY)
const imageSourceApi: image.ImageSource = image.createImageSource(file.fd)
const exif = await imageSourceApi.getImageProperties([
  image.PropertyKey.IMAGE_WIDTH,
  image.PropertyKey.IMAGE_LENGTH,
  image.PropertyKey.MAKE,
  image.PropertyKey.ISO_SPEED_RATINGS,
])

获取exif信息,报了以下的错误:

Error: There is generic napi failure! exif key: ImageWidth,Error: There is generic napi failure! exif key: ImageLength,Error: There is generic napi failure! exif key: Make,Error: There is generic napi failure! exif key: ISOSpeedRatings 

是不是不允许获取外部相片的 exif ?还是需要申请什么权限?

阅读 576
1 个回答

可参考以下demo:

import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { image } from '@kit.ImageKit';
import { fileIo as fs, picker } from '@kit.CoreFileKit';

let selectUris: string[] = [];

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  build() {
    Column() {
      Button('打开相册')
        .onClick(() => {
          //创建图库选择器对象实例
          let PhotoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
          PhotoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
          PhotoSelectOptions.maxSelectNumber = 5;
          const photoViewPicker =new photoAccessHelper.PhotoViewPicker();
          //调用select()接口拉起图库界面进行文件选择,文件选择成功后,返回PhotoSelectResult结果集
          photoViewPicker.select(PhotoSelectOptions).then(async (photoSelectResult: picker.PhotoSelectResult) => {
            //用一个全局变量存储返回的uri
            selectUris = photoSelectResult.photoUris;
            console.info('photoViewPicker.select to file succeed and uris are:' + selectUris);
          }).catch((err: BusinessError) => {
            console.error(`Invoke photoViewPicker.select failed, code is ${err.code}, message is ${err.message}`);
          })
        })
      Button('creatPixelMap')
        .margin({ top: 10 })
        .onClick((event: ClickEvent) => {
          //使用fs.openSync接口,通过uri打开这个文件得到fd
          let file = fs.openSync(selectUris[0], fs.OpenMode.READ_ONLY);
          console.info('file fd: ' + file.fd);
          //根据文件fd创建imagSource
          const imageSource: image.ImageSource = image.createImageSource(file.fd);
          let key = [image.PropertyKey.IMAGE_WIDTH, image.PropertyKey.IMAGE_LENGTH, image.PropertyKey.DATE_TIME_ORIGINAL];
          imageSource.getImageProperties(key).then((data) => {
            console.info(JSON.stringify(data));
          }).catch((err: BusinessError) => {
            console.error(JSON.stringify(err));
          });
        })
    }
    .height('100%')
    .width('100%')
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进