1# Using the Flashlight (C++) 2 3To use the flashlight mode, you manipulate your phone to turn on the flashlight, which then stays on persistently. 4 5When you use the flashlight mode with a camera application, the following situations may occur: 6- When the rear camera is used and [Camera_FlashMode](../../reference/apis-camera-kit/_o_h___camera.md#camera_flashmode) is set to off, the flashlight cannot be turned on. 7- When the front camera is used, the flashlight can be turned on and remains steady on. 8- When you switch from the front camera to the rear camera, the flashlight will be automatically turned off if it was turned on previously. 9 10## How to Develop 11 12Read [Camera](../../reference/apis-camera-kit/_o_h___camera.md) for the API reference. 13 141. Import the NDK. 15 16 ```c++ 17 // Include the NDK header files. 18 #include "hilog/log.h" 19 #include "ohcamera/camera.h" 20 #include "ohcamera/camera_input.h" 21 #include "ohcamera/capture_session.h" 22 #include "ohcamera/camera_manager.h" 23 ``` 24 252. Link the dynamic library in the CMake script. 26 27 ```txt 28 target_link_libraies(entry PUBLIC libohcamera.so libhilog_ndk.z.so) 29 ``` 30 313. Call [OH_CameraManager_IsTorchSupported()](../../reference/apis-camera-kit/_o_h___camera.md#oh_cameramanager_istorchsupported) to check whether the current device supports the flashlight. 32 33 ```c++ 34 // Check whether the device supports the flashlight. 35 Camera_OutputCapability* cameraOutputCapability = nullptr; 36 Camera_TorchMode torchMode = AUTO; 37 bool isTorchSupported = false; 38 Camera_ErrorCode ret = OH_CameraManager_IsTorchSupported(cameraManager, &isTorchSupported); 39 if (cameraManager == nullptr || ret != CAMERA_OK) { 40 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_IsTorchSupported failed."); 41 } 42 if (isTorchSupported) { 43 OH_LOG_INFO(LOG_APP, "isTorchSupported success."); 44 } else { 45 OH_LOG_ERROR(LOG_APP, "isTorchSupported failed."); 46 } 47 ``` 48 494. Call [OH_CameraManager_IsTorchSupportedByTorchMode()](../../reference/apis-camera-kit/_o_h___camera.md#oh_cameramanager_istorchsupportedbytorchmode) to check whether the current device supports a specific flashlight mode. 50 51 ```c++ 52 bool torchModeSupported = false; 53 ret = OH_CameraManager_IsTorchSupportedByTorchMode(cameraManager, torchMode, &torchModeSupported); 54 if (cameraManager == nullptr || ret != CAMERA_OK) { 55 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_IsTorchSupported failed."); 56 } 57 if (torchModeSupported) { 58 OH_LOG_INFO(LOG_APP, "isTorchModeSupported success."); 59 } else { 60 OH_LOG_ERROR(LOG_APP, "isTorchModeSupported failed. %{public}d ", ret); 61 } 62 ``` 63 645. Call [OH_CameraManager_SetTorchMode()](../../reference/apis-camera-kit/_o_h___camera.md#oh_cameramanager_settorchmode) to set the flashlight mode. 65 66 ```c++ 67 if (torchModeSupported) { 68 OH_LOG_INFO(LOG_APP, "OH_CameraManager_IsTorchModeSupported success."); 69 // Set the flashlight mode. 70 ret = OH_CameraManager_SetTorchMode(cameraManager, torchMode); 71 if (ret != CAMERA_OK) { 72 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_SetTorchMode failed. %{public}d ", ret); 73 } else { 74 OH_LOG_INFO(LOG_APP, "OH_CameraManager_SetTorchMode success."); 75 } 76 } else { 77 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_IsTorchModeSupported failed."); 78 } 79 ``` 80 81 82## Status Listening 83 84During camera application development, you can listen for changes of the flashlight status, including on, off, unavailable, and available, by using the callback function. 85 86 Register the **'cameratorchStatusChange'** event and return the listening result through a callback, which carries the **Camera_TorchStatusInfo** parameter. For details about the parameter, see [Camera_TorchStatusInfo](../../reference/apis-camera-kit/_camera___torch_status_info.md). 87 88 ```c++ 89 ret = OH_CameraManager_RegisterTorchStatusCallback(cameraManager, GetTorchStatusCb); 90 if (ret != CAMERA_OK) { 91 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_RegisterTorchStatusCallback failed."); 92 } 93 ``` 94 ```c++ 95 void OnTorchStatusChange(Camera_Manager *cameraManager, Camera_TorchStatusInfo* torchStatus) 96 { 97 OH_LOG_INFO(LOG_APP, "OH_CameraManager_RegisterTorchStatusCallback is called."); 98 } 99 void GetTorchStatusCb(Camera_Manager *cameraManager, Camera_TorchStatusInfo* torchStatus) 100 { 101 OH_LOG_INFO(LOG_APP, "GetTorchStatusCb is called."); 102 } 103 ``` 104