1# Using the Flashlight (C++) 2<!--Kit: Camera Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @qano--> 5<!--SE: @leo_ysl--> 6<!--TSE: @xchaosioda--> 7 8To use the flashlight mode, you manipulate your phone to turn on the flashlight, which then stays on persistently. 9 10When you use the flashlight mode with a camera application, the following situations may occur: 11 12- When the rear camera is used and [Camera_FlashMode](../../reference/apis-camera-kit/capi-camera-h.md#camera_flashmode) is set to **off**, the flashlight cannot be turned on. 13- When the front camera is used, the flashlight can be turned on and remains steady on. 14- When you switch from the front camera to the rear camera, the flashlight will be automatically turned off if it was turned on previously. 15 16## How to Develop 17 18Read [Camera](../../reference/apis-camera-kit/capi-oh-camera.md) for the API reference. 19 201. Import the NDK. 21 22 ```c++ 23 // Include the NDK header files. 24 #include "hilog/log.h" 25 #include "ohcamera/camera.h" 26 #include "ohcamera/camera_input.h" 27 #include "ohcamera/capture_session.h" 28 #include "ohcamera/camera_manager.h" 29 ``` 30 312. Link the dynamic library in the CMake script. 32 33 ```txt 34 target_link_libraries(entry PUBLIC 35 libace_napi.z.so 36 libohcamera.so 37 libhilog_ndk.z.so 38 ) 39 ``` 40 413. Call [OH_CameraManager_IsTorchSupported()](../../reference/apis-camera-kit/capi-camera-manager-h.md#oh_cameramanager_istorchsupported) to check whether the current device supports the flashlight. 42 43 ```c++ 44 bool IsTorchSupported(Camera_Manager* cameraManager) 45 { 46 // Check whether the device supports the flashlight. 47 bool isTorchSupported = false; 48 Camera_ErrorCode ret = OH_CameraManager_IsTorchSupported(cameraManager, &isTorchSupported); 49 if (cameraManager == nullptr || ret != CAMERA_OK) { 50 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_IsTorchSupported failed."); 51 } 52 if (isTorchSupported) { 53 OH_LOG_INFO(LOG_APP, "isTorchSupported success."); 54 } else { 55 OH_LOG_ERROR(LOG_APP, "isTorchSupported failed."); 56 } 57 return isTorchSupported; 58 } 59 60 ``` 61 624. Call [OH_CameraManager_IsTorchSupportedByTorchMode()](../../reference/apis-camera-kit/capi-camera-manager-h.md#oh_cameramanager_istorchsupportedbytorchmode) to check whether the current device supports a specific flashlight mode. 63 64 ```c++ 65 bool IsTorchSupportedByTorchMode(Camera_Manager* cameraManager, Camera_TorchMode torchMode) 66 { 67 bool torchModeSupported = false; 68 Camera_ErrorCode ret = OH_CameraManager_IsTorchSupportedByTorchMode(cameraManager, torchMode, &torchModeSupported); 69 if (cameraManager == nullptr || ret != CAMERA_OK) { 70 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_IsTorchSupported failed."); 71 } 72 if (torchModeSupported) { 73 OH_LOG_INFO(LOG_APP, "isTorchModeSupported success."); 74 } else { 75 OH_LOG_ERROR(LOG_APP, "isTorchModeSupported failed. %{public}d ", ret); 76 } 77 return torchModeSupported; 78 } 79 80 ``` 81 825. Call [OH_CameraManager_SetTorchMode()](../../reference/apis-camera-kit/capi-camera-manager-h.md#oh_cameramanager_settorchmode) to set the flashlight mode. 83 84 ```c++ 85 Camera_ErrorCode SetTorchMode(Camera_Manager* cameraManager, Camera_TorchMode torchMode) 86 { 87 // Set the flashlight mode when the specified mode is supported. 88 Camera_ErrorCode ret = OH_CameraManager_SetTorchMode(cameraManager, torchMode); 89 if (ret != CAMERA_OK) { 90 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_SetTorchMode failed. %{public}d ", ret); 91 } else { 92 OH_LOG_INFO(LOG_APP, "OH_CameraManager_SetTorchMode success."); 93 } 94 return ret; 95 } 96 ``` 97 98 99## Status Listening 100 101During camera application development, you can listen for changes of the flashlight status, including on, off, unavailable, and available, by using the callback function. 102 103 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](../../reference/apis-camera-kit/capi-oh-camera-camera-torchstatusinfo.md). 104 105 ```c++ 106 void TorchStatusCallback(Camera_Manager *cameraManager, Camera_TorchStatusInfo* torchStatus) 107 { 108 OH_LOG_INFO(LOG_APP, "TorchStatusCallback is called."); 109 } 110 Camera_ErrorCode RegisterTorchStatusCallback(Camera_Manager *cameraManager) 111 { 112 Camera_ErrorCode ret = OH_CameraManager_RegisterTorchStatusCallback(cameraManager, TorchStatusCallback); 113 if (ret != CAMERA_OK) { 114 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_RegisterTorchStatusCallback failed."); 115 } 116 return ret; 117 } 118 ``` 119