配置相机

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

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

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

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

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

    // Return only camera configs that target 30 FPS camera capture frame
    // rate.
    ArCameraConfigFilter_setTargetFps(session, filter,
                                      AR_CAMERA_CONFIG_TARGET_FPS_30);

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

    ArCameraConfigFilter_setDepthSensorUsage(
        session, filter, AR_CAMERA_CONFIG_DEPTH_SENSOR_USAGE_DO_NOT_USE);

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

使用相机配置过滤器

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

// Create an ARCore session.
ArSession* session;
ArSession_create(env, context, &session);

// Create a camera config list and filter for the session.
ArCameraConfig* selected_config;
ArCameraConfigList* configs;
ArCameraConfigFilter* filter;
ArCameraConfig_create(session, &selected_config);
ArCameraConfigList_create(session, &configs);
ArCameraConfigFilter_create(session, &filter);

// Return only camera configs that target 30 fps camera capture frame rate.
ArCameraConfigFilter_setTargetFps(session, filter,
                                  AR_CAMERA_CONFIG_TARGET_FPS_30);

// Return only camera configs that will not use the depth sensor.
ArCameraConfigFilter_setDepthSensorUsage(
    session, filter, AR_CAMERA_CONFIG_DEPTH_SENSOR_USAGE_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.
ArSession_getSupportedCameraConfigsWithFilter(session, filter, configs);

// 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.
ArCameraConfigList_getItem(session, configs, 0, selected_config);

// Set the camera config to use selected_config.
ArSession_setCameraConfig(session, selected_config);

// Free memory.
ArCameraConfigFilter_destroy(filter);
ArCameraConfigList_destroy(configs);

专注模式

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

如需了解详情,请参阅 ArConfig_setFocusMode()