• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Preview (C/C++)
2<!--Kit: Camera Kit-->
3<!--Subsystem: Multimedia-->
4<!--Owner: @qano-->
5<!--SE: @leo_ysl-->
6<!--TSE: @xchaosioda-->
7
8Preview is the image you see after you start the camera application but before you take photos or record videos.
9
10## How to Develop
11
12Read [Camera](../../reference/apis-camera-kit/capi-oh-camera.md) for the API reference.
13
141. Import the NDK, which provides camera-related attributes and methods.
15
16   ```c++
17   // Include the NDK header files.
18   #include "hilog/log.h"
19   #include "ohcamera/camera.h"
20   #include "ohcamera/camera_input.h"
21   #include "ohcamera/capture_session.h"
22   #include "ohcamera/photo_output.h"
23   #include "ohcamera/preview_output.h"
24   #include "ohcamera/video_output.h"
25   #include "ohcamera/camera_manager.h"
26   ```
27
282. Link the dynamic library in the CMake script.
29
30   ```txt
31   target_link_libraries(entry PUBLIC
32       libace_napi.z.so
33       libohcamera.so
34       libhilog_ndk.z.so
35   )
36   ```
37
383. Obtain the surface ID.
39
40    The **XComponent**, the capabilities of which are provided by the UI, offers the surface ID for preview streams. For details, see [XComponent](../../reference/apis-arkui/arkui-ts/ts-basic-components-xcomponent.md).
41
424. Call [OH_CameraManager_GetSupportedCameraOutputCapability()](../../reference/apis-camera-kit/capi-camera-manager-h.md#oh_cameramanager_getsupportedcameraoutputcapability) to obtain the preview capability supported by the current device based on the surface ID. Then call [OH_CameraManager_CreatePreviewOutput()](../../reference/apis-camera-kit/capi-camera-manager-h.md#oh_cameramanager_createpreviewoutput) to create a PreviewOutput instance, with the parameters set to the **cameraManager** pointer, the first item in the **previewProfiles** array, the surface ID obtained in step 3, and the returned **previewOutput** pointer, respectively.
43
44    ```c++
45    Camera_PreviewOutput* CreatePreviewOutput(char* previewSurfaceIdstr, Camera_Manager* cameraManager,
46        const Camera_Profile* previewProfile)
47    {
48        Camera_PreviewOutput* previewOutput = nullptr;
49        Camera_ErrorCode ret = OH_CameraManager_CreatePreviewOutput(cameraManager, previewProfile,
50            previewSurfaceIdstr, &previewOutput);
51        if (previewOutput == nullptr || ret != CAMERA_OK) {
52            OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreatePreviewOutput failed.");
53        }
54        return previewOutput;
55    }
56    ```
57
585. Configure the session. Call **commitConfig()** to commit the session configuration, and then call [OH_CaptureSession_Start()](../../reference/apis-camera-kit/capi-capture-session-h.md#oh_capturesession_start) to start outputting the preview stream. If the call fails, an error code is returned. For details, see [Camera_ErrorCode](../../reference/apis-camera-kit/capi-camera-h.md#camera_errorcode).
59
60   ```c++
61   Camera_ErrorCode SessionStart(Camera_CaptureSession* captureSession)
62   {
63       Camera_ErrorCode ret = OH_CaptureSession_Start(captureSession);
64       if (ret != CAMERA_OK) {
65           OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Start failed.");
66       }
67       return ret;
68   }
69   ```
70
716. Call [OH_CaptureSession_Stop()](../../reference/apis-camera-kit/capi-capture-session-h.md#oh_capturesession_stop) to stop outputting the preview stream. If the call fails, an error code is returned. For details, see[Camera_ErrorCode](../../reference/apis-camera-kit/capi-camera-h.md#camera_errorcode).
72
73   ```c++
74   Camera_ErrorCode SessionStop(Camera_CaptureSession* captureSession)
75   {
76       Camera_ErrorCode ret = OH_CaptureSession_Stop(captureSession);
77       if (ret != CAMERA_OK) {
78           OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Stop failed.");
79       }
80       return ret;
81   }
82   ```
83
84## Status Listening
85
86During camera application development, you can listen for the preview output stream status, including preview stream start, preview stream end, and preview stream output errors.
87
88- Register the **'frameStart'** event to listen for preview start events. This event can be registered when a PreviewOutput object is created and is triggered when the bottom layer starts exposure for the first time. The preview stream starts as long as a result is returned.
89
90  ```c++
91  void PreviewOutputOnFrameStart(Camera_PreviewOutput* previewOutput)
92  {
93      OH_LOG_INFO(LOG_APP, "PreviewOutputOnFrameStart");
94  }
95  ```
96
97- Register the **'frameEnd'** event to listen for preview end events. This event can be registered when a PreviewOutput object is created and is triggered when the last frame of preview ends. The preview stream ends as long as a result is returned.
98
99  ```c++
100  void PreviewOutputOnFrameEnd(Camera_PreviewOutput* previewOutput, int32_t frameCount)
101  {
102      OH_LOG_INFO(LOG_APP, "PreviewOutput frameCount = %{public}d", frameCount);
103  }
104  ```
105
106- Register the **'error'** event to listen for preview output errors. The callback function returns an error code when an API is incorrectly used. For details about the error code types, see [Camera_ErrorCode](../../reference/apis-camera-kit/capi-camera-h.md#camera_errorcode).
107
108  ```c++
109  void PreviewOutputOnError(Camera_PreviewOutput* previewOutput, Camera_ErrorCode errorCode)
110  {
111      OH_LOG_ERROR(LOG_APP, "PreviewOutput errorCode = %{public}d", errorCode);
112  }
113  ```
114  ```c++
115  PreviewOutput_Callbacks* GetPreviewOutputListener(void)
116  {
117      static PreviewOutput_Callbacks previewOutputListener = {
118          .onFrameStart = PreviewOutputOnFrameStart,
119          .onFrameEnd = PreviewOutputOnFrameEnd,
120          .onError = PreviewOutputOnError
121      };
122      return &previewOutputListener;
123  }
124  Camera_ErrorCode RegisterPreviewOutputCallback(Camera_PreviewOutput* previewOutput)
125  {
126      Camera_ErrorCode ret = OH_PreviewOutput_RegisterCallback(previewOutput, GetPreviewOutputListener());
127      if (ret != CAMERA_OK) {
128          OH_LOG_ERROR(LOG_APP, "OH_PreviewOutput_RegisterCallback failed.");
129      }
130      return ret;
131  }
132  ```
133