1# Device Input Management (C/C++) 2 3Before developing a camera application, request permissions by following the instructions provided in [Camera Development Preparations](camera-preparation.md). 4 5A camera application invokes and controls a camera device to perform basic operations such as preview, photo capture, and video recording. 6 7## How to Develop 8 9Read [Camera](../../reference/apis-camera-kit/_o_h___camera.md) for the API reference. 10 111. Import the NDK. 12 13 ```c++ 14 // Include the NDK header files. 15 #include "hilog/log.h" 16 #include "ohcamera/camera.h" 17 #include "ohcamera/camera_input.h" 18 #include "ohcamera/capture_session.h" 19 #include "ohcamera/photo_output.h" 20 #include "ohcamera/preview_output.h" 21 #include "ohcamera/video_output.h" 22 #include "ohcamera/camera_manager.h" 23 ``` 24 252. Link the dynamic library in the CMake script. 26 27 ```txt 28 target_link_libraries(entry PUBLIC libohcamera.so libhilog_ndk.z.so) 29 ``` 30 313. Call [OH_CameraManager_CreateCameraInput()](../../reference/apis-camera-kit/_o_h___camera.md#oh_cameramanager_createcamerainput) to obtain a **cameraInput** object. 32 33 ```c++ 34 Camera_Manager *cameraManager = nullptr; 35 Camera_Input* cameraInput = nullptr; 36 Camera_Device* cameras = nullptr; 37 Camera_OutputCapability* cameraOutputCapability = nullptr; 38 const Camera_Profile* previewProfile = nullptr; 39 const Camera_Profile* photoProfile = nullptr; 40 uint32_t size = 0; 41 uint32_t cameraDeviceIndex = 0; 42 // Create a CameraManager object. 43 Camera_ErrorCode ret = OH_Camera_GetCameraManager(&cameraManager); 44 if (cameraManager == nullptr || ret != CAMERA_OK) { 45 OH_LOG_ERROR(LOG_APP, "OH_Camera_GetCameraMananger failed."); 46 } 47 // Listen for camera status changes. 48 ret = OH_CameraManager_RegisterCallback(cameraManager, GetCameraManagerListener()); 49 if (ret != CAMERA_OK) { 50 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_RegisterCallback failed."); 51 } 52 // Obtain the camera list. 53 ret = OH_CameraManager_GetSupportedCameras(cameraManager, &cameras, &size); 54 if (cameras == nullptr || size < 0 || ret != CAMERA_OK) { 55 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameras failed."); 56 } 57 // Create a camera input stream. 58 ret = OH_CameraManager_CreateCameraInput(cameraManager, &cameras[cameraDeviceIndex], &cameraInput); 59 if (cameraInput == nullptr || ret != CAMERA_OK) { 60 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateCameraInput failed."); 61 } 62 ret == OH_CameraInput_RegisterCallback(cameraInput, GetCameraInputListener()); 63 if (ret != CAMERA_OK) { 64 OH_LOG_ERROR(LOG_APP, "OH_CameraInput_RegisterCallback failed."); 65 } 66 // Open the camera. 67 ret = OH_CameraInput_Open(cameraInput); 68 if (ret != CAMERA_OK) { 69 OH_LOG_ERROR(LOG_APP, "OH_CameraInput_open failed."); 70 } 71 ``` 72 ```c++ 73 // Listen for camera input errors. 74 void OnCameraInputError(const Camera_Input* cameraInput, Camera_ErrorCode errorCode) 75 { 76 OH_LOG_INFO(LOG_APP, "OnCameraInput errorCode: %{public}d", errorCode); 77 } 78 79 CameraInput_Callbacks* GetCameraInputListener(void) 80 { 81 static CameraInput_Callbacks cameraInputCallbacks = { 82 .onError = OnCameraInputError 83 }; 84 return &cameraInputCallbacks; 85 } 86 ``` 87 88 > **NOTE** 89 > 90 > Before any camera device input, you must complete camera management by following the instructions provided in [Camera Device Management](native-camera-device-management.md). 91 924. Call [OH_CameraManager_GetSupportedSceneModes()](../../reference/apis-camera-kit/_o_h___camera.md#oh_cameramanager_getsupportedscenemodes) to obtain the list of scene modes supported by the current camera device. The list stores all the [Camera_SceneModes](../../reference/apis-camera-kit/_o_h___camera.md#camera_scenemode) supported by the camera device. 93 94 ```c++ 95 Camera_SceneMode* sceneModes = nullptr; 96 uint32_t length = 0; 97 uint32_t sceneModeIndex = 0; 98 ret = OH_CameraManager_GetSupportedSceneModes(&cameras[cameraDeviceIndex], &sceneModes, &length); 99 if (sceneModes == nullptr || ret != CAMERA_OK) { 100 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedSceneModes failed."); 101 } 102 for (int index = 0; index < length; index++) { 103 OH_LOG_INFO(LOG_APP, "scene mode = %{public}s ", sceneModes[index]); // Obtain the specified scene mode. 104 } 105 ``` 106 1075. Call [OH_CameraManager_GetSupportedCameraOutputCapabilityWithSceneMode()](../../reference/apis-camera-kit/_o_h___camera.md#oh_cameramanager_getsupportedcameraoutputcapabilitywithscenemode) 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**. Different types of output streams must be added based on the value of [Camera_SceneMode](../../reference/apis-camera-kit/_o_h___camera.md#camera_scenemode) specified by the camera device. 108 109 110 ```c++ 111 // Obtain the output streams supported by the camera device. 112 Camera_OutputCapability* cameraOutputCapability = nullptr; 113 const Camera_Profile* previewProfile = nullptr; 114 const Camera_Profile* photoProfile = nullptr; 115 ret = OH_CameraManager_GetSupportedCameraOutputCapabilityWithSceneMode(cameraManager, &cameras[cameraDeviceIndex], sceneModes[sceneModeIndex] 116 &cameraOutputCapability); 117 if (cameraOutputCapability == nullptr || ret != CAMERA_OK) { 118 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameraOutputCapability failed."); 119 } 120 // The following uses the NORMAL_PHOTO mode as an example. You need to add the preview stream and photo stream. 121 if (cameraOutputCapability->previewProfilesSize < 0) { 122 OH_LOG_ERROR(LOG_APP, "previewProfilesSize == null"); 123 } 124 previewProfile = cameraOutputCapability->previewProfiles[0]; 125 126 if (cameraOutputCapability->photoProfilesSize < 0) { 127 OH_LOG_ERROR(LOG_APP, "photoProfilesSize == null"); 128 } 129 photoProfile = cameraOutputCapability->photoProfiles[0]; 130 ``` 131