目录

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:

How to Develop

For detailed API descriptions, refer to OH_Camera.

  1. 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"
  2. Link the dynamic library in the CMake script.

    target_link_libraries(entry PUBLIC
        libace_napi.z.so
        libohcamera.so
        libhilog_ndk.z.so
    )
  3. 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;
    }
  4. 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_IsTorchSupportedByTorchMode failed.");
        }
        if (torchModeSupported) {
             OH_LOG_INFO(LOG_APP, "IsTorchSupportedByTorchMode success.");
        } else {
             OH_LOG_ERROR(LOG_APP, "IsTorchSupportedByTorchMode failed. %{public}d ", ret);
        }
        return torchModeSupported;
    }
  5. 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. The callback returns the listening result and 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;
}