在 Android NDK (C) 上使用设备的闪存单元

在 AR 会话期间启用设备的闪光灯单元有助于提高可见度。

检查当前的相机配置是否支持闪光灯

并非所有相机配置都支持启用闪光灯。 在启用闪光灯或向用户提供启用闪光灯的选项之前,请确保闪光灯单元适用于当前的相机配置:

// Get the camera ID from the current session.
ArCameraConfig* ar_camera_config = NULL;
ArSession_getCameraConfig(ar_session, ar_camera_config);
char* camera_id = NULL;
ArCameraConfig_getCameraId(ar_session, ar_camera_config, &camera_id);
ArCameraConfig_destroy(ar_camera_config);

// Get the camera characteristics.
ACameraManager* camera_manager = ACameraManager_create();
CHECK(camera_manager != NULL);
ACameraMetadata* characteristics = NULL;
camera_status_t status = ACameraManager_getCameraCharacteristics(camera_manager, camera_id, &characteristics);
ArString_release(camera_id);
CHECK(status == ACAMERA_OK);
CHECK(characteristics != NULL);

// Check if flash is supported.
ACameraMetadata_const_entry entry;
status = ACameraMetadata_getConstEntry(characteristics, ACAMERA_FLASH_INFO_AVAILABLE, &entry);
CHECK(status == ACAMERA_OK);
uint32_t is_flash_supported = false;
if (entry.count == 1 && entry.data.u8[0] == ACAMERA_FLASH_INFO_AVAILABLE_TRUE) {
  is_flash_supported = true;
}

ACameraManager_delete(camera_manager);

启用闪光灯装置

使用 AR_FLASH_MODE_TORCH 配置 AR 会话,以启用闪光灯单元:

ArConfig* ar_config = NULL;
ArSession_getConfig(ar_session, ar_config);
if (is_flash_supported) {
  ArConfig_setFlashMode(ar_session, ar_config, AR_FLASH_MODE_TORCH);
}
CHECK(ArSession_configure(ar_session, ar_config) == AR_SUCCESS);
ArConfig_destroy(ar_config);

停用闪光灯

通过配置 AR 会话来停用闪存装置 AR_FLASH_MODE_OFF:

ArConfig* ar_config = NULL;
ArSession_getConfig(ar_session, ar_config);
if (is_flash_supported) {
  ArConfig_setFlashMode(ar_session, ar_config, AR_FLASH_MODE_OFF);
}
CHECK(ArSession_configure(ar_session, ar_config) == AR_SUCCESS);
ArConfig_destroy(ar_config);