1# Camera Device Management (C/C++)
2
3Before developing a camera application, you must call the camera APIs to create an independent camera device.
4
5## How to Develop
6
7Read [Camera](../../reference/apis-camera-kit/_o_h___camera.md) for the API reference.
8
91. Import the NDK.
10
11   ```c++
12    // Include the NDK header files.
13    #include "hilog/log.h"
14    #include "ohcamera/camera.h"
15    #include "ohcamera/camera_input.h"
16    #include "ohcamera/capture_session.h"
17    #include "ohcamera/photo_output.h"
18    #include "ohcamera/preview_output.h"
19    #include "ohcamera/video_output.h"
20    #include "ohcamera/camera_manager.h"
21   ```
22
232. Link the dynamic library in the CMake script.
24
25   ```txt
26    target_link_libraries(entry PUBLIC libohcamera.so libhilog_ndk.z.so)
27   ```
28
293. Call [OH_Camera_GetCameraManager()](../../reference/apis-camera-kit/_o_h___camera.md#oh_camera_getcameramanager) to obtain a **cameraManager** object.
30
31   ```c++
32   Camera_Manager *cameraManager = nullptr;
33   Camera_Input* cameraInput = nullptr;
34   Camera_Device* cameras = nullptr;
35   uint32_t size = 0;
36   uint32_t cameraDeviceIndex = 0;
37   // Create a CameraManager object.
38   Camera_ErrorCode ret = OH_Camera_GetCameraManager(&cameraManager);
39   if (cameraManager == nullptr || ret != CAMERA_OK) {
40      OH_LOG_ERROR(LOG_APP, "OH_Camera_GetCameraManager failed.");
41   }
42   ```
43
44   > **NOTE**
45   >
46   > If obtaining the object fails, the camera device may be occupied or unusable. If it is occupied, wait until it is released.
47
484. Call [OH_CameraManager_GetSupportedCameras()](../../reference/apis-camera-kit/_o_h___camera.md#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.
49
50   ```c++
51   // Obtain the camera list.
52   ret = OH_CameraManager_GetSupportedCameras(cameraManager, &cameras, &size);
53   if (cameras == nullptr || size < 0 || ret != CAMERA_OK) {
54      OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameras failed.");
55   }
56   for (int index = 0; index < size; index++) {
57      OH_LOG_INFO(LOG_APP, "cameraId  =  %{public}s ", cameras[index].cameraId);              // Obtain the camera ID.
58      OH_LOG_INFO(LOG_APP, "cameraPosition  =  %{public}d ", cameras[index].cameraPosition);  // Obtain the camera position.
59      OH_LOG_INFO(LOG_APP, "cameraType  =  %{public}d ", cameras[index].cameraType);          // Obtain the camera type.
60      OH_LOG_INFO(LOG_APP, "connectionType  =  %{public}d ", cameras[index].connectionType);  // Obtain the camera connection type.
61   }
62   ```
63
64
65## Status Listening
66
67During 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.
68
69Register 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](../../reference/apis-camera-kit/_camera___status_info.md).
70
71   ```c++
72   ret = OH_CameraManager_RegisterCallback(cameraManager, GetCameraManagerListener());
73   if (ret != CAMERA_OK) {
74      OH_LOG_ERROR(LOG_APP, "OH_CameraManager_RegisterCallback failed.");
75   }
76   ```
77   ```c++
78   void CameraManagerStatusCallback(Camera_Manager* cameraManager, Camera_StatusInfo* status)
79   {
80      OH_LOG_INFO(LOG_APP, "CameraManagerStatusCallback is called");
81   }
82   CameraManager_Callbacks* GetCameraManagerListener()
83   {
84      static CameraManager_Callbacks cameraManagerListener = {
85         .onCameraStatus = CameraManagerStatusCallback
86      };
87      return &cameraManagerListener;
88   }
89   ```
90