图像接收类,用于获取组件surface id,接收最新的图片和读取下一张图片,以及释放ImageReceiver实例。
开发步骤
添加依赖
在进行应用开发之前,开发者需要打开native工程的src/main/cpp/CMakeLists.txt,在target_link_libraries依赖中添加libace_napi.z.so,libimage_ndk.z.so,libimage_receiver_ndk.z.so,libnative_image.so 以及日志依赖libhilog_ndk.z.so。
target_link_libraries(entry PUBLIC libace_napi.z.so libhilog_ndk.z.so libimage_ndk.z.so libimage_receiver_ndk.z.so libnative_image.so)
添加接口映射
打开src/main/cpp/hello.cpp文件,在Init函数中添加接口映射如下:
EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{napi_property_descriptor desc[] = {{ "createFromReceiver", nullptr, createFromReceiver, nullptr, nullptr, nullptr, napi_default, nullptr },};napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);return exports;
}
EXTERN_C_END
添加权限申请
此处通过camera图片获取输入数据,需要申请权限ohos.permission.CAMERA,申请方式请参考[向用户申请授权]
JS侧调用
- 打开src\main\cpp\types\libentry\index.d.ts(其中libentry根据工程名生成),导入如下引用文件:
import { image } from '@kit.ImageKit';export const createFromReceiver: (a: image.ImageReceiver) => image.Image;
- 打开src\main\ets\pages\index.ets,导入"libentry.so(根据工程名生成)",调用Native接口,传入JS的资源对象。示例如下:
import testNapi from 'libentry.so'
import { image } from '@kit.ImageKit';
import { abilityAccessCtrl } from '@kit.AbilityKit';
import { camera } from '@kit.CameraKit';@Entry
@Component
struct Index {private receiver: image.ImageReceiver | undefined = undefined;func (){let context = getContext()abilityAccessCtrl.createAtManager().requestPermissionsFromUser(context,['ohos.permission.CAMERA']).then(async () => {let cameraManager = await camera.getCameraManager(context);// 获取支持的相机设备对象let cameraDevices: Array<camera.CameraDevice> = cameraManager.getSupportedCameras();if (cameraDevices.length <= 0) {return;}// 获取对应相机设备的profileslet profiles: camera.CameraOutputCapability = cameraManager.getSupportedOutputCapability(cameraDevices[0])let previewProfiles: Array<camera.Profile> = profiles.previewProfiles;if (previewProfiles.length <= 0) {return;}let profileObj = previewProfiles[0];this.receiver = image.createImageReceiver(profileObj.size.width, profileObj.size.height, image.ImageFormat.JPEG, 8);let receiverSurfaceId: string = await this.receiver.getReceivingSurfaceId();// 创建预览流输出对象let previewOutput: camera.PreviewOutput = cameraManager.createPreviewOutput(profileObj,receiverSurfaceId);let cameraInput : camera.CameraInput = cameraManager.createCameraInput(cameraDevices[0]);// 打开相机await cameraInput.open();// 会话流程let captureSession : camera.CaptureSession = cameraManager.createCaptureSession();// 配置会话captureSession.beginConfig();// 把cameraInput加入到会话captureSession.addInput(cameraInput);// 把预览流加入到会话captureSession.addOutput(previewOutput);// 提交配置信息await captureSession.commitConfig();// 会话开始await captureSession.start();this.receiver.on('imageArrival', () => {let img : image.Image = testNapi.createFromReceiver(this.receiver);img.release();})});}build() {Row() {Column() {Button("start").width(100).height(100).onClick(() => {console.log("button click in");if (this.receiver == undefined) {this.func();}})}.width('100%')}.height('100%')}
}
Native接口调用
在hello.cpp文件中获取JS的资源对象,并转为Native的资源对象,即可调用Native接口,调用方式示例代码如下:
添加引用文件
#include <multimedia/image_framework/image_mdk.h>#include <multimedia/image_framework/image_receiver_mdk.h>#include <malloc.h>#include <hilog/log.h>static napi_value createFromReceiver(napi_env env, napi_callback_info info){size_t argc = 1;napi_value args[2] = {nullptr};napi_get_cb_info(env, info, &argc, args , nullptr, nullptr);napi_valuetype valuetype0;napi_typeof(env, args[0], &valuetype0);napi_ref reference;napi_create_reference(env, args[0], 1 ,&reference);napi_value imgReceiver_js;napi_get_reference_value(env, reference, &imgReceiver_js);ImageReceiverNative * imgReceiver_c = OH_Image_Receiver_InitImageReceiverNative(env, imgReceiver_js);int32_t capacity;OH_Image_Receiver_GetCapacity(imgReceiver_c, &capacity);OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "[receiver]", "capacity: %{public}d", capacity);int32_t format;OH_Image_Receiver_GetFormat(imgReceiver_c, &format);OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "[receiver]", "format: %{public}d", format);char * surfaceId = static_cast<char *>(malloc(sizeof(char)));OH_Image_Receiver_GetReceivingSurfaceId(imgReceiver_c, surfaceId, sizeof(char));OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "[receiver]", "surfaceId: %{public}c", surfaceId[0]);OhosImageSize size;OH_Image_Receiver_GetSize(imgReceiver_c, &size);OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "[receiver]", "OH_Image_Receiver_GetSize width: %{public}d, height:%{public}d", size.width, size.height);int32_t ret;napi_value nextImage;// 或调用 OH_Image_Receiver_ReadNextImage(imgReceiver_c, &nextImage);ret = OH_Image_Receiver_ReadLatestImage(imgReceiver_c, &nextImage);ImageNative * nextImage_native = OH_Image_InitImageNative(env, nextImage);OhosImageSize imageSize;OH_Image_Size(nextImage_native, &imageSize);OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "[receiver]", "OH_Image_Size width: %{public}d, height:%{public}d", imageSize.width, imageSize.height);OhosImageComponent imgComponent;ret = OH_Image_GetComponent(nextImage_native, 4, &imgComponent); // 4=jpeguint8_t *img_buffer = imgComponent.byteBuffer;ret = OH_Image_Release(nextImage_native);ret = OH_Image_Receiver_Release(imgReceiver_c);return nextImage;}
最后呢
很多开发朋友不知道需要学习那些鸿蒙技术?鸿蒙开发岗位需要掌握那些核心技术点?为此鸿蒙的开发学习必须要系统性的进行。
而网上有关鸿蒙的开发资料非常的少,假如你想学好鸿蒙的应用开发与系统底层开发。你可以参考这份资料,少走很多弯路,节省没必要的麻烦。由两位前阿里高级研发工程师联合打造的《鸿蒙NEXT星河版OpenHarmony开发文档》里面内容包含了(ArkTS、ArkUI开发组件、Stage模型、多端部署、分布式应用开发、音频、视频、WebGL、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、Harmony南向开发、鸿蒙项目实战等等)鸿蒙(Harmony NEXT)技术知识点
如果你是一名Android、Java、前端等等开发人员,想要转入鸿蒙方向发展。可以直接领取这份资料辅助你的学习。下面是鸿蒙开发的学习路线图。
针对鸿蒙成长路线打造的鸿蒙学习文档。话不多说,我们直接看详细鸿蒙(OpenHarmony )手册(共计1236页)与鸿蒙(OpenHarmony )开发入门视频,帮助大家在技术的道路上更进一步。
- 《鸿蒙 (OpenHarmony)开发学习视频》
- 《鸿蒙生态应用开发V2.0白皮书》
- 《鸿蒙 (OpenHarmony)开发基础到实战手册》
- OpenHarmony北向、南向开发环境搭建
- 《鸿蒙开发基础》
- 《鸿蒙开发进阶》
- 《鸿蒙开发实战》
总结
鸿蒙—作为国家主力推送的国产操作系统。部分的高校已经取消了安卓课程,从而开设鸿蒙课程;企业纷纷跟进启动了鸿蒙研发。
并且鸿蒙是完全具备无与伦比的机遇和潜力的;预计到年底将有 5,000 款的应用完成原生鸿蒙开发,未来将会支持 50 万款的应用。那么这么多的应用需要开发,也就意味着需要有更多的鸿蒙人才。鸿蒙开发工程师也将会迎来爆发式的增长,学习鸿蒙势在必行! 自↓↓↓拿