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 <cstdlib>
14   #include <hilog/log.h>
15   #include <memory>
16   #include <multimedia/image_framework/image/image_native.h>
17   #include <multimedia/image_framework/image/image_receiver_native.h>
18   #include "ohcamera/camera.h"
19   #include "ohcamera/camera_input.h"
20   #include "ohcamera/capture_session.h"
21   #include "ohcamera/preview_output.h"
22   #include "ohcamera/camera_manager.h"
23   ```
24
252. Link the dynamic library in the CMake script.
26
27   ```txt
28   target_link_libraries(entry PUBLIC
29       libace_napi.z.so
30       libhilog_ndk.z.so
31       libohimage.so
32       libimage_receiver.so
33       libnative_image.so
34       libohcamera.so
35       libnative_buffer.so
36   )
37   ```
38
393. Initialize an [ImageReceiver](../image/image-receiver-c.md) instance and obtain a surface ID.
40
41   Call **OH_ImageReceiverNative_Create** of the image module to create an **OH_ImageReceiverNative** instance, and call **OH_ImageReceiverNative_GetReceivingSurfaceId** of the instance to obtain a surface ID.
42
43   ```c++
44   void InitImageReceiver() {
45       OH_ImageReceiverOptions *options = nullptr;
46       // Capture exceptions and check whether the instance is null. This example shows only the API call process.
47       // Set image parameters.
48       Image_ErrorCode errCode = OH_ImageReceiverOptions_Create(&options);
49       Image_Size imgSize;
50       imgSize.width = 1080; // Width of the created preview stream.
51       imgSize.height = 1080; // Height of the created preview stream.
52       int32_t capacity = 8; // Maximum number of images in BufferQueue. The recommended value is 8.
53       errCode = OH_ImageReceiverOptions_SetSize(options, imgSize);
54       errCode = OH_ImageReceiverOptions_SetCapacity(options, capacity);
55       // Create an OH_ImageReceiverNative instance.
56       OH_ImageReceiverNative *receiver = nullptr;
57       errCode = OH_ImageReceiverNative_Create(options, &receiver);
58       // Obtain the Surface ID of the OH_ImageReceiverNative instance.
59       uint64_t receiverSurfaceID = 0;
60       errCode = OH_ImageReceiverNative_GetReceivingSurfaceId(receiver, &receiverSurfaceID);
61       OH_LOG_INFO(LOG_APP, "receiver surfaceID:%{public}%llu", receiverSurfaceID);
62   }
63   ```
64
654. Create a preview stream based on the surface ID obtained. For details, see step 4 in [Camera Preview (C/C++)](./native-camera-preview.md).
66
675. Create a session and enable it. For details, see [Camera Session Management (C/C++)](./native-camera-session-management.md).
68
696. Register an ImageReceiver callback to listen for the image content reported by each frame.
70
71   ```c++
72   OH_ImageReceiverNative *receiver; // Instance created in step 3.
73
74   // Image callback. For details, see Media/Image Kit.
75   static void OnCallback(OH_ImageReceiverNative *receiver) {
76       OH_LOG_INFO(LOG_APP, "ImageReceiverNativeCTest buffer available.");
77       // Capture exceptions and check whether the instance is null. This example shows only the API call process.
78       OH_ImageNative *image = nullptr;
79       // Obtain the image from the bufferQueue.
80       Image_ErrorCode errCode = OH_ImageReceiverNative_ReadNextImage(receiver, &image);
81       // Read the image width and height.
82       Image_Size size;
83       errCode = OH_ImageNative_GetImageSize(image, &size);
84       OH_LOG_INFO(LOG_APP, "OH_ImageNative_GetImageSize errCode:%{public}d width:%{public}d height:%{public}d", errCode,
85           size.width, size.height);
86
87       // Obtain the image's component type.
88       size_t typeSize = 0;
89       OH_ImageNative_GetComponentTypes(image, nullptr, &typeSize);
90       uint32_t* types = new uint32_t[typeSize];
91       OH_ImageNative_GetComponentTypes(image, &types, &typeSize);
92       uint32_t component = types[0];
93       // Obtain the image buffer.
94       OH_NativeBuffer *imageBuffer = nullptr;
95       errCode = OH_ImageNative_GetByteBuffer(image, component, &imageBuffer);
96       size_t bufferSize = 0;
97       errCode = OH_ImageNative_GetBufferSize(image, component, &bufferSize);
98       OH_LOG_INFO(LOG_APP, "ImageReceiverNativeCTest buffer component: %{public}d size:%{public}zu", component, bufferSize);
99       // Obtain the row stride of the image.
100       int32_t stride = 0;
101       errCode = OH_ImageNative_GetRowStride(image, component, &stride);
102       OH_LOG_INFO(LOG_APP, "ImageReceiverNativeCTest buffer stride: %{public}d.", stride);
103       void* srcVir = nullptr;
104       OH_NativeBuffer_Map(imageBuffer, &srcVir);
105       uint8_t* srcBuffer = static_cast<uint8_t*>(srcVir);
106       // Check whether the row stride is the same as the width of the preview stream. If they are different, consider the impact of the stride on buffer reading.
107       if (stride == size.width) {
108           // Process the buffer by calling the API that does not support stride.
109       } else {
110           // Process the buffer by calling the API that supports stride or remove the stride data.
111           // The following uses the operation of removing the stride data as an example. Specifically, remove the stride data from the byteBuffer and obtain a new dstBuffer by means of copy.
112           size_t dstBufferSize = size.width * size.height * 1.5; // Camera preview stream in NV21 format.
113           std::unique_ptr<uint8_t[]> dstBuffer = std::make_unique<uint8_t[]>(dstBufferSize);
114           uint8_t *dstPtr = dstBuffer.get();
115           for (int j = 0; j < size.height * 1.5; j++) {
116               memcpy(dstPtr, srcBuffer, size.width);
117               dstPtr += size.width;
118               srcBuffer += stride;
119           }
120           // Process the buffer by calling the API that does not support stride.
121       }
122       // Release resources.
123       OH_NativeBuffer_Unmap(imageBuffer); // Release the buffer to ensure that the bufferQueue is rotated properly.
124       errCode = OH_ImageNative_Release(image);
125	   delete[] types;
126   }
127
128   void OnImageReceiver() {
129       // Register the callback to listen for the image content reported by each frame.
130       Image_ErrorCode errCode = OH_ImageReceiverNative_On(receiver, OnCallback);
131   }
132   ```
133