Use the device's flash unit on Android NDK (C)

Enabling the device's flash unit during an AR session can help improve visibility.

Check that the current camera configuration supports flash

Not all camera configurations support enabling a flash unit. Before enabling the flash or offering users the option to enable the flash, ensure that the flash unit is available for the active camera configuration:

// 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);

Enable the flash unit

Enable the flash unit by configuring the AR session with AR_FLASH_MODE_TORCH:

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);

Disable the flash unit

Disable the flash unit by configuring the AR session with 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);