• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Secondary Processing of Preview Streams (C/C++)
2
3You can use the APIs in the **ImageReceiver** class to create a **PreviewOutput** instance and obtain real-time data of the preview stream for secondary processing. For example, you can add a filter algorithm to the preview stream.
4
5## How to Develop
6
7Read [Camera](../reference/apis-camera-kit/_o_h___camera.md) for the API reference.
8
91. Import the NDK, which provides camera-related attributes and methods.
10
11   ```c++
12    // Include the NDK header files.
13    #include "hilog/log.h"
14    #include "ohcamera/camera.h"
15    #include "ohcamera/camera_input.h"
16    #include "ohcamera/capture_session.h"
17    #include "ohcamera/photo_output.h"
18    #include "ohcamera/preview_output.h"
19    #include "ohcamera/video_output.h"
20    #include "ohcamera/camera_manager.h"
21   ```
22
232. Link the dynamic library in the CMake script.
24
25   ```txt
26    target_link_libraries(entry PUBLIC libohcamera.so libhilog_ndk.z.so)
27   ```
28
293. Obtain the surface ID.
30
31   Call **createImageReceiver(**) of the image module to create an **ImageReceiver** instance, and call **getReceivingSurfaceId()** of the instance to obtain the surface ID.
32
334. Call **OH_CameraManager_GetSupportedCameraOutputCapability()** to obtain the preview capability supported by the current device based on the surface ID. Then call **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.
34
35   ```c++
36    NDKCamera::NDKCamera(char *str)
37    {
38      Camera_Manager *cameraManager = nullptr;
39      Camera_Device* cameras = nullptr;
40      Camera_OutputCapability* cameraOutputCapability = nullptr;
41      Camera_PreviewOutput* previewOutput = nullptr;
42      const Camera_Profile* previewProfile = nullptr;
43      uint32_t size = 0;
44      uint32_t cameraDeviceIndex = 0;
45      char* previewSurfaceId = str;
46      Camera_ErrorCode ret = OH_Camera_GetCameraManager(&cameraManager);
47      if (cameraManager == nullptr || ret != CAMERA_OK) {
48        OH_LOG_ERROR(LOG_APP, "OH_Camera_GetCameraManager failed.");
49      }
50      ret = OH_CameraManager_GetSupportedCameras(cameraManager, &cameras, &size);
51      if (cameras == nullptr || size < 0 || ret != CAMERA_OK) {
52        OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameras failed.");
53      }
54      ret = OH_CameraManager_GetSupportedCameraOutputCapability(cameraManager, &cameras[cameraDeviceIndex],
55                                                                      &cameraOutputCapability);
56      if (cameraOutputCapability == nullptr || ret != CAMERA_OK) {
57        OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameraOutputCapability failed.");
58      }
59      if (cameraOutputCapability->previewProfilesSize < 0) {
60        OH_LOG_ERROR(LOG_APP, "previewProfilesSize == null");
61      }
62      previewProfile = cameraOutputCapability->previewProfiles[0];
63
64      ret = OH_CameraManager_CreatePreviewOutput(cameraManager, previewProfile, previewSurfaceId, &previewOutput);
65      if (previewProfile == nullptr || previewOutput == nullptr || ret != CAMERA_OK) {
66        OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreatePreviewOutput failed.");
67      }
68    }
69   ```
70
715. Configure the session. Call **commitConfig()** to commit the session configuration, and then call **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/_o_h___camera.md#camera_errorcode-1).
72
73   ```c++
74    ret = OH_PreviewOutput_Start(previewOutput);
75    if (ret != CAMERA_OK) {
76        OH_LOG_ERROR(LOG_APP, "OH_PreviewOutput_Start failed.");
77    }
78   ```
79
806. Call **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/_o_h___camera.md#camera_errorcode-1).
81
82   ```c++
83    ret = OH_PreviewOutput_Stop(previewOutput);
84    if (ret != CAMERA_OK) {
85        OH_LOG_ERROR(LOG_APP, "OH_PreviewOutput_Stop failed.");
86    }
87   ```
88
89## Status Listening
90
91During camera application development, you can listen for the preview output stream status, including preview stream start, preview stream end, and preview stream output errors.
92
93- Register the **'frameStart'** event to listen for preview start events. This event can be registered when a **PreviewOutput** instance 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.
94
95  ```c++
96    ret = OH_PreviewOutput_RegisterCallback(previewOutput, GetPreviewOutputListener());
97    if (ret != CAMERA_OK) {
98        OH_LOG_ERROR(LOG_APP, "OH_PreviewOutput_RegisterCallback failed.");
99    }
100  ```
101  ```c++
102    void PreviewOutputOnFrameStart(Camera_PreviewOutput* previewOutput)
103    {
104        OH_LOG_INFO(LOG_APP, "PreviewOutputOnFrameStart");
105    }
106    PreviewOutput_Callbacks* GetPreviewOutputListener(void)
107    {
108        static PreviewOutput_Callbacks previewOutputListener = {
109            .onFrameStart = PreviewOutputOnFrameStart,
110            .onFrameEnd = PreviewOutputOnFrameEnd,
111            .onError = PreviewOutputOnError
112        };
113        return &previewOutputListener;
114    }
115  ```
116
117- Register the **'frameEnd'** event to listen for preview end events. This event can be registered when a **PreviewOutput** instance is created and is triggered when the last frame of preview ends. The preview stream ends as long as a result is returned.
118
119  ```c++
120    void PreviewOutputOnFrameEnd(Camera_PreviewOutput* previewOutput, int32_t frameCount)
121    {
122        OH_LOG_INFO(LOG_APP, "PreviewOutput frameCount = %{public}d", frameCount);
123    }
124  ```
125
126- 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/_o_h___camera.md#camera_errorcode-1).
127
128  ```c++
129    void PreviewOutputOnError(Camera_PreviewOutput* previewOutput, Camera_ErrorCode errorCode)
130    {
131        OH_LOG_INFO(LOG_APP, "PreviewOutput errorCode = %{public}d", errorCode);
132    }
133  ```
134