Using the Flashlight (C++)
To use the flashlight mode, you manipulate your device to turn on the flashlight, which then stays on persistently.
When you use the flashlight mode with a camera application, the following situations may occur:
- When the rear camera is used and Camera_FlashMode is set to off, the flashlight cannot be turned on.
- When the front camera is used, the flashlight can be turned on and remains steady on.
- When you switch from the front camera to the rear camera, the flashlight will be automatically turned off if it was turned on previously.
How to Develop
Read Camera for the API reference.
-
Import the NDK.
// Include the NDK header files. #include "hilog/log.h" #include "ohcamera/camera.h" #include "ohcamera/camera_input.h" #include "ohcamera/capture_session.h" #include "ohcamera/camera_manager.h" -
Link the dynamic library in the CMake script.
target_link_libraries(entry PUBLIC libace_napi.z.so libohcamera.so libhilog_ndk.z.so ) -
Call OH_CameraManager_IsTorchSupported() to check whether the current device supports the flashlight.
bool IsTorchSupported(Camera_Manager* cameraManager) { // Check whether the device supports the flashlight. bool isTorchSupported = false; if (cameraManager == nullptr) { OH_LOG_ERROR(LOG_APP, "cameraManager is nullptr."); return isTorchSupported; } Camera_ErrorCode ret = OH_CameraManager_IsTorchSupported(cameraManager, &isTorchSupported); if (ret != CAMERA_OK) { OH_LOG_ERROR(LOG_APP, "OH_CameraManager_IsTorchSupported failed."); } if (isTorchSupported) { OH_LOG_INFO(LOG_APP, "isTorchSupported success."); } else { OH_LOG_ERROR(LOG_APP, "isTorchSupported failed."); } return isTorchSupported; } -
Call OH_CameraManager_IsTorchSupportedByTorchMode() to check whether the current device supports a specific flashlight mode.
bool IsTorchSupportedByTorchMode(Camera_Manager* cameraManager, Camera_TorchMode torchMode) { bool torchModeSupported = false; Camera_ErrorCode ret = OH_CameraManager_IsTorchSupportedByTorchMode(cameraManager, torchMode, &torchModeSupported); if (ret != CAMERA_OK) { OH_LOG_ERROR(LOG_APP, "OH_CameraManager_IsTorchSupported failed."); } if (torchModeSupported) { OH_LOG_INFO(LOG_APP, "isTorchModeSupported success."); } else { OH_LOG_ERROR(LOG_APP, "isTorchModeSupported failed. %{public}d ", ret); } return torchModeSupported; } -
Call OH_CameraManager_SetTorchMode() to set the flashlight mode.
Camera_ErrorCode SetTorchMode(Camera_Manager* cameraManager, Camera_TorchMode torchMode) { // Set the flashlight mode when the specified mode is supported. Camera_ErrorCode ret = OH_CameraManager_SetTorchMode(cameraManager, torchMode); if (ret != CAMERA_OK) { OH_LOG_ERROR(LOG_APP, "OH_CameraManager_SetTorchMode failed. %{public}d ", ret); } else { OH_LOG_INFO(LOG_APP, "OH_CameraManager_SetTorchMode success."); } return ret; }
Status Listening
During camera application development, you can listen for changes of the flashlight status, including on, off, unavailable, and available, by using the callback function.
Register the torchStatus event and return the listening result through a callback, which carries the Camera_TorchStatusInfo parameter. For details about the parameter, see Camera_TorchStatusInfo.
void TorchStatusCallback(Camera_Manager *cameraManager, Camera_TorchStatusInfo* torchStatus)
{
OH_LOG_INFO(LOG_APP, "TorchStatusCallback is called.");
}
Camera_ErrorCode RegisterTorchStatusCallback(Camera_Manager *cameraManager)
{
Camera_ErrorCode ret = OH_CameraManager_RegisterTorchStatusCallback(cameraManager, TorchStatusCallback);
if (ret != CAMERA_OK) {
OH_LOG_ERROR(LOG_APP, "OH_CameraManager_RegisterTorchStatusCallback failed.");
}
return ret;
}