自學(xué)HarmonyOS應(yīng)用開發(fā)(73)- 準(zhǔn)備相機(jī)(2)

畫面布局初始化
MainAbilittySlice的初始化代碼如下:
public void onStart(Intent intent) {super.onStart(intent);super.setUIContent(ResourceTable.Layout_ability_main);initComponents();}
代碼中只是調(diào)用了如下的組件初始化代碼:
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);}
目前這段代碼只是為兩個(gè)按鈕指派功能。其中【初始化】按鈕的功能如下:
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);}
它的功能是初始化相機(jī),有一點(diǎn)需要注意的是:構(gòu)建params時(shí)使用的參數(shù)必須和前一篇文章中說明的布局文件中為id:surface_container指定的屬性相同。
當(dāng)初始化過程結(jié)束后,下面的回調(diào)函數(shù)會(huì)被執(zhí)行:
private class SurfaceCallBack implements SurfaceOps.Callback {public void surfaceCreated(SurfaceOps callbackSurfaceOps) {openCamera();}public void surfaceChanged(SurfaceOps callbackSurfaceOps, int format, int width, int height) {}public void surfaceDestroyed(SurfaceOps callbackSurfaceOps) {}}
的那個(gè)surface被成功創(chuàng)建之后,就可以打開相機(jī)了:
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);}
至此整個(gè)相機(jī)初始化過程結(jié)束。
還有一點(diǎn)需要補(bǔ)充的是,由于在前一篇文章中說明的獲取權(quán)限的過程和本文中的布局初始化是異步執(zhí)行的,導(dǎo)致布局初始化會(huì)在獲取所有權(quán)限之前完成。如果在布局初始化之后緊接著初始化相機(jī),會(huì)導(dǎo)致初始化過程失敗。因此本文使用按鈕啟動(dòng)相機(jī)的初始化過程。
以下是動(dòng)作視頻:
參考資料
相機(jī)示例代碼
https://gitee.com/openharmony/app_samples/tree/master/media/Camera
權(quán)限開發(fā)概述
https://developer.harmonyos.com/cn/docs/documentation/doc-guides/security-permissions-overview-0000000000029883
權(quán)限開發(fā)指導(dǎo)
https://developer.harmonyos.com/cn/docs/documentation/doc-guides/security-permissions-guidelines-0000000000029886
應(yīng)用權(quán)限列表
https://developer.harmonyos.com/cn/docs/documentation/doc-guides/security-permissions-available-0000001051089272
作者著作介紹
《實(shí)戰(zhàn)Python設(shè)計(jì)模式》是作者去年3月份出版的技術(shù)書籍,該書利用Python 的標(biāo)準(zhǔn)GUI 工具包tkinter,通過可執(zhí)行的示例對(duì)23 個(gè)設(shè)計(jì)模式逐個(gè)進(jìn)行說明。這樣一方面可以使讀者了解真實(shí)的軟件開發(fā)工作中每個(gè)設(shè)計(jì)模式的運(yùn)用場(chǎng)景和想要解決的問題;另一方面通過對(duì)這些問題的解決過程進(jìn)行說明,讓讀者明白在編寫代碼時(shí)如何判斷使用設(shè)計(jì)模式的利弊,并合理運(yùn)用設(shè)計(jì)模式。

對(duì)設(shè)計(jì)模式感興趣而且希望隨學(xué)隨用的讀者通過本書可以快速跨越從理解到運(yùn)用的門檻;希望學(xué)習(xí)Python GUI 編程的讀者可以將本書中的示例作為設(shè)計(jì)和開發(fā)的參考;使用Python 語(yǔ)言進(jìn)行圖像分析、數(shù)據(jù)處理工作的讀者可以直接以本書中的示例為基礎(chǔ),迅速構(gòu)建自己的系統(tǒng)架構(gòu)。
覺得本文有幫助?請(qǐng)分享給更多人。
關(guān)注微信公眾號(hào)【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!
面向?qū)ο箝_發(fā),面向?qū)ο笏伎迹?/span>
