1e41f4b71Sopenharmony_ci# Using Image_NativeModule to Receive Images 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciYou can use the **ImageReceiver** class to obtain the surface ID of a component, read the latest image or the next image, and release **ImageReceiver** instances. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci## How to Develop 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci### Adding Dependencies 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciOpen the **src/main/cpp/CMakeLists.txt** file of the native project, add **libohimage.so**, **libimage_receiver.so**, **libnative_image.so**, and **libhilog_ndk.z.so** (on which the log APIs depend) to the **target_link_libraries** dependency. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci```txt 12e41f4b71Sopenharmony_citarget_link_libraries(entry PUBLIC libhilog_ndk.z.so libohimage.so libimage_receiver.so libnative_image.so) 13e41f4b71Sopenharmony_ci``` 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci### Calling the Native APIs 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ciFor details about the APIs, see [Image_NativeModule](../../reference/apis-image-kit/_image___native_module.md). 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ciImplement the C native APIs in **hello.cpp**. Refer to the sample code below. 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci```c++ 22e41f4b71Sopenharmony_ci#include <hilog/log.h> 23e41f4b71Sopenharmony_ci#include <multimedia/image_framework/image/image_native.h> 24e41f4b71Sopenharmony_ci#include <multimedia/image_framework/image/image_receiver_native.h> 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci#undef LOG_DOMAIN 27e41f4b71Sopenharmony_ci#define LOG_DOMAIN 0x3200 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci#undef LOG_TAG 30e41f4b71Sopenharmony_ci#define LOG_TAG "MY_TAG" 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci#define IMAGE_WIDTH 320 33e41f4b71Sopenharmony_ci#define IMAGE_HEIGHT 480 34e41f4b71Sopenharmony_ci#define IMAGE_CAPACITY 2 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_cistatic void OnCallback(OH_ImageReceiverNative *receiver) 37e41f4b71Sopenharmony_ci{ 38e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "ImageReceiverNativeCTest buffer avaliable."); 39e41f4b71Sopenharmony_ci} 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_cistatic void ImageReceiverNativeCTest() 42e41f4b71Sopenharmony_ci{ 43e41f4b71Sopenharmony_ci // Create an OH_ImageReceiverOptions instance. 44e41f4b71Sopenharmony_ci OH_ImageReceiverOptions* options = nullptr; 45e41f4b71Sopenharmony_ci Image_ErrorCode errCode = OH_ImageReceiverOptions_Create(&options); 46e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 47e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest create image receiver options failed, errCode: %{public}d.", errCode); 48e41f4b71Sopenharmony_ci return; 49e41f4b71Sopenharmony_ci } 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci Image_Size imgSize; 52e41f4b71Sopenharmony_ci imgSize.width = IMAGE_WIDTH; 53e41f4b71Sopenharmony_ci imgSize.height = IMAGE_HEIGHT; 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci // Set the size attribute of OH_ImageReceiverOptions. 56e41f4b71Sopenharmony_ci errCode = OH_ImageReceiverOptions_SetSize(options, imgSize); 57e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 58e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest set image receiver options size failed, errCode: %{public}d.", errCode); 59e41f4b71Sopenharmony_ci OH_ImageReceiverOptions_Release(options); 60e41f4b71Sopenharmony_ci return; 61e41f4b71Sopenharmony_ci } 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci // Set the capacity attribute of OH_ImageReceiverOptions. 64e41f4b71Sopenharmony_ci errCode = OH_ImageReceiverOptions_SetCapacity(options, IMAGE_CAPACITY); 65e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 66e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest set image receiver options capacity failed, errCode: %{public}d.", errCode); 67e41f4b71Sopenharmony_ci OH_ImageReceiverOptions_Release(options); 68e41f4b71Sopenharmony_ci return; 69e41f4b71Sopenharmony_ci } 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci // Read the size attribute of OH_ImageReceiverOptions. 72e41f4b71Sopenharmony_ci Image_Size imgSizeRead; 73e41f4b71Sopenharmony_ci errCode = OH_ImageReceiverOptions_GetSize(options, &imgSizeRead); 74e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 75e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest get image receiver options size failed, errCode: %{public}d.", errCode); 76e41f4b71Sopenharmony_ci OH_ImageReceiverOptions_Release(options); 77e41f4b71Sopenharmony_ci return; 78e41f4b71Sopenharmony_ci } 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci // Check whether the size read is the same as the size set. 81e41f4b71Sopenharmony_ci if (imgSizeRead.width != IMAGE_WIDTH || imgSizeRead.height != IMAGE_HEIGHT) { 82e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest get image receiver options size failed, width: %{public}d, height: %{public}d.", imgSizeRead.width, imgSizeRead.height); 83e41f4b71Sopenharmony_ci OH_ImageReceiverOptions_Release(options); 84e41f4b71Sopenharmony_ci return; 85e41f4b71Sopenharmony_ci } 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ci // Read the capacity attribute of OH_ImageReceiverOptions. 88e41f4b71Sopenharmony_ci int32_t capacity = 0; 89e41f4b71Sopenharmony_ci errCode = OH_ImageReceiverOptions_GetCapacity(options, &capacity); 90e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 91e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest get image receiver options capacity failed, errCode: %{public}d.", errCode); 92e41f4b71Sopenharmony_ci OH_ImageReceiverOptions_Release(options); 93e41f4b71Sopenharmony_ci return; 94e41f4b71Sopenharmony_ci } 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci // Check whether the capacity read is the same as the capacity set. 97e41f4b71Sopenharmony_ci if (capacity != IMAGE_CAPACITY) { 98e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest get image receiver options capacity failed, capacity: %{public}d.", capacity); 99e41f4b71Sopenharmony_ci OH_ImageReceiverOptions_Release(options); 100e41f4b71Sopenharmony_ci return; 101e41f4b71Sopenharmony_ci } 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci // Create an OH_ImageReceiverNative instance. 104e41f4b71Sopenharmony_ci OH_ImageReceiverNative* receiver = nullptr; 105e41f4b71Sopenharmony_ci errCode = OH_ImageReceiverNative_Create(options, &receiver); 106e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 107e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest create image receiver failed, errCode: %{public}d.", errCode); 108e41f4b71Sopenharmony_ci OH_ImageReceiverOptions_Release(options); 109e41f4b71Sopenharmony_ci return; 110e41f4b71Sopenharmony_ci } 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ci // Register a callback event. Each time a new image is received, the callback event is triggered. 113e41f4b71Sopenharmony_ci uint64_t surfaceID = 0; 114e41f4b71Sopenharmony_ci errCode = OH_ImageReceiverNative_On(receiver, OnCallback); 115e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 116e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest image receiver on failed, errCode: %{public}d.", errCode); 117e41f4b71Sopenharmony_ci OH_ImageReceiverOptions_Release(options); 118e41f4b71Sopenharmony_ci OH_ImageReceiverNative_Release(receiver); 119e41f4b71Sopenharmony_ci return; 120e41f4b71Sopenharmony_ci } 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci // Read the surface ID attribute of OH_ImageReceiverNative. 123e41f4b71Sopenharmony_ci uint64_t surfaceID = 0; 124e41f4b71Sopenharmony_ci errCode = OH_ImageReceiverNative_GetReceivingSurfaceId(receiver, &surfaceID); 125e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 126e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest get image receiver surfaceID failed, errCode: %{public}d.", errCode); 127e41f4b71Sopenharmony_ci OH_ImageReceiverOptions_Release(options); 128e41f4b71Sopenharmony_ci OH_ImageReceiverNative_Release(receiver); 129e41f4b71Sopenharmony_ci return; 130e41f4b71Sopenharmony_ci } 131e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "ImageReceiverNativeCTest get image receiver surfaceID: %{public}llu.", surfaceID); 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ci // Read the size attribute of OH_ImageReceiverNative. 134e41f4b71Sopenharmony_ci errCode = OH_ImageReceiverNative_GetSize(receiver, &imgSizeRead); 135e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 136e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest get image receiver size failed, errCode: %{public}d.", errCode); 137e41f4b71Sopenharmony_ci OH_ImageReceiverOptions_Release(options); 138e41f4b71Sopenharmony_ci OH_ImageReceiverNative_Release(receiver); 139e41f4b71Sopenharmony_ci return; 140e41f4b71Sopenharmony_ci } 141e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "ImageReceiverNativeCTest get image receiver size: width = %{public}d, height = %{public}d.", imgSizeRead.width, imgSizeRead.height); 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ci // Read the capacity attribute of OH_ImageReceiverNative. 144e41f4b71Sopenharmony_ci errCode = OH_ImageReceiverNative_GetCapacity(receiver, &capacity); 145e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 146e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest get image receiver capacity failed, errCode: %{public}d.", errCode); 147e41f4b71Sopenharmony_ci OH_ImageReceiverOptions_Release(options); 148e41f4b71Sopenharmony_ci OH_ImageReceiverNative_Release(receiver); 149e41f4b71Sopenharmony_ci return; 150e41f4b71Sopenharmony_ci } 151e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "ImageReceiverNativeCTest get image receiver capacity: %{public}d.", capacity); 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ci // Read the next image object of OH_ImageReceiverNative. 154e41f4b71Sopenharmony_ci OH_ImageNative* image = nullptr; 155e41f4b71Sopenharmony_ci errCode = OH_ImageReceiverNative_ReadNextImage(receiver, &image); 156e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 157e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest get image receiver next image failed, errCode: %{public}d.", errCode); 158e41f4b71Sopenharmony_ci OH_ImageReceiverOptions_Release(options); 159e41f4b71Sopenharmony_ci OH_ImageReceiverNative_Release(receiver); 160e41f4b71Sopenharmony_ci return; 161e41f4b71Sopenharmony_ci } 162e41f4b71Sopenharmony_ci 163e41f4b71Sopenharmony_ci // Release the OH_ImageNative instance. 164e41f4b71Sopenharmony_ci errCode = OH_ImageNative_Release(image); 165e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 166e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest release image native failed, errCode: %{public}d.", errCode); 167e41f4b71Sopenharmony_ci } 168e41f4b71Sopenharmony_ci 169e41f4b71Sopenharmony_ci // Read the latest image object of OH_ImageReceiverNative. 170e41f4b71Sopenharmony_ci errCode = OH_ImageReceiverNative_ReadLatestImage(receiver, &image); 171e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 172e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest get image receiver latest image failed, errCode: %{public}d.", errCode); 173e41f4b71Sopenharmony_ci OH_ImageReceiverOptions_Release(options); 174e41f4b71Sopenharmony_ci OH_ImageReceiverNative_Release(receiver); 175e41f4b71Sopenharmony_ci return; 176e41f4b71Sopenharmony_ci } 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_ci // Release the OH_ImageNative instance. 179e41f4b71Sopenharmony_ci errCode = OH_ImageNative_Release(image); 180e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 181e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest release image native failed, errCode: %{public}d.", errCode); 182e41f4b71Sopenharmony_ci } 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ci // Release the OH_ImageReceiverOptions instance. 185e41f4b71Sopenharmony_ci errCode = OH_ImageReceiverOptions_Release(options); 186e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 187e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest release image receiver options failed, errCode: %{public}d.", errCode); 188e41f4b71Sopenharmony_ci } 189e41f4b71Sopenharmony_ci 190e41f4b71Sopenharmony_ci // Unregister the callback event registered by calling OH_ImageReceiverNative_On. 191e41f4b71Sopenharmony_ci errCode = OH_ImageReceiverNative_Off(receiver); 192e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 193e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest image receiver off failed, errCode: %{public}d.", errCode); 194e41f4b71Sopenharmony_ci } 195e41f4b71Sopenharmony_ci 196e41f4b71Sopenharmony_ci // Release the OH_ImageReceiverOptions instance. 197e41f4b71Sopenharmony_ci errCode = OH_ImageReceiverNative_Release(receiver); 198e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 199e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest release image receiver failed, errCode: %{public}d.", errCode); 200e41f4b71Sopenharmony_ci } 201e41f4b71Sopenharmony_ci} 202e41f4b71Sopenharmony_ci``` 203