設定相機

CameraConfig 會說明基礎相機感應器的屬性,包括:

  • 攝影機 ID
  • 在適用情況下,是否要使用深度感應器
  • 相機朝向的方向:
    • 前置鏡頭 (自拍)
    • 後置 (世界)
  • FPS (每秒影格數) 範圍
  • CPU 圖片尺寸
  • GPU 紋理尺寸
  • 如果有設定,是否要使用裝置的多鏡頭攝影機

建立新的 ARCore 工作階段時,ARCore 會使用 setCameraConfig 設定最符合 getSupportedCameraConfigs(CameraConfigFilter) 傳回的可用設定清單的相機設定。您的應用程式可以使用 CameraConfigFilter,根據應用程式需求進行篩選,藉此在執行階段縮小指定裝置可用的相機設定。

常見的篩選用途包括:

  • 將相機擷取畫面更新率限制為 30 fps。在支援 60 fps 的裝置上,ARCore 會優先採用支援該影格速率的相機設定。如要篩除支援 60 fps 的所有相機設定,請使用 TargetFps.TARGET_FPS_30setTargetFps 套用濾鏡。

    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