1e41f4b71Sopenharmony_ci# Secondary Processing of Preview Streams (C/C++) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciYou 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. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci## How to Develop 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciRead [Camera](../../reference/apis-camera-kit/_o_h___camera.md) for the API reference. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci1. Import the NDK, which provides camera-related attributes and methods. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci ```c++ 12e41f4b71Sopenharmony_ci // Include the NDK header files. 13e41f4b71Sopenharmony_ci #include "hilog/log.h" 14e41f4b71Sopenharmony_ci #include "ohcamera/camera.h" 15e41f4b71Sopenharmony_ci #include "ohcamera/camera_input.h" 16e41f4b71Sopenharmony_ci #include "ohcamera/capture_session.h" 17e41f4b71Sopenharmony_ci #include "ohcamera/photo_output.h" 18e41f4b71Sopenharmony_ci #include "ohcamera/preview_output.h" 19e41f4b71Sopenharmony_ci #include "ohcamera/video_output.h" 20e41f4b71Sopenharmony_ci #include "ohcamera/camera_manager.h" 21e41f4b71Sopenharmony_ci ``` 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci2. Link the dynamic library in the CMake script. 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci ```txt 26e41f4b71Sopenharmony_ci target_link_libraries(entry PUBLIC libohcamera.so libhilog_ndk.z.so) 27e41f4b71Sopenharmony_ci ``` 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci3. Obtain the surface ID. 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci Call **createImageReceiver()** of the image module to create an **ImageReceiver** instance, and call **getReceivingSurfaceId()** of the instance to obtain the surface ID. 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci4. 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. 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci ```c++ 36e41f4b71Sopenharmony_ci NDKCamera::NDKCamera(char *str) 37e41f4b71Sopenharmony_ci { 38e41f4b71Sopenharmony_ci Camera_Manager *cameraManager = nullptr; 39e41f4b71Sopenharmony_ci Camera_Device* cameras = nullptr; 40e41f4b71Sopenharmony_ci Camera_OutputCapability* cameraOutputCapability = nullptr; 41e41f4b71Sopenharmony_ci Camera_PreviewOutput* previewOutput = nullptr; 42e41f4b71Sopenharmony_ci const Camera_Profile* previewProfile = nullptr; 43e41f4b71Sopenharmony_ci uint32_t size = 0; 44e41f4b71Sopenharmony_ci uint32_t cameraDeviceIndex = 0; 45e41f4b71Sopenharmony_ci char* previewSurfaceId = str; 46e41f4b71Sopenharmony_ci Camera_ErrorCode ret = OH_Camera_GetCameraManager(&cameraManager); 47e41f4b71Sopenharmony_ci if (cameraManager == nullptr || ret != CAMERA_OK) { 48e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "OH_Camera_GetCameraManager failed."); 49e41f4b71Sopenharmony_ci } 50e41f4b71Sopenharmony_ci ret = OH_CameraManager_GetSupportedCameras(cameraManager, &cameras, &size); 51e41f4b71Sopenharmony_ci if (cameras == nullptr || size < 0 || ret != CAMERA_OK) { 52e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameras failed."); 53e41f4b71Sopenharmony_ci } 54e41f4b71Sopenharmony_ci ret = OH_CameraManager_GetSupportedCameraOutputCapability(cameraManager, &cameras[cameraDeviceIndex], 55e41f4b71Sopenharmony_ci &cameraOutputCapability); 56e41f4b71Sopenharmony_ci if (cameraOutputCapability == nullptr || ret != CAMERA_OK) { 57e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameraOutputCapability failed."); 58e41f4b71Sopenharmony_ci } 59e41f4b71Sopenharmony_ci if (cameraOutputCapability->previewProfilesSize < 0) { 60e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "previewProfilesSize == null"); 61e41f4b71Sopenharmony_ci } 62e41f4b71Sopenharmony_ci previewProfile = cameraOutputCapability->previewProfiles[0]; 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci ret = OH_CameraManager_CreatePreviewOutput(cameraManager, previewProfile, previewSurfaceId, &previewOutput); 65e41f4b71Sopenharmony_ci if (previewProfile == nullptr || previewOutput == nullptr || ret != CAMERA_OK) { 66e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreatePreviewOutput failed."); 67e41f4b71Sopenharmony_ci } 68e41f4b71Sopenharmony_ci } 69e41f4b71Sopenharmony_ci ``` 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci5. 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). 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci ```c++ 74e41f4b71Sopenharmony_ci ret = OH_PreviewOutput_Start(previewOutput); 75e41f4b71Sopenharmony_ci if (ret != CAMERA_OK) { 76e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "OH_PreviewOutput_Start failed."); 77e41f4b71Sopenharmony_ci } 78e41f4b71Sopenharmony_ci ``` 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci6. 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). 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ci ```c++ 83e41f4b71Sopenharmony_ci ret = OH_PreviewOutput_Stop(previewOutput); 84e41f4b71Sopenharmony_ci if (ret != CAMERA_OK) { 85e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "OH_PreviewOutput_Stop failed."); 86e41f4b71Sopenharmony_ci } 87e41f4b71Sopenharmony_ci ``` 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ci## Status Listening 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ciDuring camera application development, you can listen for the preview output stream status, including preview stream start, preview stream end, and preview stream output errors. 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci- 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. 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci ```c++ 96e41f4b71Sopenharmony_ci ret = OH_PreviewOutput_RegisterCallback(previewOutput, GetPreviewOutputListener()); 97e41f4b71Sopenharmony_ci if (ret != CAMERA_OK) { 98e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "OH_PreviewOutput_RegisterCallback failed."); 99e41f4b71Sopenharmony_ci } 100e41f4b71Sopenharmony_ci ``` 101e41f4b71Sopenharmony_ci ```c++ 102e41f4b71Sopenharmony_ci void PreviewOutputOnFrameStart(Camera_PreviewOutput* previewOutput) 103e41f4b71Sopenharmony_ci { 104e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "PreviewOutputOnFrameStart"); 105e41f4b71Sopenharmony_ci } 106e41f4b71Sopenharmony_ci PreviewOutput_Callbacks* GetPreviewOutputListener(void) 107e41f4b71Sopenharmony_ci { 108e41f4b71Sopenharmony_ci static PreviewOutput_Callbacks previewOutputListener = { 109e41f4b71Sopenharmony_ci .onFrameStart = PreviewOutputOnFrameStart, 110e41f4b71Sopenharmony_ci .onFrameEnd = PreviewOutputOnFrameEnd, 111e41f4b71Sopenharmony_ci .onError = PreviewOutputOnError 112e41f4b71Sopenharmony_ci }; 113e41f4b71Sopenharmony_ci return &previewOutputListener; 114e41f4b71Sopenharmony_ci } 115e41f4b71Sopenharmony_ci ``` 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ci- 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. 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci ```c++ 120e41f4b71Sopenharmony_ci void PreviewOutputOnFrameEnd(Camera_PreviewOutput* previewOutput, int32_t frameCount) 121e41f4b71Sopenharmony_ci { 122e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "PreviewOutput frameCount = %{public}d", frameCount); 123e41f4b71Sopenharmony_ci } 124e41f4b71Sopenharmony_ci ``` 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ci- 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). 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_ci ```c++ 129e41f4b71Sopenharmony_ci void PreviewOutputOnError(Camera_PreviewOutput* previewOutput, Camera_ErrorCode errorCode) 130e41f4b71Sopenharmony_ci { 131e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "PreviewOutput errorCode = %{public}d", errorCode); 132e41f4b71Sopenharmony_ci } 133e41f4b71Sopenharmony_ci ``` 134