1# Device Input Management (C/C++) 2 3Before developing a camera application, you must create an independent camera object. The application invokes and controls the camera object to perform basic operations such as preview, photographing, and video recording. 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()** to obtain a **CameraManager** object. 30 31 ```c++ 32 Camera_Manager *cameraManager = nullptr; 33 Camera_Input* cameraInput = nullptr; 34 Camera_Device* cameras = nullptr; 35 Camera_OutputCapability* cameraOutputCapability = nullptr; 36 const Camera_Profile* previewProfile = nullptr; 37 const Camera_Profile* photoProfile = nullptr; 38 uint32_t size = 0; 39 uint32_t cameraDeviceIndex = 0; 40 // Create a CameraManager object. 41 Camera_ErrorCode ret = OH_Camera_GetCameraManager(&cameraManager); 42 if (cameraManager == nullptr || ret != CAMERA_OK) { 43 OH_LOG_ERROR(LOG_APP, "OH_Camera_GetCameraManager failed."); 44 } 45 ``` 46 47 > **NOTE** 48 > 49 > If obtaining the object fails, the camera hardware may be occupied or unusable. If it is occupied, wait until it is released. 50 514. 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. 52 53 ```c++ 54 // Obtain the camera list. 55 ret = OH_CameraManager_GetSupportedCameras(cameraManager, &cameras, &size); 56 if (cameras == nullptr || size < 0 || ret != CAMERA_OK) { 57 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameras failed."); 58 } 59 for (int index = 0; index < size; index++) { 60 OH_LOG_ERROR(LOG_APP, "cameraId = %{public}s ", cameras[index].cameraId); // Obtain the camera ID. 61 OH_LOG_ERROR(LOG_APP, "cameraPosition = %{public}d ", cameras[index].cameraPosition); // Obtain the camera position. 62 OH_LOG_ERROR(LOG_APP, "cameraType = %{public}d ", cameras[index].cameraType); // Obtain the camera type. 63 OH_LOG_ERROR(LOG_APP, "connectionType = %{public}d ", cameras[index].connectionType); // Obtain the camera connection type. 64 } 65 ``` 66 675. Call **OH_CameraManager_GetSupportedCameraOutputCapability()** to obtain all output streams supported by the current device, such as preview streams and photo streams. The output streams supported are the value of each **profile** field under **CameraOutputCapability**. 68 69 ```c++ 70 // Create a camera input stream. 71 ret = OH_CameraManager_CreateCameraInput(cameraManager, &cameras[cameraDeviceIndex], &cameraInput); 72 if (cameraInput == nullptr || ret != CAMERA_OK) { 73 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateCameraInput failed."); 74 } 75 ret = OH_CameraInput_RegisterCallback(cameraInput, GetCameraInputListener()); 76 if (ret != CAMERA_OK) { 77 OH_LOG_ERROR(LOG_APP, "OH_CameraInput_RegisterCallback failed."); 78 } 79 // Open the camera. 80 ret = OH_CameraInput_Open(cameraInput); 81 if (ret != CAMERA_OK) { 82 OH_LOG_ERROR(LOG_APP, "OH_CameraInput_Open failed."); 83 } 84 // Obtain the output streams supported by the camera. 85 ret = OH_CameraManager_GetSupportedCameraOutputCapability(cameraManager, &cameras[cameraDeviceIndex], 86 &cameraOutputCapability); 87 if (cameraOutputCapability == nullptr || ret != CAMERA_OK) { 88 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameraOutputCapability failed."); 89 } 90 91 if (cameraOutputCapability->previewProfilesSize < 0) { 92 OH_LOG_ERROR(LOG_APP, "previewProfilesSize == null"); 93 } 94 previewProfile = cameraOutputCapability->previewProfiles[0]; 95 96 if (cameraOutputCapability->photoProfilesSize < 0) { 97 OH_LOG_ERROR(LOG_APP, "photoProfilesSize == null"); 98 } 99 photoProfile = cameraOutputCapability->photoProfiles[0]; 100 ``` 101 ```c++ 102 // Listen for camera input errors. 103 void OnCameraInputError(const Camera_Input* cameraInput, Camera_ErrorCode errorCode) 104 { 105 OH_LOG_INFO(LOG_APP, "OnCameraInput errorCode = %{public}d", errorCode); 106 } 107 108 CameraInput_Callbacks* GetCameraInputListener(void) 109 { 110 static CameraInput_Callbacks cameraInputCallbacks = { 111 .onError = OnCameraInputError 112 }; 113 return &cameraInputCallbacks; 114 } 115 ``` 116 117## Status Listening 118 119During 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. 120 121 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](../reference/apis-camera-kit/_camera___status_info.md). 122 123 ```c++ 124 ret = OH_CameraManager_RegisterCallback(cameraManager, GetCameraManagerListener()); 125 if (ret != CAMERA_OK) { 126 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_RegisterCallback failed."); 127 } 128 ``` 129 ```c++ 130 void CameraManagerStatusCallback(Camera_Manager* cameraManager, Camera_StatusInfo* status) 131 { 132 OH_LOG_INFO(LOG_APP, "CameraManagerStatusCallback is called"); 133 } 134 CameraManager_Callbacks* GetCameraManagerListener() 135 { 136 static CameraManager_Callbacks cameraManagerListener = { 137 .onCameraStatus = CameraManagerStatusCallback 138 }; 139 return &cameraManagerListener; 140 } 141 ``` 142