• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 设备输入(C/C++)
2
3在开发相机应用时,需要先参考开发准备[申请相关权限](camera-preparation.md)。
4
5相机应用通过调用和控制相机设备,完成预览、拍照和录像等基础操作。
6
7## 开发步骤
8
9详细的API说明请参考[Camera API参考](../../reference/apis-camera-kit/_o_h___camera.md)。
10
111. 导入NDK接口。选择系统提供的NDK接口能力,导入NDK接口的方法如下。
12
13   ```c++
14   // 导入NDK接口头文件。
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. 在CMake脚本中链接相关动态库。
26
27   ```txt
28   target_link_libraries(entry PUBLIC
29       libace_napi.z.so
30       libohcamera.so
31       libhilog_ndk.z.so
32   )
33   ```
34
353. 通过[OH_CameraManager_CreateCameraInput()](../../reference/apis-camera-kit/_o_h___camera.md#oh_cameramanager_createcamerainput)方法,获取cameraInput对象。
36   ```c++
37   // 监听cameraInput错误信息。
38   void OnCameraInputError(const Camera_Input* cameraInput, Camera_ErrorCode errorCode)
39   {
40      OH_LOG_INFO(LOG_APP, "OnCameraInput errorCode: %{public}d", errorCode);
41   }
42
43   CameraInput_Callbacks* GetCameraInputListener(void)
44   {
45      static CameraInput_Callbacks cameraInputCallbacks = {
46         .onError = OnCameraInputError
47      };
48      return &cameraInputCallbacks;
49   }
50   ```
51   ```c++
52   // 监听cameraStatus信息。
53   void CameraManagerStatusCallback(Camera_Manager* cameraManager, Camera_StatusInfo* status)
54   {
55       OH_LOG_INFO(LOG_APP, "CameraManagerStatusCallback is called");
56   }
57
58   CameraManager_Callbacks* GetCameraManagerListener()
59   {
60       static CameraManager_Callbacks cameraManagerListener = {
61           .onCameraStatus = CameraManagerStatusCallback
62       };
63       return &cameraManagerListener;
64   }
65   ```
66   ```c++
67   void CreateAndOpenCamera()
68   {
69       Camera_Manager* cameraManager = nullptr;
70       Camera_Input* cameraInput = nullptr;
71       Camera_Device* cameras = nullptr;
72       uint32_t size = 0;
73       uint32_t cameraDeviceIndex = 0;
74       // 创建CameraManager对象。
75       Camera_ErrorCode ret = OH_Camera_GetCameraManager(&cameraManager);
76       if (cameraManager == nullptr || ret != CAMERA_OK) {
77           OH_LOG_ERROR(LOG_APP, "OH_Camera_GetCameraManager failed.");
78           return;
79       }
80       // 监听相机状态变化。
81       ret = OH_CameraManager_RegisterCallback(cameraManager, GetCameraManagerListener());
82       if (ret != CAMERA_OK) {
83           OH_LOG_ERROR(LOG_APP, "OH_CameraManager_RegisterCallback failed.");
84       }
85       // 获取相机列表。
86        ret = OH_CameraManager_GetSupportedCameras(cameraManager, &cameras, &size);
87        if (cameras == nullptr || size < 0 || ret != CAMERA_OK) {
88            OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameras failed.");
89            return;
90        }
91       // 创建相机输入流。
92       ret = OH_CameraManager_CreateCameraInput(cameraManager, &cameras[cameraDeviceIndex], &cameraInput);
93       if (cameraInput == nullptr || ret != CAMERA_OK) {
94           OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateCameraInput failed.");
95           return;
96       }
97       ret = OH_CameraInput_RegisterCallback(cameraInput, GetCameraInputListener());
98       if (ret != CAMERA_OK) {
99           OH_LOG_ERROR(LOG_APP, "OH_CameraInput_RegisterCallback failed.");
100       }
101       // 打开相机。
102       ret = OH_CameraInput_Open(cameraInput);
103       if (ret != CAMERA_OK) {
104           OH_LOG_ERROR(LOG_APP, "OH_CameraInput_open failed.");
105           return;
106       }
107   }
108   ```
109   > **说明:**
110   >
111   > 在相机设备输入之前需要先完成相机管理,详细开发步骤请参考[相机管理](native-camera-device-management.md)。
112
1134. 通过[OH_CameraManager_GetSupportedSceneModes()](../../reference/apis-camera-kit/_o_h___camera.md#oh_cameramanager_getsupportedscenemodes)方法,获取当前相机设备支持的模式列表,列表中存储了相机设备支持的所有模式[Camera_SceneMode](../../reference/apis-camera-kit/_o_h___camera.md#camera_scenemode)。
114
115   ```c++
116   bool IsSupportedSceneMode(Camera_Device camera, Camera_SceneMode sceneMode)
117   {
118       Camera_SceneMode* sceneModes = nullptr;
119       uint32_t sceneModeSize = 0;
120       Camera_ErrorCode ret = OH_CameraManager_GetSupportedSceneModes(&camera, &sceneModes, &sceneModeSize);
121       if (sceneModes == nullptr || ret != CAMERA_OK) {
122           OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedSceneModes failed.");
123           return false;
124       }
125       for (int index = 0; index < sceneModeSize; index++) {
126           OH_LOG_INFO(LOG_APP, "scene mode = %{public}u ", sceneModes[index]);    // 获取相机指定模式。
127           if (sceneModes[index] == sceneMode) {
128               return true;
129           }
130       }
131       return false;
132   }
133   ```
134
1355. 通过[OH_CameraManager_GetSupportedCameraOutputCapabilityWithSceneMode()](../../reference/apis-camera-kit/_o_h___camera.md#oh_cameramanager_getsupportedcameraoutputcapabilitywithscenemode)方法,获取当前设备支持的所有输出流,如预览流、拍照流等。输出流在CameraOutputCapability中的各个profile字段中。根据相机设备指定模式[Camera_SceneMode](../../reference/apis-camera-kit/_o_h___camera.md#camera_scenemode)的不同,需要添加不同类型的输出流。
136
137
138   ```c++
139   Camera_OutputCapability* GetSupportedCameraOutputCapability(Camera_Manager* cameraManager, Camera_Device &camera)
140   {
141       Camera_OutputCapability* cameraOutputCapability = nullptr;
142       // 示例代码以NORMAL_PHOTO模式为例,查询NORMAL_PHOTO在camera中是否支持。
143       bool isSupported = IsSupportedSceneMode(camera, Camera_SceneMode::NORMAL_PHOTO);
144       if (!isSupported) {
145           OH_LOG_ERROR(LOG_APP, "NORMAL_PHOTO is not supported.");
146           return cameraOutputCapability;
147       }
148       // 获取相机设备支持的输出流能力。
149       const Camera_Profile* previewProfile = nullptr;
150       const Camera_Profile* photoProfile = nullptr;
151       Camera_ErrorCode ret = OH_CameraManager_GetSupportedCameraOutputCapabilityWithSceneMode(cameraManager, &camera,
152           Camera_SceneMode::NORMAL_PHOTO, &cameraOutputCapability);
153       if (cameraOutputCapability == nullptr || ret != CAMERA_OK) {
154           OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameraOutputCapability failed.");
155           return cameraOutputCapability;
156       }
157       // 以NORMAL_PHOTO模式为例,需要添加预览流、拍照流。
158       if (cameraOutputCapability->previewProfiles == nullptr) {
159           OH_LOG_ERROR(LOG_APP, "previewProfiles == null");
160       } else {
161           // 根据所需从cameraOutputCapability->previewProfiles中选择合适的预览分辨率。
162           previewProfile = cameraOutputCapability->previewProfiles[0];
163       }
164       if (cameraOutputCapability->photoProfiles == nullptr) {
165           OH_LOG_ERROR(LOG_APP, "photoProfiles == null");
166       } else {
167           // 根据所需从cameraOutputCapability->photoProfiles中选择合适的拍照分辨率。
168           photoProfile = cameraOutputCapability->photoProfiles[0];
169       }
170       return cameraOutputCapability;
171   }
172   ```