• 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 <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   )
36   ```
37
383. Initialize an [ImageReceiver](../image/image-receiver-c.md) instance and obtain a surface ID.
39
40   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.
41
42   ```c++
43   void InitImageReceiver() {
44       OH_ImageReceiverOptions *options = nullptr;
45       // Capture exceptions and check whether the instance is null. This example shows only the API call process.
46       // Set image parameters.
47       Image_ErrorCode errCode = OH_ImageReceiverOptions_Create(&options);
48       Image_Size imgSize;
49       imgSize.width = 1080; // Width of the created preview stream.
50       imgSize.height = 1080; // Height of the created preview stream.
51       int32_t capacity = 8; // Maximum number of images in BufferQueue. The recommended value is 8.
52       errCode = OH_ImageReceiverOptions_SetSize(options, imgSize);
53       errCode = OH_ImageReceiverOptions_SetCapacity(options, capacity);
54       // Create an OH_ImageReceiverNative instance.
55       OH_ImageReceiverNative *receiver = nullptr;
56       errCode = OH_ImageReceiverNative_Create(options, &receiver);
57       // Obtain the Surface ID of the OH_ImageReceiverNative instance.
58       uint64_t receiverSurfaceID = 0;
59       errCode = OH_ImageReceiverNative_GetReceivingSurfaceId(receiver, &receiverSurfaceID);
60       OH_LOG_INFO(LOG_APP, "receiver surfaceID:%{public}%llu", receiverSurfaceID);
61   }
62   ```
63
644. Create a preview stream based on the surface ID obtained. For details, see step 4 in [Camera Preview (C/C++)](./native-camera-preview.md).
65
665. Create a session and enable it. For details, see [Camera Session Management (C/C++)](./native-camera-session-management.md).
67
686. Register an ImageReceiver callback to listen for the image content reported by each frame.
69
70   ```c++
71   OH_ImageReceiverNative *receiver; // Instance created in step 3.
72   uint32_t PREVIEW_WIDTH = 1080; // Width of the created preview stream.
73   uint32_t PREVIEW_HEIGHT = 1080; // Height of the created preview stream.
74
75   // Image callback. For details, see Media/Image Kit.
76   static void OnCallback(OH_ImageReceiverNative *receiver) {
77       OH_LOG_INFO(LOG_APP, "ImageReceiverNativeCTest buffer available.");
78       // Capture exceptions and check whether the instance is null. This example shows only the API call process.
79       OH_ImageNative *image = nullptr;
80       // Obtain the image from the bufferQueue.
81       Image_ErrorCode errCode = OH_ImageReceiverNative_ReadNextImage(receiver, &image);
82       // Obtain the image's component type.
83       size_t typeSize = 0;
84       OH_ImageNative_GetComponentTypes(image, nullptr, &typeSize);
85       uint32_t* types = new uint32_t[typeSize];
86       OH_ImageNative_GetComponentTypes(image, &types, &typeSize);
87       uint32_t component = types[0];
88       // Obtain the image buffer.
89       OH_NativeBuffer *imageBuffer = nullptr;
90       errCode = OH_ImageNative_GetByteBuffer(image, component, &imageBuffer);
91       size_t bufferSize = 0;
92       errCode = OH_ImageNative_GetBufferSize(image, component, &bufferSize);
93       OH_LOG_INFO(LOG_APP, "ImageReceiverNativeCTest buffer component: %{public}d size:%{public}zu", component, bufferSize);
94       // Obtain the row stride of the image.
95       int32_t stride = 0;
96       errCode = OH_ImageNative_GetRowStride(image, component, &stride);
97       OH_LOG_INFO(LOG_APP, "ImageReceiverNativeCTest buffer stride: %{public}d.", stride);
98       void* srcVir = nullptr;
99       OH_NativeBuffer_Map(imageBuffer, &srcVir);
100       uint8_t* srcBuffer = static_cast<uint8_t*>(srcVir);
101       // 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.
102       if (stride == PREVIEW_WIDTH) {
103           // Process the buffer by calling the API that does not support stride.
104       } else {
105           // Process the buffer by calling the API that supports stride or remove the stride data.
106           // 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.
107           size_t dstBufferSize = PREVIEW_WIDTH * PREVIEW_HEIGHT * 1.5; // Camera preview stream in NV21 format.
108           std::unique_ptr<uint8_t[]> dstBuffer = std::make_unique<uint8_t[]>(dstBufferSize);
109           uint8_t *dstPtr = dstBuffer.get();
110           for (int j = 0; j < PREVIEW_HEIGHT * 1.5; j++) {
111               memcpy(dstPtr, srcBuffer, PREVIEW_WIDTH);
112               dstPtr += PREVIEW_WIDTH;
113               srcBuffer += stride;
114           }
115           // Process the buffer by calling the API that does not support stride.
116       }
117       // Release the buffer to ensure that the bufferQueue is rotated properly.
118       OH_NativeBuffer_Unmap(imageBuffer);
119       errCode = OH_ImageNative_Release(image);
120   }
121
122   void OnImageReceiver() {
123       // Register the callback to listen for the image content reported by each frame.
124       Image_ErrorCode errCode = OH_ImageReceiverNative_On(receiver, OnCallback);
125   }
126   ```
127