相机图片元数据

ARCore 允许您使用 ImageMetadata 访问相机图像拍摄结果中的元数据键值对。您可能希望访问的一些常见类型的相机图片元数据包括焦距、图片时间戳数据或光照信息。

Android Camera 模块可以为拍摄的每个帧记录 160 个或更多关于图片的参数,具体取决于设备的功能。如需查看所有可能的元数据键的列表,请参阅 ImageMetadata

获取单个元数据键的值

使用 getImageMetadata() 获取特定的元数据键值对,并在 MetadataNotFoundException 不可用时捕获该键值对。以下示例展示了如何获取 SENSOR_EXPOSURE_TIME 元数据键值对。

Java

// Obtain the SENSOR_EXPOSURE_TIME metadata value from the frame.
Long getSensorExposureTime(Frame frame) {
  try {
    // Can throw NotYetAvailableException when sensors data is not yet available.
    ImageMetadata metadata = frame.getImageMetadata();

    // Get the exposure time metadata. Throws MetadataNotFoundException if it's not available.
    return metadata.getLong(ImageMetadata.SENSOR_EXPOSURE_TIME);
  } catch (MetadataNotFoundException | NotYetAvailableException exception) {
    return null;
  }
}

Kotlin

// Obtain the SENSOR_EXPOSURE_TIME metadata value from the frame.
fun getSensorExposureTime(frame: Frame): Long? {
  return runCatching {
      // Can throw NotYetAvailableException when sensors data is not yet available.
      val metadata = frame.imageMetadata

      // Get the exposure time metadata. Throws MetadataNotFoundException if it's not available.
      return metadata.getLong(ImageMetadata.SENSOR_EXPOSURE_TIME)
    }
    .getOrNull()
}