harmony 鸿蒙Camera Device Management (C/C++)

  • 2025-06-12
  • 浏览 (4)

Camera Device Management (C/C++)

Before developing a camera application, you must call the camera APIs to create an independent camera device.

How to Develop

Read Camera for the API reference.

  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/photo_output.h"
   #include "ohcamera/preview_output.h"
   #include "ohcamera/video_output.h"
   #include "ohcamera/camera_manager.h"
  1. Link the dynamic library in the CMake script.
   target_link_libraries(entry PUBLIC
       libace_napi.z.so
       libohcamera.so
       libhilog_ndk.z.so
   )
  1. Call OH_Camera_GetCameraManager() to obtain a cameraManager object.
   Camera_ErrorCode CreateCameraManager(Camera_Manager** cameraManager)
   {
       // Create a CameraManager object.
       Camera_ErrorCode ret = OH_Camera_GetCameraManager(cameraManager);
       if (cameraManager == nullptr||ret != CAMERA_OK) {
          OH_LOG_ERROR(LOG_APP, "OH_Camera_GetCameraManager failed.");
       }
       return ret;
   }

NOTE

If obtaining the object fails, the camera device may be occupied or unusable. If it is occupied, wait until it is released.

  1. Call OH_CameraManager_GetSupportedCameras() to obtain the list of cameras supported by the current device. The list stores the IDs of all cameras supported. If the list is not empty, each ID in the list can be used to create an independent camera object. If the list is empty, no camera is available for the current device and subsequent operations cannot be performed.
   Camera_ErrorCode GetSupportedCameras(Camera_Manager* cameraManager, Camera_Device** cameras, uint32_t &size)
   {
       // Obtain the camera list.
       Camera_ErrorCode ret = OH_CameraManager_GetSupportedCameras(cameraManager, cameras, &size);
       if (cameras == nullptr||size < 0||ret != CAMERA_OK) {
          OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameras failed.");
       }
       for (int index = 0; index < size; index++) {
          OH_LOG_INFO(LOG_APP, "cameraId  =  %{public}s ", (*cameras)[index].cameraId);              // Obtain the camera ID.
          OH_LOG_INFO(LOG_APP, "cameraPosition  =  %{public}d ", (*cameras)[index].cameraPosition);  // Obtain the camera position.
          OH_LOG_INFO(LOG_APP, "cameraType  =  %{public}d ", (*cameras)[index].cameraType);          // Obtain the camera type.
          OH_LOG_INFO(LOG_APP, "connectionType  =  %{public}d ", (*cameras)[index].connectionType);  // Obtain the camera connection type.
       }
       return ret;
   }

Status Listening

During camera application development, you can listen for the camera status, including the appearance of a new camera, removal of a camera, and availability of a camera. The camera ID and camera status are included in the callback function. When a new camera appears, the new camera can be added to the supported camera list.

Register the ‘cameraStatus’ event and return the listening result through a callback, which carries the Camera_StatusInfo parameter. For details about the parameter, see Camera_StatusInfo.

  void CameraStatusCallback(Camera_Manager* cameraManager, Camera_StatusInfo* status)
  {
     OH_LOG_INFO(LOG_APP, "CameraStatusCallback is called");
  }
  CameraManager_Callbacks* GetCameraManagerListener()
  {
     static CameraManager_Callbacks cameraManagerListener = {
        .onCameraStatus = CameraStatusCallback
     };
     return &cameraManagerListener;
  }
  Camera_ErrorCode RegisterCameraStatusCallback(Camera_Manager &cameraManager)
  {
      Camera_ErrorCode ret = OH_CameraManager_RegisterCallback(&cameraManager, GetCameraManagerListener());
      if (ret != CAMERA_OK) {
         OH_LOG_ERROR(LOG_APP, "OH_CameraManager_RegisterCallback failed.");
      }
      return ret;
  }

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Camera Kit

harmony 鸿蒙Basic Camera Animation (ArkTS)

harmony 鸿蒙Practices for Camera Recovery from the Background (ArkTS)

harmony 鸿蒙Deferred Photo Delivery Practices (ArkTS)

harmony 鸿蒙Deferred Photo Delivery (ArkTS)

harmony 鸿蒙Practices for High-Performance Photo Capture (for System Applications Only) (ArkTS)

harmony 鸿蒙High-Performance Photo Capture (for System Applications Only) (ArkTS)

harmony 鸿蒙Depth Data (for System Applications Only) (ArkTS)

harmony 鸿蒙Device Input Management (ArkTS)

harmony 鸿蒙Camera Device Management (ArkTS)

0  赞