1e41f4b71Sopenharmony_ci# Using Image to Receive Images 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciYou can use the **ImageReceiver** APIs 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 **libace_napi.z.so**, **libimage_ndk.z.so**, **libimage_receiver_ndk.z.so**, **libnative_image.so**, and **libhilog_ndk.z.so** (on which the native log APIs depend) to the **target_link_libraries** dependency. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci```txt 12e41f4b71Sopenharmony_citarget_link_libraries(entry PUBLIC libace_napi.z.so libhilog_ndk.z.so libimage_ndk.z.so libimage_receiver_ndk.z.so libnative_image.so) 13e41f4b71Sopenharmony_ci``` 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci### Adding API Mappings 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ciOpen the **src/main/cpp/hello.cpp** file and add the following API mappings to the **Init** function: 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci```c++ 20e41f4b71Sopenharmony_ciEXTERN_C_START 21e41f4b71Sopenharmony_cistatic napi_value Init(napi_env env, napi_value exports) 22e41f4b71Sopenharmony_ci{ 23e41f4b71Sopenharmony_ci napi_property_descriptor desc[] = { 24e41f4b71Sopenharmony_ci { "createFromReceiver", nullptr, createFromReceiver, nullptr, nullptr, nullptr, napi_default, nullptr }, 25e41f4b71Sopenharmony_ci }; 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); 28e41f4b71Sopenharmony_ci return exports; 29e41f4b71Sopenharmony_ci} 30e41f4b71Sopenharmony_ciEXTERN_C_END 31e41f4b71Sopenharmony_ci``` 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci### Requesting Permissions 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ciTo obtain input data of an image from a camera, you must request the **ohos.permission.CAMERA** permission. For details, see [Requesting User Authorization](../../security/AccessToken/request-user-authorization.md). 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci### Calling APIs on the JS Side 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci1. Open **src\main\cpp\types\*libentry*\index.d.ts** (where **libentry** varies according to the project name), and import the following files: 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci ```js 42e41f4b71Sopenharmony_ci import { image } from '@kit.ImageKit'; 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci export const createFromReceiver: (a: image.ImageReceiver) => image.Image; 45e41f4b71Sopenharmony_ci ``` 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci2. Open **src\main\ets\pages\index.ets**, import ***libentry*.so** (where **libentry** varies according to the project name), call the native APIs, and pass in the JS resource object. The following is an example: 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci ```js 50e41f4b71Sopenharmony_ci import testNapi from 'libentry.so' 51e41f4b71Sopenharmony_ci import { image } from '@kit.ImageKit'; 52e41f4b71Sopenharmony_ci import { abilityAccessCtrl } from '@kit.AbilityKit'; 53e41f4b71Sopenharmony_ci import { camera } from '@kit.CameraKit'; 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci @Entry 56e41f4b71Sopenharmony_ci @Component 57e41f4b71Sopenharmony_ci struct Index { 58e41f4b71Sopenharmony_ci private receiver: image.ImageReceiver | undefined = undefined; 59e41f4b71Sopenharmony_ci func (){ 60e41f4b71Sopenharmony_ci let context = getContext() 61e41f4b71Sopenharmony_ci abilityAccessCtrl.createAtManager().requestPermissionsFromUser(context,['ohos.permission.CAMERA']).then(async () => { 62e41f4b71Sopenharmony_ci let cameraManager = await camera.getCameraManager(context); 63e41f4b71Sopenharmony_ci // Obtain the supported camera devices. 64e41f4b71Sopenharmony_ci let cameraDevices: Array<camera.CameraDevice> = cameraManager.getSupportedCameras(); 65e41f4b71Sopenharmony_ci if (cameraDevices.length <= 0) { 66e41f4b71Sopenharmony_ci return; 67e41f4b71Sopenharmony_ci } 68e41f4b71Sopenharmony_ci // Obtain the profiles of the cameras. 69e41f4b71Sopenharmony_ci let profiles: camera.CameraOutputCapability = cameraManager.getSupportedOutputCapability(cameraDevices[0]) 70e41f4b71Sopenharmony_ci let previewProfiles: Array<camera.Profile> = profiles.previewProfiles; 71e41f4b71Sopenharmony_ci if (previewProfiles.length <= 0) { 72e41f4b71Sopenharmony_ci return; 73e41f4b71Sopenharmony_ci } 74e41f4b71Sopenharmony_ci let profileObj = previewProfiles[0]; 75e41f4b71Sopenharmony_ci this.receiver = image.createImageReceiver(profileObj.size.width, profileObj.size.height, image.ImageFormat.JPEG, 8); 76e41f4b71Sopenharmony_ci let receiverSurfaceId: string = await this.receiver.getReceivingSurfaceId(); 77e41f4b71Sopenharmony_ci // Create an output object for the preview stream. 78e41f4b71Sopenharmony_ci let previewOutput: camera.PreviewOutput = cameraManager.createPreviewOutput(profileObj,receiverSurfaceId); 79e41f4b71Sopenharmony_ci let cameraInput : camera.CameraInput = cameraManager.createCameraInput(cameraDevices[0]); 80e41f4b71Sopenharmony_ci // Open a camera. 81e41f4b71Sopenharmony_ci await cameraInput.open(); 82e41f4b71Sopenharmony_ci // Create a session. 83e41f4b71Sopenharmony_ci let captureSession : camera.CaptureSession = cameraManager.createCaptureSession(); 84e41f4b71Sopenharmony_ci // Configure the session. 85e41f4b71Sopenharmony_ci captureSession.beginConfig(); 86e41f4b71Sopenharmony_ci // Add a CameraInput instance to the session. 87e41f4b71Sopenharmony_ci captureSession.addInput(cameraInput); 88e41f4b71Sopenharmony_ci // Add the preview stream to the session. 89e41f4b71Sopenharmony_ci captureSession.addOutput(previewOutput); 90e41f4b71Sopenharmony_ci // Commit the configuration. 91e41f4b71Sopenharmony_ci await captureSession.commitConfig(); 92e41f4b71Sopenharmony_ci // Start the session. 93e41f4b71Sopenharmony_ci await captureSession.start(); 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci this.receiver.on('imageArrival', () => { 96e41f4b71Sopenharmony_ci let img : image.Image = testNapi.createFromReceiver(this.receiver); 97e41f4b71Sopenharmony_ci img.release(); 98e41f4b71Sopenharmony_ci }) 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ci }); 101e41f4b71Sopenharmony_ci } 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci build() { 104e41f4b71Sopenharmony_ci Row() { 105e41f4b71Sopenharmony_ci Column() { 106e41f4b71Sopenharmony_ci Button("start") 107e41f4b71Sopenharmony_ci .width(100) 108e41f4b71Sopenharmony_ci .height(100) 109e41f4b71Sopenharmony_ci .onClick(() => { 110e41f4b71Sopenharmony_ci console.log("button click in"); 111e41f4b71Sopenharmony_ci if (this.receiver == undefined) { 112e41f4b71Sopenharmony_ci this.func(); 113e41f4b71Sopenharmony_ci } 114e41f4b71Sopenharmony_ci }) 115e41f4b71Sopenharmony_ci } 116e41f4b71Sopenharmony_ci .width('100%') 117e41f4b71Sopenharmony_ci } 118e41f4b71Sopenharmony_ci .height('100%') 119e41f4b71Sopenharmony_ci } 120e41f4b71Sopenharmony_ci } 121e41f4b71Sopenharmony_ci ``` 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ci### Calling the Native APIs 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ciFor details about the APIs, see [Image API Reference](../../reference/apis-image-kit/image.md). 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ciObtain the JS resource object from the **hello.cpp** file and convert it to a native resource object. Then you can call native APIs. 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci**Adding Reference Files** 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci ```c++ 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ci #include <multimedia/image_framework/image_mdk.h> 134e41f4b71Sopenharmony_ci #include <multimedia/image_framework/image_receiver_mdk.h> 135e41f4b71Sopenharmony_ci #include <malloc.h> 136e41f4b71Sopenharmony_ci #include <hilog/log.h> 137e41f4b71Sopenharmony_ci 138e41f4b71Sopenharmony_ci static napi_value createFromReceiver(napi_env env, napi_callback_info info) 139e41f4b71Sopenharmony_ci { 140e41f4b71Sopenharmony_ci size_t argc = 1; 141e41f4b71Sopenharmony_ci napi_value args[2] = {nullptr}; 142e41f4b71Sopenharmony_ci napi_get_cb_info(env, info, &argc, args , nullptr, nullptr); 143e41f4b71Sopenharmony_ci napi_valuetype valuetype0; 144e41f4b71Sopenharmony_ci napi_typeof(env, args[0], &valuetype0); 145e41f4b71Sopenharmony_ci napi_ref reference; 146e41f4b71Sopenharmony_ci napi_create_reference(env, args[0], 1 ,&reference); 147e41f4b71Sopenharmony_ci napi_value imgReceiver_js; 148e41f4b71Sopenharmony_ci napi_get_reference_value(env, reference, &imgReceiver_js); 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_ci ImageReceiverNative * imgReceiver_c = OH_Image_Receiver_InitImageReceiverNative(env, imgReceiver_js); 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci int32_t capacity; 153e41f4b71Sopenharmony_ci OH_Image_Receiver_GetCapacity(imgReceiver_c, &capacity); 154e41f4b71Sopenharmony_ci OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "[receiver]", "capacity: %{public}d", capacity); 155e41f4b71Sopenharmony_ci int32_t format; 156e41f4b71Sopenharmony_ci OH_Image_Receiver_GetFormat(imgReceiver_c, &format); 157e41f4b71Sopenharmony_ci OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "[receiver]", "format: %{public}d", format); 158e41f4b71Sopenharmony_ci char * surfaceId = static_cast<char *>(malloc(sizeof(char))); 159e41f4b71Sopenharmony_ci OH_Image_Receiver_GetReceivingSurfaceId(imgReceiver_c, surfaceId, sizeof(char)); 160e41f4b71Sopenharmony_ci OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "[receiver]", "surfaceId: %{public}c", surfaceId[0]); 161e41f4b71Sopenharmony_ci OhosImageSize size; 162e41f4b71Sopenharmony_ci OH_Image_Receiver_GetSize(imgReceiver_c, &size); 163e41f4b71Sopenharmony_ci OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "[receiver]", "OH_Image_Receiver_GetSize width: %{public}d, height:%{public}d", size.width, size.height); 164e41f4b71Sopenharmony_ci 165e41f4b71Sopenharmony_ci int32_t ret; 166e41f4b71Sopenharmony_ci napi_value nextImage; 167e41f4b71Sopenharmony_ci // Alternatively, call OH_Image_Receiver_ReadNextImage(imgReceiver_c, &nextImage). 168e41f4b71Sopenharmony_ci ret = OH_Image_Receiver_ReadLatestImage(imgReceiver_c, &nextImage); 169e41f4b71Sopenharmony_ci 170e41f4b71Sopenharmony_ci ImageNative * nextImage_native = OH_Image_InitImageNative(env, nextImage); 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ci OhosImageSize imageSize; 173e41f4b71Sopenharmony_ci OH_Image_Size(nextImage_native, &imageSize); 174e41f4b71Sopenharmony_ci OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "[receiver]", "OH_Image_Size width: %{public}d, height:%{public}d", imageSize.width, imageSize.height); 175e41f4b71Sopenharmony_ci 176e41f4b71Sopenharmony_ci OhosImageComponent imgComponent; 177e41f4b71Sopenharmony_ci ret = OH_Image_GetComponent(nextImage_native, 4, &imgComponent); // 4=jpeg 178e41f4b71Sopenharmony_ci 179e41f4b71Sopenharmony_ci uint8_t *img_buffer = imgComponent.byteBuffer; 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_ci ret = OH_Image_Release(nextImage_native); 182e41f4b71Sopenharmony_ci ret = OH_Image_Receiver_Release(imgReceiver_c); 183e41f4b71Sopenharmony_ci return nextImage; 184e41f4b71Sopenharmony_ci } 185e41f4b71Sopenharmony_ci ``` 186