• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Device Input Management (C/C++)
2<!--Kit: Camera Kit-->
3<!--Subsystem: Multimedia-->
4<!--Owner: @qano-->
5<!--SE: @leo_ysl-->
6<!--TSE: @xchaosioda-->
7
8Before developing a camera application, request permissions by following the instructions provided in [Requesting Camera Development Permissions](camera-preparation.md).
9
10A camera application invokes and controls a camera device to perform basic operations such as preview, photo capture, and video recording.
11
12## How to Develop
13
14Read [Camera](../../reference/apis-camera-kit/capi-oh-camera.md) for the API reference.
15
161. Import the NDK.
17
18   ```c++
19   // Include the NDK header files.
20   #include "hilog/log.h"
21   #include "ohcamera/camera.h"
22   #include "ohcamera/camera_input.h"
23   #include "ohcamera/capture_session.h"
24   #include "ohcamera/photo_output.h"
25   #include "ohcamera/preview_output.h"
26   #include "ohcamera/video_output.h"
27   #include "ohcamera/camera_manager.h"
28   ```
29
302. Link the dynamic library in the CMake script.
31
32   ```txt
33   target_link_libraries(entry PUBLIC
34       libace_napi.z.so
35       libohcamera.so
36       libhilog_ndk.z.so
37   )
38   ```
39
403. Call [OH_CameraManager_CreateCameraInput()](../../reference/apis-camera-kit/capi-camera-manager-h.md#oh_cameramanager_createcamerainput) to obtain a cameraInput object.
41   ```c++
42   // Listen for camera input errors.
43   void OnCameraInputError(const Camera_Input* cameraInput, Camera_ErrorCode errorCode)
44   {
45      OH_LOG_INFO(LOG_APP, "OnCameraInput errorCode: %{public}d", errorCode);
46   }
47
48   CameraInput_Callbacks* GetCameraInputListener(void)
49   {
50      static CameraInput_Callbacks cameraInputCallbacks = {
51         .onError = OnCameraInputError
52      };
53      return &cameraInputCallbacks;
54   }
55   ```
56   ```c++
57   // Listen for camera status information.
58   void CameraManagerStatusCallback(Camera_Manager* cameraManager, Camera_StatusInfo* status)
59   {
60       OH_LOG_INFO(LOG_APP, "CameraManagerStatusCallback is called");
61   }
62
63   CameraManager_Callbacks* GetCameraManagerListener()
64   {
65       static CameraManager_Callbacks cameraManagerListener = {
66           .onCameraStatus = CameraManagerStatusCallback
67       };
68       return &cameraManagerListener;
69   }
70   ```
71   ```c++
72   void CreateAndOpenCamera()
73   {
74       Camera_Manager* cameraManager = nullptr;
75       Camera_Input* cameraInput = nullptr;
76       Camera_Device* cameras = nullptr;
77       uint32_t size = 0;
78       uint32_t cameraDeviceIndex = 0;
79       // Create a CameraManager object.
80       Camera_ErrorCode ret = OH_Camera_GetCameraManager(&cameraManager);
81       if (cameraManager == nullptr || ret != CAMERA_OK) {
82           OH_LOG_ERROR(LOG_APP, "OH_Camera_GetCameraManager failed.");
83           return;
84       }
85       // Listen for camera status changes.
86       ret = OH_CameraManager_RegisterCallback(cameraManager, GetCameraManagerListener());
87       if (ret != CAMERA_OK) {
88           OH_LOG_ERROR(LOG_APP, "OH_CameraManager_RegisterCallback failed.");
89       }
90       // Obtain the camera list.
91        ret = OH_CameraManager_GetSupportedCameras(cameraManager, &cameras, &size);
92        if (cameras == nullptr || size < 0 || ret != CAMERA_OK) {
93            OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameras failed.");
94            return;
95        }
96       // Create a camera input stream.
97       ret = OH_CameraManager_CreateCameraInput(cameraManager, &cameras[cameraDeviceIndex], &cameraInput);
98       if (cameraInput == nullptr || ret != CAMERA_OK) {
99           OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateCameraInput failed.");
100           return;
101       }
102       ret = OH_CameraInput_RegisterCallback(cameraInput, GetCameraInputListener());
103       if (ret != CAMERA_OK) {
104           OH_LOG_ERROR(LOG_APP, "OH_CameraInput_RegisterCallback failed.");
105       }
106       // Open a camera.
107       ret = OH_CameraInput_Open(cameraInput);
108       if (ret != CAMERA_OK) {
109           OH_LOG_ERROR(LOG_APP, "OH_CameraInput_open failed.");
110           return;
111       }
112   }
113   ```
114
115   > **NOTE**
116   >
117   > Before any camera device input, you must complete camera management by following the instructions provided in [Camera Device Management](native-camera-device-management.md).
118
1194. Call [OH_CameraManager_GetSupportedSceneModes()](../../reference/apis-camera-kit/capi-camera-manager-h.md#oh_cameramanager_getsupportedscenemodes) to obtain the list of scene modes supported by the current camera device. The list stores all the [Camera_SceneMode](../../reference/apis-camera-kit/capi-camera-h.md#camera_scenemode) supported by the camera device.
120
121   ```c++
122   bool IsSupportedSceneMode(Camera_Device camera, Camera_SceneMode sceneMode)
123   {
124       Camera_SceneMode* sceneModes = nullptr;
125       uint32_t sceneModeSize = 0;
126       Camera_ErrorCode ret = OH_CameraManager_GetSupportedSceneModes(&camera, &sceneModes, &sceneModeSize);
127       if (sceneModes == nullptr || ret != CAMERA_OK) {
128           OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedSceneModes failed.");
129           return false;
130       }
131       for (int index = 0; index < sceneModeSize; index++) {
132           OH_LOG_INFO(LOG_APP, "scene mode = %{public}u ", sceneModes[index]);    // Obtain the specified scene mode.
133           if (sceneModes[index] == sceneMode) {
134               return true;
135           }
136       }
137       return false;
138   }
139   ```
140
1415. Call [OH_CameraManager_GetSupportedCameraOutputCapabilityWithSceneMode()](../../reference/apis-camera-kit/capi-camera-manager-h.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/capi-camera-h.md#camera_scenemode) specified by the camera device.
142
143
144   ```c++
145   Camera_OutputCapability* GetSupportedCameraOutputCapability(Camera_Manager* cameraManager, Camera_Device &camera)
146   {
147       Camera_OutputCapability* cameraOutputCapability = nullptr;
148       // The following sample code checks whether the NORMAL_PHOTO mode is supported by the camera.
149       bool isSupported = IsSupportedSceneMode(camera, Camera_SceneMode::NORMAL_PHOTO);
150       if (!isSupported) {
151           OH_LOG_ERROR(LOG_APP, "NORMAL_PHOTO is not supported.");
152           return cameraOutputCapability;
153       }
154       // Obtain the output stream capability supported by the camera.
155       const Camera_Profile* previewProfile = nullptr;
156       const Camera_Profile* photoProfile = nullptr;
157       Camera_ErrorCode ret = OH_CameraManager_GetSupportedCameraOutputCapabilityWithSceneMode(cameraManager, &camera,
158           Camera_SceneMode::NORMAL_PHOTO, &cameraOutputCapability);
159       if (cameraOutputCapability == nullptr || ret != CAMERA_OK) {
160           OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameraOutputCapability failed.");
161           return cameraOutputCapability;
162       }
163       // The following uses the NORMAL_PHOTO mode as an example. You need to add the preview stream and photo stream.
164       if (cameraOutputCapability->previewProfiles == nullptr) {
165           OH_LOG_ERROR(LOG_APP, "previewProfiles == null");
166       } else {
167           // Select an appropriate preview resolution from previewProfiles of cameraOutputCapability.
168           previewProfile = cameraOutputCapability->previewProfiles[0];
169       }
170       if (cameraOutputCapability->photoProfiles == nullptr) {
171           OH_LOG_ERROR(LOG_APP, "photoProfiles == null");
172       } else {
173           // Select an appropriate photo resolution from photoProfiles of cameraOutputCapability.
174           photoProfile = cameraOutputCapability->photoProfiles[0];
175       }
176       return cameraOutputCapability;
177   }
178   ```
179