> 文档中心 > 自学HarmonyOS应用开发(73)- 准备相机(2)

自学HarmonyOS应用开发(73)- 准备相机(2)

画面布局初始化

MainAbilittySlice的初始化代码如下:

@Overridepublic void onStart(Intent intent) {    super.onStart(intent);    super.setUIContent(ResourceTable.Layout_ability_main);    initComponents();}

代码中只是调用了如下的组件初始化代码:​​​​​​​

private void initComponents() {    Button iniCameraButton = (Button)findComponentById(ResourceTable.Id_ini_camera);    iniCameraButton.setClickedListener(Component->initSurface());    Button takePhotoButton = (Button) findComponentById(ResourceTable.Id_tack_picture_btn);    takePhotoButton.setClickedListener(this::takeSingleCapture);    takePhotoButton.setLongClickedListener(this::takeMultiCapture); }

目前这段代码只是为两个按钮指派功能。其中【初始化】按钮的功能如下:​​​​​​​

private void initSurface() {    getWindow().setTransparent(true);    DirectionalLayout.LayoutConfig params = new DirectionalLayout.LayoutConfig(     ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_PARENT);    surfaceProvider = new SurfaceProvider(this);    surfaceProvider.setLayoutConfig(params);    surfaceProvider.pinToZTop(false);    surfaceProvider.getSurfaceOps().get().addCallback(new SurfaceCallBack());    surfaceContainer = (ComponentContainer) findComponentById(ResourceTable.Id_surface_container);    surfaceContainer.addComponent(surfaceProvider);}

它的功能是初始化相机,有一点需要注意的是:构建params时使用的参数必须和前一篇文章中说明的布局文件中为id:surface_container指定的属性相同。

当初始化过程结束后,下面的回调函数会被执行:​​​​​​​

private class SurfaceCallBack implements SurfaceOps.Callback {    @Override    public void surfaceCreated(SurfaceOps callbackSurfaceOps) { openCamera();    }    @Override    public void surfaceChanged(SurfaceOps callbackSurfaceOps, int format, int width, int height) {    }    @Override    public void surfaceDestroyed(SurfaceOps callbackSurfaceOps) {    }}

的那个surface被成功创建之后,就可以打开相机了:​​​​​​​

private void openCamera() {    imageReceiver = ImageReceiver.create(SCREEN_WIDTH, SCREEN_HEIGHT, ImageFormat.JPEG, IMAGE_RCV_CAPACITY);    imageReceiver.setImageArrivalListener(this::saveImage);    CameraKit cameraKit = CameraKit.getInstance(getApplicationContext());    String[] cameraList = cameraKit.getCameraIds();    String cameraId = cameraList.length > 1 && isCameraFront ? cameraList[1] : cameraList[0];    CameraStateCallbackImpl cameraStateCallback = new CameraStateCallbackImpl();    cameraKit.createCamera(cameraId, cameraStateCallback, eventHandler);}

至此整个相机初始化过程结束。

还有一点需要补充的是,由于在前一篇文章中说明的获取权限的过程和本文中的布局初始化是异步执行的,导致布局初始化会在获取所有权限之前完成。如果在布局初始化之后紧接着初始化相机,会导致初始化过程失败。因此本文使用按钮启动相机的初始化过程。
以下是动作视频:

动作视频

参考资料

相机示例代码

https://gitee.com/openharmony/app_samples/tree/master/media/Camera

权限开发概述

https://developer.harmonyos.com/cn/docs/documentation/doc-guides/security-permissions-overview-0000000000029883

权限开发指导

https://developer.harmonyos.com/cn/docs/documentation/doc-guides/security-permissions-guidelines-0000000000029886

应用权限列表

https://developer.harmonyos.com/cn/docs/documentation/doc-guides/security-permissions-available-0000001051089272

作者著作介绍

《实战Python设计模式》是作者去年3月份出版的技术书籍,该书利用Python 的标准GUI 工具包tkinter,通过可执行的示例对23 个设计模式逐个进行说明。这样一方面可以使读者了解真实的软件开发工作中每个设计模式的运用场景和想要解决的问题;另一方面通过对这些问题的解决过程进行说明,让读者明白在编写代码时如何判断使用设计模式的利弊,并合理运用设计模式。

对设计模式感兴趣而且希望随学随用的读者通过本书可以快速跨越从理解到运用的门槛;希望学习Python GUI 编程的读者可以将本书中的示例作为设计和开发的参考;使用Python 语言进行图像分析、数据处理工作的读者可以直接以本书中的示例为基础,迅速构建自己的系统架构。


觉得本文有帮助?请分享给更多人。

关注微信公众号【面向对象思考】轻松学习每一天!

面向对象开发,面向对象思考!