配置相机

CameraConfig 描述底层摄像头传感器的属性,包括:

  • 相机 ID
  • 是否使用深度传感器(如果有)
  • 摄像头朝向的方向:
    • 前置(自拍)
    • 后置(世界)
  • FPS(每秒帧数)范围
  • CPU 图片尺寸
  • GPU 纹理尺寸
  • 如果存在,则系统是否使用设备的立体声多摄像头

创建新的 ARCore 会话时,ARCore 会使用 setCameraConfig 设置与 getSupportedCameraConfigs(CameraConfigFilter) 返回的可用配置列表最匹配的摄像头配置。您的应用可以使用 CameraConfigFilter 根据应用的需求进行过滤,从而在运行时缩小给定设备的可用摄像头配置范围。

过滤功能的常见用例包括:

  • 将相机拍摄帧速率限制为 30 fps。在支持 60 fps 的设备上,ARCore 会优先支持支持该帧速率的摄像头配置。如需过滤掉支持 60 fps 的所有摄像头配置,请使用 TargetFps.TARGET_FPS_30 通过 setTargetFps 应用滤镜。

    Java

    // Return only camera configs that target 30 FPS camera capture frame rate.
    filter.setTargetFps(EnumSet.of(CameraConfig.TargetFps.TARGET_FPS_30));

    Kotlin

    // Return only camera configs that target 30 FPS camera capture frame rate.
    filter.targetFps = EnumSet.of(CameraConfig.TargetFps.TARGET_FPS_30)

  • 阻止 ARCore 使用深度传感器。在支持深度传感器的设备上,ARCore 会优先考虑使用深度传感器的摄像头配置。如需滤除所有使用深度传感器的相机配置,请使用 DepthSensorUsage.DO_NOT_USE 应用 setDepthSensorUsage 过滤器。

    Java

    // Return only camera configs that will not use the depth sensor.
    filter.setDepthSensorUsage(EnumSet.of(CameraConfig.DepthSensorUsage.DO_NOT_USE));

    Kotlin

    // Return only camera configs that will not use the depth sensor.
    filter.depthSensorUsage = EnumSet.of(CameraConfig.DepthSensorUsage.DO_NOT_USE)

  • 选择其他 GPU 纹理分辨率。在支持的设备上,ARCore 可能会提供额外的 GPU 纹理分辨率。选择分辨率较低的 GPU 纹理可以通过减少 GPU 负载和降低内存带宽要求来帮助提高应用性能,但不能保证在所有情况下都能提高性能。

使用相机配置过滤器

请按照以下步骤操作,让您的应用能够过滤相机配置。

Java

// Create a camera config filter for the session.
CameraConfigFilter filter = new CameraConfigFilter(session);

// Return only camera configs that target 30 fps camera capture frame rate.
filter.setTargetFps(EnumSet.of(CameraConfig.TargetFps.TARGET_FPS_30));

// Return only camera configs that will not use the depth sensor.
filter.setDepthSensorUsage(EnumSet.of(CameraConfig.DepthSensorUsage.DO_NOT_USE));

// Get list of configs that match filter settings.
// In this case, this list is guaranteed to contain at least one element,
// because both TargetFps.TARGET_FPS_30 and DepthSensorUsage.DO_NOT_USE
// are supported on all ARCore supported devices.
List<CameraConfig> cameraConfigList = session.getSupportedCameraConfigs(filter);

// Use element 0 from the list of returned camera configs. This is because
// it contains the camera config that best matches the specified filter
// settings.
session.setCameraConfig(cameraConfigList.get(0));

Kotlin

// Create a camera config filter for the session.
val filter = CameraConfigFilter(session)

// Return only camera configs that target 30 fps camera capture frame rate.
filter.targetFps = EnumSet.of(CameraConfig.TargetFps.TARGET_FPS_30)

// Return only camera configs that will not use the depth sensor.
filter.depthSensorUsage = EnumSet.of(CameraConfig.DepthSensorUsage.DO_NOT_USE)

// Get list of configs that match filter settings.
// In this case, this list is guaranteed to contain at least one element,
// because both TargetFps.TARGET_FPS_30 and DepthSensorUsage.DO_NOT_USE
// are supported on all ARCore supported devices.
val cameraConfigList = session.getSupportedCameraConfigs(filter)

// Use element 0 from the list of returned camera configs. This is because
// it contains the camera config that best matches the specified filter
// settings.
session.cameraConfig = cameraConfigList[0]

专注模式

您还可以在会话配置中设置专注模式。固定焦点通常更适合跟踪(并且是大多数设备上的 ARCore 默认设置)。自动对焦功能在录制、摄影、摄像以及需要对附近物体时都需要使用自动对焦功能。

如需了解详情,请参阅 Config.FocusMode