1e41f4b71Sopenharmony_ci# Using Image_NativeModule to Process Image Information 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciYou can use the **ImageInfo** class to set and read the rectangle size, component information, and pixel information of an image. 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 ImageNativeCTest() 37e41f4b71Sopenharmony_ci{ 38e41f4b71Sopenharmony_ci // Create an OH_ImageReceiverOptions instance. 39e41f4b71Sopenharmony_ci OH_ImageReceiverOptions* options = nullptr; 40e41f4b71Sopenharmony_ci Image_ErrorCode errCode = OH_ImageReceiverOptions_Create(&options); 41e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 42e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest create image receiver options failed, errCode: %{public}d.", errCode); 43e41f4b71Sopenharmony_ci return; 44e41f4b71Sopenharmony_ci } 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci Image_Size imgSize; 47e41f4b71Sopenharmony_ci imgSize.width = IMAGE_WIDTH; 48e41f4b71Sopenharmony_ci imgSize.height = IMAGE_HEIGHT; 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci // Set the size attribute of OH_ImageReceiverOptions. 51e41f4b71Sopenharmony_ci errCode = OH_ImageReceiverOptions_SetSize(options, imgSize); 52e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 53e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest set image receiver options size failed, errCode: %{public}d.", errCode); 54e41f4b71Sopenharmony_ci OH_ImageReceiverOptions_Release(options); 55e41f4b71Sopenharmony_ci return; 56e41f4b71Sopenharmony_ci } 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci // Set the capacity attribute of OH_ImageReceiverOptions. 59e41f4b71Sopenharmony_ci errCode = OH_ImageReceiverOptions_SetCapacity(options, IMAGE_CAPACITY); 60e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 61e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest set image receiver options capacity failed, errCode: %{public}d.", errCode); 62e41f4b71Sopenharmony_ci OH_ImageReceiverOptions_Release(options); 63e41f4b71Sopenharmony_ci return; 64e41f4b71Sopenharmony_ci } 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci // Create an OH_ImageReceiverNative instance. 67e41f4b71Sopenharmony_ci OH_ImageReceiverNative* receiver = nullptr; 68e41f4b71Sopenharmony_ci errCode = OH_ImageReceiverNative_Create(options, &receiver); 69e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 70e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest create image receiver failed, errCode: %{public}d.", errCode); 71e41f4b71Sopenharmony_ci OH_ImageReceiverOptions_Release(options); 72e41f4b71Sopenharmony_ci return; 73e41f4b71Sopenharmony_ci } 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci // Read the next image object of OH_ImageReceiverNative. 76e41f4b71Sopenharmony_ci OH_ImageNative* image = nullptr; 77e41f4b71Sopenharmony_ci errCode = OH_ImageReceiverNative_ReadNextImage(receiver, &image); 78e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 79e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest get image receiver next image failed, errCode: %{public}d.", errCode); 80e41f4b71Sopenharmony_ci OH_ImageReceiverOptions_Release(options);OH_ImageReceiverNative_Release(receiver); 81e41f4b71Sopenharmony_ci return; 82e41f4b71Sopenharmony_ci } 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci // Read the size attribute of OH_ImageNative. 85e41f4b71Sopenharmony_ci errCode = OH_ImageNative_GetImageSize(image, &imgSizeRead); 86e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 87e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest get image size failed, errCode: %{public}d.", errCode); 88e41f4b71Sopenharmony_ci OH_ImageNative_Release(image); 89e41f4b71Sopenharmony_ci OH_ImageReceiverOptions_Release(options);OH_ImageReceiverNative_Release(receiver); 90e41f4b71Sopenharmony_ci return; 91e41f4b71Sopenharmony_ci } 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci // Read the number of elements in the component list of OH_ImageNative. 94e41f4b71Sopenharmony_ci size_t componentTypeSize = 0; 95e41f4b71Sopenharmony_ci errCode = OH_ImageNative_GetComponentTypes(image, nullptr, &componentTypeSize); 96e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 97e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest get image component types failed, errCode: %{public}d.", errCode); 98e41f4b71Sopenharmony_ci OH_ImageNative_Release(image); 99e41f4b71Sopenharmony_ci OH_ImageReceiverOptions_Release(options);OH_ImageReceiverNative_Release(receiver); 100e41f4b71Sopenharmony_ci return; 101e41f4b71Sopenharmony_ci } 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci // Read the component list of OH_ImageNative. 104e41f4b71Sopenharmony_ci uint32_t* components = new uint32_t[componentTypeSize]; 105e41f4b71Sopenharmony_ci errCode = OH_ImageNative_GetComponentTypes(image, &components, &componentTypeSize); 106e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 107e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest get image component types failed, errCode: %{public}d.", errCode); 108e41f4b71Sopenharmony_ci delete [] components; 109e41f4b71Sopenharmony_ci OH_ImageNative_Release(image); 110e41f4b71Sopenharmony_ci OH_ImageReceiverOptions_Release(options);OH_ImageReceiverNative_Release(receiver); 111e41f4b71Sopenharmony_ci return; 112e41f4b71Sopenharmony_ci } 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci delete [] components; 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci // Read the buffer object corresponding to the first component of OH_ImageNative. 117e41f4b71Sopenharmony_ci OH_NativeBuffer* nativeBuffer = nullptr; 118e41f4b71Sopenharmony_ci errCode = OH_ImageNative_GetByteBuffer(image, components[0], &nativeBuffer); 119e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 120e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest get image byte buffer failed, errCode: %{public}d.", errCode); 121e41f4b71Sopenharmony_ci OH_ImageNative_Release(image); 122e41f4b71Sopenharmony_ci OH_ImageReceiverOptions_Release(options);OH_ImageReceiverNative_Release(receiver); 123e41f4b71Sopenharmony_ci return; 124e41f4b71Sopenharmony_ci } 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ci // Read the size of the buffer corresponding to the first component of OH_ImageNative. 127e41f4b71Sopenharmony_ci size_t nativeBufferSize = 0; 128e41f4b71Sopenharmony_ci errCode = OH_ImageNative_GetBufferSize(image, components[0], &nativeBufferSize); 129e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 130e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest get image buffer size failed, errCode: %{public}d.", errCode); 131e41f4b71Sopenharmony_ci OH_ImageNative_Release(image); 132e41f4b71Sopenharmony_ci OH_ImageReceiverOptions_Release(options);OH_ImageReceiverNative_Release(receiver); 133e41f4b71Sopenharmony_ci return; 134e41f4b71Sopenharmony_ci } 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_ci // Read the row stride corresponding to the first component of OH_ImageNative. 137e41f4b71Sopenharmony_ci int32_t rowStride = 0; 138e41f4b71Sopenharmony_ci errCode = OH_ImageNative_GetRowStride(image, components[0], &rowStride); 139e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 140e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest get image row stride failed, errCode: %{public}d.", errCode); 141e41f4b71Sopenharmony_ci OH_ImageNative_Release(image); 142e41f4b71Sopenharmony_ci OH_ImageReceiverOptions_Release(options);OH_ImageReceiverNative_Release(receiver); 143e41f4b71Sopenharmony_ci return; 144e41f4b71Sopenharmony_ci } 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_ci // Read the pixel stride corresponding to the first component of OH_ImageNative. 147e41f4b71Sopenharmony_ci int32_t pixelStride = 0; 148e41f4b71Sopenharmony_ci errCode = OH_ImageNative_GetPixelStride(image, components[0], &pixelStride); 149e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 150e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest get image pixel stride failed, errCode: %{public}d.", errCode); 151e41f4b71Sopenharmony_ci OH_ImageNative_Release(image); 152e41f4b71Sopenharmony_ci OH_ImageReceiverOptions_Release(options);OH_ImageReceiverNative_Release(receiver); 153e41f4b71Sopenharmony_ci return; 154e41f4b71Sopenharmony_ci } 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_ci // Release the OH_ImageNative instance. 157e41f4b71Sopenharmony_ci errCode = OH_ImageNative_Release(image); 158e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 159e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest release image native failed, errCode: %{public}d.", errCode); 160e41f4b71Sopenharmony_ci } 161e41f4b71Sopenharmony_ci 162e41f4b71Sopenharmony_ci // Release the OH_ImageReceiverOptions instance. 163e41f4b71Sopenharmony_ci errCode = OH_ImageReceiverOptions_Release(options); 164e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 165e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest release image receiver options failed, errCode: %{public}d.", errCode); 166e41f4b71Sopenharmony_ci } 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ci // Release the OH_ImageReceiverOptions instance. 169e41f4b71Sopenharmony_ci errCode = OH_ImageReceiverNative_Release(receiver); 170e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 171e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImageReceiverNativeCTest release image receiver failed, errCode: %{public}d.", errCode); 172e41f4b71Sopenharmony_ci } 173e41f4b71Sopenharmony_ci} 174e41f4b71Sopenharmony_ci``` 175