• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Camera Photographing Sample (C/C++)
2
3This topic provides sample code that covers the complete photographing process and the API calling sequence. For details about a single process (such as device input, session management, and photographing), see the corresponding C/C++ development guide links provided in [Camera Development Preparations](camera-preparation.md).
4
5## Development Process
6
7After obtaining the output stream capabilities supported by the camera, create a photo stream. The development process is as follows:
8
9![Photographing Development Process](figures/photographing-ndk-development-process.png)
10
11## Sample Code
12
131. Link the dynamic library in the CMake script.
14    ```txt
15        target_link_libraries(entry PUBLIC libohcamera.so libhilog_ndk.z.so)
16    ```
17
182. Import the NDK APIs on the C++ side, and perform photographing based on the surface ID passed in.
19    ```c++
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    void CaptureSessionOnFocusStateChange(Camera_CaptureSession* session, Camera_FocusState focusState)
30    {
31        OH_LOG_INFO(LOG_APP, "CaptureSessionOnFocusStateChange");
32    }
33
34    void CaptureSessionOnError(Camera_CaptureSession* session, Camera_ErrorCode errorCode)
35    {
36        OH_LOG_INFO(LOG_APP, "CaptureSessionOnError = %{public}d", errorCode);
37    }
38
39    CaptureSession_Callbacks* GetCaptureSessionRegister(void)
40    {
41        static CaptureSession_Callbacks captureSessionCallbacks = {
42            .onFocusStateChange = CaptureSessionOnFocusStateChange,
43            .onError = CaptureSessionOnError
44        };
45        return &captureSessionCallbacks;
46    }
47
48    void PreviewOutputOnFrameStart(Camera_PreviewOutput* previewOutput)
49    {
50        OH_LOG_INFO(LOG_APP, "PreviewOutputOnFrameStart");
51    }
52
53    void PreviewOutputOnFrameEnd(Camera_PreviewOutput* previewOutput, int32_t frameCount)
54    {
55        OH_LOG_INFO(LOG_APP, "PreviewOutputOnFrameEnd = %{public}d", frameCount);
56    }
57
58    void PreviewOutputOnError(Camera_PreviewOutput* previewOutput, Camera_ErrorCode errorCode)
59    {
60        OH_LOG_INFO(LOG_APP, "PreviewOutputOnError = %{public}d", errorCode);
61    }
62
63    PreviewOutput_Callbacks* GetPreviewOutputListener(void)
64    {
65        static PreviewOutput_Callbacks previewOutputListener = {
66            .onFrameStart = PreviewOutputOnFrameStart,
67            .onFrameEnd = PreviewOutputOnFrameEnd,
68            .onError = PreviewOutputOnError
69        };
70        return &previewOutputListener;
71    }
72
73    void OnCameraInputError(const Camera_Input* cameraInput, Camera_ErrorCode errorCode)
74    {
75        OH_LOG_INFO(LOG_APP, "OnCameraInput errorCode = %{public}d", errorCode);
76    }
77
78    CameraInput_Callbacks* GetCameraInputListener(void)
79    {
80        static CameraInput_Callbacks cameraInputCallbacks = {
81            .onError = OnCameraInputError
82        };
83        return &cameraInputCallbacks;
84    }
85
86    void CameraManagerStatusCallback(Camera_Manager* cameraManager, Camera_StatusInfo* status)
87    {
88        OH_LOG_INFO(LOG_APP, "CameraManagerStatusCallback is called");
89    }
90
91    CameraManager_Callbacks* GetCameraManagerListener()
92    {
93        static CameraManager_Callbacks cameraManagerListener = {
94            .onCameraStatus = CameraManagerStatusCallback
95        };
96        return &cameraManagerListener;
97    }
98
99    NDKCamera::NDKCamera(char *previewId, char *photoId)
100    {
101        Camera_Manager* cameraManager = nullptr;
102        Camera_Device* cameras = nullptr;
103        Camera_CaptureSession* captureSession = nullptr;
104        Camera_OutputCapability* cameraOutputCapability = nullptr;
105        const Camera_Profile* previewProfile = nullptr;
106        const Camera_Profile* photoProfile = nullptr;
107        Camera_PreviewOutput* previewOutput = nullptr;
108        Camera_PhotoOutput* photoOutput = nullptr;
109        Camera_Input* cameraInput = nullptr;
110        uint32_t size = 0;
111        uint32_t cameraDeviceIndex = 0;
112        char* previewSurfaceId = previewId;
113        char* photoSurfaceId = photoId;
114        // Create a CameraManager object.
115        Camera_ErrorCode ret = OH_Camera_GetCameraManager(&cameraManager);
116        if (cameraManager == nullptr || ret != CAMERA_OK) {
117          OH_LOG_ERROR(LOG_APP, "OH_Camera_GetCameraMananger failed.");
118        }
119        // Listen for camera status changes.
120        ret = OH_CameraManager_RegisterCallback(cameraManager, GetCameraManagerListener());
121        if (ret != CAMERA_OK) {
122          OH_LOG_ERROR(LOG_APP, "OH_CameraManager_RegisterCallback failed.");
123        }
124
125        // Obtain the camera list.
126        ret = OH_CameraManager_GetSupportedCameras(cameraManager, &cameras, &size);
127        if (cameras == nullptr || size < 0 || ret != CAMERA_OK) {
128          OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameras failed.");
129        }
130
131        // Create a camera input stream.
132        ret = OH_CameraManager_CreateCameraInput(cameraManager, &cameras[cameraDeviceIndex], &cameraInput);
133        if (cameraInput == nullptr || ret != CAMERA_OK) {
134          OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateCameraInput failed.");
135        }
136
137        // Listen for camera input errors.
138        ret = OH_CameraInput_RegisterCallback(cameraInput, GetCameraInputListener());
139        if (ret != CAMERA_OK) {
140          OH_LOG_ERROR(LOG_APP, "OH_CameraInput_RegisterCallback failed.");
141        }
142
143        // Open the camera.
144        ret = OH_CameraInput_Open(cameraInput);
145        if (ret != CAMERA_OK) {
146          OH_LOG_ERROR(LOG_APP, "OH_CameraInput_Open failed.");
147        }
148
149        // Obtain the output streams supported by the camera.
150        ret = OH_CameraManager_GetSupportedCameraOutputCapability(cameraManager, &cameras[cameraDeviceIndex],
151                                                                  &cameraOutputCapability);
152        if (cameraOutputCapability == nullptr || ret != CAMERA_OK) {
153          OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameraOutputCapability failed.");
154        }
155
156        if (cameraOutputCapability->previewProfilesSize < 0) {
157          OH_LOG_ERROR(LOG_APP, "previewProfilesSize == null");
158        }
159        previewProfile = cameraOutputCapability->previewProfiles[0];
160
161        if (cameraOutputCapability->photoProfilesSize < 0) {
162          OH_LOG_ERROR(LOG_APP, "photoProfilesSize == null");
163        }
164        photoProfile = cameraOutputCapability->photoProfiles[0];
165
166
167        // Create a preview output stream, with the previewSurfaceId parameter set to the ID of the surface provided by the <XComponent>.
168        ret = OH_CameraManager_CreatePreviewOutput(cameraManager, previewProfile, previewSurfaceId, &previewOutput);
169        if (previewProfile == nullptr || previewOutput == nullptr || ret != CAMERA_OK) {
170          OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreatePreviewOutput failed.");
171        }
172
173        // Listen for preview output errors.
174        ret = OH_PreviewOutput_RegisterCallback(previewOutput, GetPreviewOutputListener());
175        if (ret != CAMERA_OK) {
176          OH_LOG_ERROR(LOG_APP, "OH_PreviewOutput_RegisterCallback failed.");
177        }
178
179        // Create a photo output stream, with the photoSurfaceId parameter set to the ID of the surface obtained through ImageReceiver.
180        ret = OH_CameraManager_CreatePhotoOutput(cameraManager, photoProfile, photoSurfaceId, &photoOutput);
181        if (photoProfile == nullptr || photoOutput == nullptr || ret != CAMERA_OK) {
182          OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreatePhotoOutput failed.");
183        }
184
185        // Create a session.
186        ret = OH_CameraManager_CreateCaptureSession(cameraManager, &captureSession);
187        if (captureSession == nullptr || ret != CAMERA_OK) {
188          OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateCaptureSession failed.");
189        }
190
191        // Listen for session errors.
192        ret = OH_CaptureSession_RegisterCallback(captureSession, GetCaptureSessionRegister());
193        if (ret != CAMERA_OK) {
194          OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_RegisterCallback failed.");
195        }
196
197        // Start configuration for the session.
198        ret = OH_CaptureSession_BeginConfig(captureSession);
199        if (ret != CAMERA_OK) {
200          OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_BeginConfig failed.");
201        }
202
203        // Add the camera input stream to the session.
204        ret = OH_CaptureSession_AddInput(captureSession, cameraInput);
205        if (ret != CAMERA_OK) {
206          OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddInput failed.");
207        }
208
209        // Add the preview output stream to the session.
210        ret = OH_CaptureSession_AddPreviewOutput(captureSession, previewOutput);
211        if (ret != CAMERA_OK) {
212          OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddPreviewOutput failed.");
213        }
214
215        // Add the photo output stream to the session.
216        ret = OH_CaptureSession_AddPhotoOutput(captureSession, photoOutput);
217        if (ret != CAMERA_OK) {
218          OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddPhotoOutput failed.");
219        }
220
221        // Commit the session configuration.
222        ret = OH_CaptureSession_CommitConfig(captureSession);
223        if (ret != CAMERA_OK) {
224          OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_CommitConfig failed.");
225        }
226
227        // Start the session.
228        ret = OH_CaptureSession_Start(captureSession);
229        if (ret != CAMERA_OK) {
230          OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Start failed.");
231        }
232
233        // Check whether the camera has flash.
234        Camera_FlashMode flashMode = FLASH_MODE_AUTO;
235        bool hasFlash = false;
236        ret = OH_CaptureSession_HasFlash(captureSession, &hasFlash);
237        if (ret != CAMERA_OK) {
238          OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_HasFlash failed.");
239        }
240        if (hasFlash) {
241          OH_LOG_INFO(LOG_APP, "hasFlash success");
242        } else {
243          OH_LOG_ERROR(LOG_APP, "hasFlash fail");
244        }
245        // Check whether a flash mode is supported.
246        bool isSupported = false;
247        ret = OH_CaptureSession_IsFlashModeSupported(captureSession, flashMode, &isSupported);
248        if (ret != CAMERA_OK) {
249          OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_IsFlashModeSupported failed.");
250        }
251        if (isSupported) {
252          OH_LOG_INFO(LOG_APP, "isFlashModeSupported success");
253          // Set the flash mode.
254          ret = OH_CaptureSession_SetFlashMode(captureSession, flashMode);
255          if (ret == CAMERA_OK) {
256              OH_LOG_INFO(LOG_APP, "OH_CaptureSession_SetFlashMode success.");
257          } else {
258              OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_SetFlashMode failed. %{public}d ", ret);
259          }
260          // Obtain the flash mode in use.
261          ret = OH_CaptureSession_GetFlashMode(captureSession, &flashMode);
262          if (ret == CAMERA_OK) {
263              OH_LOG_INFO(LOG_APP, "OH_CaptureSession_GetFlashMode success. flashMode: %{public}d ", flashMode);
264          } else {
265              OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_GetFlashMode failed. %d ", ret);
266          }
267        } else {
268          OH_LOG_ERROR(LOG_APP, "isFlashModeSupported fail");
269        }
270
271        // Check whether the continuous auto focus is supported.
272        Camera_FocusMode focusMode = FOCUS_MODE_CONTINUOUS_AUTO;
273        bool isFocusModeSupported = false;
274        ret = OH_CaptureSession_IsFocusModeSupported(captureSession, focusMode, &isFocusModeSupported);
275        if (ret != CAMERA_OK) {
276          OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_IsFocusModeSupported failed.");
277        }
278        if (isFocusModeSupported) {
279          OH_LOG_INFO(LOG_APP, "isFocusModeSupported success");
280          ret = OH_CaptureSession_SetFocusMode(captureSession, focusMode);
281          if (ret != CAMERA_OK) {
282              OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_SetFocusMode failed. %{public}d ", ret);
283          }
284          ret = OH_CaptureSession_GetFocusMode(captureSession, &focusMode);
285          if (ret == CAMERA_OK) {
286              OH_LOG_INFO(LOG_APP, "OH_CaptureSession_GetFocusMode success. focusMode%{public}d ", focusMode);
287          } else {
288              OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_GetFocusMode failed. %d ", ret);
289          }
290        } else {
291          OH_LOG_ERROR(LOG_APP, "isFocusModeSupported fail");
292        }
293
294        // Obtain the zoom ratio range supported by the camera.
295        float minZoom;
296        float maxZoom;
297        ret = OH_CaptureSession_GetZoomRatioRange(captureSession, &minZoom, &maxZoom);
298        if (ret != CAMERA_OK) {
299          OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_GetZoomRatioRange failed.");
300        } else {
301          OH_LOG_INFO(LOG_APP, "OH_CaptureSession_GetZoomRatioRange success. minZoom: %{public}f, maxZoom:%{public}f",
302              minZoom, maxZoom);
303        }
304        // Set a zoom ratio.
305        ret = OH_CaptureSession_SetZoomRatio(captureSession, maxZoom);
306        if (ret == CAMERA_OK) {
307          OH_LOG_INFO(LOG_APP, "OH_CaptureSession_SetZoomRatio success.");
308        } else {
309          OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_SetZoomRatio failed. %{public}d ", ret);
310        }
311        // Obtain the zoom ratio of the camera.
312        ret = OH_CaptureSession_GetZoomRatio(captureSession, &maxZoom);
313        if (ret == CAMERA_OK) {
314          OH_LOG_INFO(LOG_APP, "OH_CaptureSession_GetZoomRatio success. zoom: %{public}f ", maxZoom);
315        } else {
316          OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_GetZoomRatio failed. %{public}d ", ret);
317        }
318
319        // Take photos without photographing settings.
320        ret = OH_PhotoOutput_Capture(photoOutput);
321        if (ret == CAMERA_OK) {
322          OH_LOG_INFO(LOG_APP, "OH_PhotoOutput_Capture success ");
323        } else {
324          OH_LOG_ERROR(LOG_APP, "OH_PhotoOutput_Capture failed. %d ", ret);
325        }
326
327        // Stop the session.
328        ret = OH_CaptureSession_Stop(captureSession);
329        if (ret == CAMERA_OK) {
330          OH_LOG_INFO(LOG_APP, "OH_CaptureSession_Stop success ");
331        } else {
332          OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Stop failed. %d ", ret);
333        }
334
335        // Release the camera input stream.
336        ret = OH_CameraInput_Close(cameraInput);
337        if (ret == CAMERA_OK) {
338          OH_LOG_INFO(LOG_APP, "OH_CameraInput_Close success ");
339        } else {
340          OH_LOG_ERROR(LOG_APP, "OH_CameraInput_Close failed. %d ", ret);
341        }
342
343        // Release the preview output stream.
344        ret = OH_PreviewOutput_Release(previewOutput);
345        if (ret == CAMERA_OK) {
346          OH_LOG_INFO(LOG_APP, "OH_PreviewOutput_Release success ");
347        } else {
348          OH_LOG_ERROR(LOG_APP, "OH_PreviewOutput_Release failed. %d ", ret);
349        }
350
351        // Release the photo output stream.
352        ret = OH_PhotoOutput_Release(photoOutput);
353        if (ret == CAMERA_OK) {
354          OH_LOG_INFO(LOG_APP, "OH_PhotoOutput_Release success ");
355        } else {
356          OH_LOG_ERROR(LOG_APP, "OH_PhotoOutput_Release failed. %d ", ret);
357        }
358
359        // Release the session.
360        ret = OH_CaptureSession_Release(captureSession);
361        if (ret == CAMERA_OK) {
362          OH_LOG_INFO(LOG_APP, "OH_CaptureSession_Release success ");
363        } else {
364          OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Release failed. %d ", ret);
365        }
366
367        // Release the resources.
368        ret = OH_CameraManager_DeleteSupportedCameras(cameraManager, cameras, size);
369        if (ret != CAMERA_OK) {
370          OH_LOG_ERROR(LOG_APP, "Delete Cameras failed.");
371        } else {
372          OH_LOG_ERROR(LOG_APP, "OH_CameraManager_DeleteSupportedCameras. ok");
373        }
374        ret = OH_CameraManager_DeleteSupportedCameraOutputCapability(cameraManager, cameraOutputCapability);
375        if (ret != CAMERA_OK) {
376          OH_LOG_ERROR(LOG_APP, "Delete Cameras failed.");
377        } else {
378          OH_LOG_ERROR(LOG_APP, "OH_CameraManager_DeleteSupportedCameraOutputCapability. ok");
379        }
380        ret = OH_Camera_DeleteCameraManager(cameraManager);
381        if (ret != CAMERA_OK) {
382          OH_LOG_ERROR(LOG_APP, "Delete Cameras failed.");
383        } else {
384          OH_LOG_ERROR(LOG_APP, "OH_Camera_DeleteCameraManager. ok");
385        }
386    }
387    ```
388