適配倉庫地址[1]
作者倉庫:https://github.com/svprdga/torch_light
在數(shù)字化浪潮的推動下,跨平臺開發(fā)框架如 Flutter 憑借其高效、便捷的特性,成為了開發(fā)者們的寵兒。而鴻蒙系統(tǒng)的崛起,更是為跨平臺開發(fā)注入了新的活力。為了助力開發(fā)者在鴻蒙生態(tài)中快速實現(xiàn) torch_light 來處理設備手電筒的插件功能,本文將深入淺出地為大家解析如何適配 torch_light 三方庫至鴻蒙平臺。
一、適配鴻蒙版 torch_light 三方庫
(一)版本選擇與倉庫簡介
我們先去 pub 上查看最新版本,我們選擇以 0.0.10 版本為基礎進行適配。torch_light 是一個簡單的 Flutter 插件來處理設備手電筒的插件,其 GitHub 倉庫為https://github.com/svprdga/torch_light#,我們的目標是將這個插件適配到鴻蒙平臺。
(二)引入背景與使用場景
在 OpenHarmony 北向生態(tài)的發(fā)展過程中,許多已經(jīng)適配了 Flutter 的廠商在接入 OpenHarmony 時,都希望能夠繼續(xù)使用 torch_light 來實現(xiàn)手電筒功能。因此,我們提供了這個適配方案,采用插件化的適配器模式,幫助生態(tài)伙伴快速實現(xiàn)產(chǎn)品化。
本方案適用于已經(jīng)支持 Flutter 框架的設備在移植到 OpenHarmony 系統(tǒng)過程中,作為一個備選方案。
(三)使用文檔與插件庫使用
適配 OpenHarmony 平臺的詳細使用指導可以參考:Flutter 使用指導文檔[2]
在項目中使用該插件庫時,只需在 pubspec.yaml
文件的 dependencies
中新增如下配置:
dependencies:
torch_light:
git:
url: "git@:nutpi/flutter_torch_light.git"
path: ""
然后在項目根目錄運行 flutter pub get
,即可完成依賴添加
接下來是具體的適配過程。
二、適配過程詳解
(一)準備工作
確保已經(jīng)配置好了 Flutter 開發(fā)環(huán)境,具體可參考 Flutter 配置指南[3]。同時,從 官方插件庫[4] 下載待適配的三方插件。本指導書, 以適配 torch_light[5] 為例
image-20250417200546042(二)插件目錄結(jié)構
下載并解壓插件后,我們會看到以下目錄結(jié)構:
- lib :對接 Dart 端代碼的入口,由此文件接收到參數(shù)后,通過 channel 將數(shù)據(jù)發(fā)送到原生端。
- android :安卓端代碼實現(xiàn)目錄。
- example :一個依賴于該插件的 Flutter 應用程序,用于說明如何使用它。
- CHANGELOG.md :記錄每個版本中的更改。
(三)創(chuàng)建插件的鴻蒙模塊
在插件目錄下,打開 Terminal,執(zhí)行以下命令來創(chuàng)建一個鴻蒙平臺的 Flutter 模塊:
flutter create . --org com.svprdga.torchlight --template=plugin --platforms=ohos
步驟:
用 vscode/trae 打開剛剛下載好的插件。
執(zhí)行命令flutter create . --template=plugin --platforms=ohos
創(chuàng)建一個 ohos 平臺的 flutter 模塊。
第一個問題,修改 sdk 的版本,適配舊版本。
我們做好修改就好。
(四)在根目錄下添加鴻蒙平臺配置
在項目根目錄的 pubspec.yaml
文件中,添加鴻蒙平臺的相關配置:
name: torch_light
description:AFlutterplugintocheckifthedevicehasatorch/flashlight,andtoturnitonandoff.
version:1.1.0
homepage:https:///
repository:https://github.com/svprdga/torch_light
environment:
sdk:">=2.17.5 <4.0.0"
flutter:">=2.3.0"
dependencies:
flutter:
sdk:flutter
dev_dependencies:
flutter_test:
sdk:flutter
lint:2.3.0
flutter:
plugin:
platforms:
android:
package:com.svprdga.torchlight
pluginClass:TorchLightPlugin
ios:
pluginClass:TorchLightPlugin
ohos:
package:com.svprdga.torchlight
pluginClass:TorchLightPlugin
(五)編寫鴻蒙插件的原生 ArkTS 模塊
1. 創(chuàng)建鴻蒙插件模塊
使用 DevEco Studio 打開鴻蒙項目。
2. 修改相關配置文件
在 ohos
目錄內(nèi)的 oh-package.json5
文件中添加 libs/flutter.har
依賴,并創(chuàng)建 .gitignore
文件,添加以下內(nèi)容以忽略 libs
目錄:
/node_modules
/oh_modules
/local.properties
/.preview
/.idea
/build
/libs
*.har
/.cxx
/.test
/BuildProfile.ets
/oh-package-lock.json5
oh-package.json5
文件內(nèi)容如下:
{
"name": "torch_light",
"version": "1.0.0",
"description": "Please describe the basic information.",
"main": "index.ets",
"author": "",
"license": "Apache-2.0",
"dependencies": {
"@ohos/flutter_ohos": "file:./har/flutter.har"
}
}
在 ohos
目錄下創(chuàng)建 index.ets
文件,導出配置:
import TorchLightPlugin from './src/main/ets/components/plugin/TorchLightPlugin';
export default TorchLightPlugin;
3. 編寫 ETS 代碼
文件結(jié)構和代碼邏輯可以參考安卓或 iOS 的實現(xiàn),鴻蒙的 API 文檔可以參考 :https:///openharmony-sig/flutter_packages/tree/master/packages/path_provider/path_provider_android
ohos 的 api 可以參考:https:///openharmony/docs
以下是 TorchLightPlugin.ets
文件的代碼示例:
private channel: MethodChannel | null = null;
private nativeEventIsTorchAvailable: string = "torch_available";
private errorIsTorchAvailable: string = "torch_available_error";
private nativeEventEnableTorch: string = "enable_torch";
private errorEnableTorchExistentUser: string = "enable_torch_error_existent_user";
private errorEnableTorch: string = "enable_torch_error";
private errorEnableTorchNotAvailable: string = "enable_torch_not_available";
private nativeEventDisableTorch: string = "disable_torch";
private errorDisableTorchExistentUser: string = "disable_torch_error_existent_user";
private errorDisableTorch: string = "disable_torch_error";
private errorDisableTorchNotAvailable: string = "disable_torch_not_available";
這里我主要參考的是
三、手電筒使用
手電筒模式的使用是通過操作手機啟用手電筒功能,使設備的手電筒功能持續(xù)保持常亮狀態(tài)。
在使用相機應用并操作手電筒功能時,存在以下幾種情況說明:
- 當使用后置攝像頭并設置閃光燈模式FlashMode[6]關閉時,手電筒功能無法啟用。
- 當使用前置攝像頭時,手電筒可以正常啟用并保持常亮狀態(tài)。
- 從前置攝像頭切換至后置攝像頭時,如果手電筒原本處于開啟狀態(tài),它將會被自動關閉。
詳情請參考Camera API 參考[7]。
開發(fā)步驟
導入 camera 接口,接口中提供了相機相關的屬性和方法,導入方法如下。
import { camera } from '@kit.CameraKit';
import { BusinessError } from '@kit.BasicServicesKit';
通過CameraManager[8]類中的isTorchSupported[9]方法,檢測當前設備是否支持手電筒功能。
function isTorchSupported(cameraManager: camera.CameraManager) : boolean {
let torchSupport: boolean = false;
try {
torchSupport = cameraManager.isTorchSupported();
} catch (error) {
let err = error as BusinessError;
console.error('Failed to torch. errorCode = ' + err.code);
}
console.info('Returned with the torch support status:' + torchSupport);
return torchSupport;
}
通過CameraManager[10]類中的isTorchModeSupported[11]方法,檢測是否支持指定的手電筒模式TorchMode[12]。
function isTorchModeSupported(cameraManager: camera.CameraManager, torchMode: camera.TorchMode) : boolean {
let isTorchModeSupport: boolean = false;
try {
isTorchModeSupport = cameraManager.isTorchModeSupported(torchMode);
} catch (error) {
let err = error as BusinessError;
console.error('Failed to set the torch mode. errorCode = ' + err.code);
}
return isTorchModeSupport;
}
通過CameraManager[13]類中的setTorchMode[14]方法,設置當前設備的手電筒模式。以及通過CameraManager[15]類中的getTorchMode[16]方法,獲取當前設備的手電筒模式。
在使用getTorchMode[17]方法前,需要先注冊監(jiān)聽手電筒的狀態(tài)變化,請參考狀態(tài)監(jiān)聽[18]。
function setTorchModeSupported(cameraManager: camera.CameraManager, torchMode: camera.TorchMode) : void {
cameraManager.setTorchMode(torchMode);
let isTorchMode = cameraManager.getTorchMode();
console.info(`Returned with the torch mode supportd mode: ${isTorchMode}`);
}
狀態(tài)監(jiān)聽
在相機應用開發(fā)過程中,可以隨時監(jiān)聽手電筒狀態(tài),包括手電筒打開、手電筒關閉、手電筒不可用、手電筒恢復可用。手電筒狀態(tài)發(fā)生變化,可通過回調(diào)函數(shù)獲取手電筒模式的變化。
通過注冊 torchStatusChange 事件,通過回調(diào)返回監(jiān)聽結(jié)果,callback 返回 TorchStatusInfo 參數(shù),參數(shù)的具體內(nèi)容可參考相機管理器回調(diào)接口實例TorchStatusInfo[19]。
function onTorchStatusChange(cameraManager: camera.CameraManager): void {
cameraManager.on('torchStatusChange', (err: BusinessError, torchStatusInfo: camera.TorchStatusInfo) => {
if (err !== undefined && err.code !== 0) {
console.error(`Callback Error, errorCode: ${err.code}`);
return;
}
console.info(`onTorchStatusChange, isTorchAvailable: ${torchStatusInfo.isTorchAvailable}, isTorchActive: ${torchStatusInfo.
isTorchActive}, level: ${torchStatusInfo.torchLevel}`);
});
}
完整的代碼
微信:17752170152獲取/
四、編寫 Example
1. 創(chuàng)建 Example 應用
在插件根目錄下創(chuàng)建一個名為 example
的文件夾,用于存放示例應用。在 example
文件夾中,創(chuàng)建一個鴻蒙平臺的 Flutter 應用,用于驗證插件功能。
2. 簽名與運行
使用 Deveco Studio
打開 example > ohos
目錄,單擊 File > Project Structure > Project > Signing Configs
,勾選 Automatically generate signature
,等待自動簽名完成。然后運行以下命令:
flutter pub get
flutter build hap --debug
如果應用正常啟動,說明插件適配成功。如果沒有,歡迎大家聯(lián)系堅果派一起支持。
五、總結(jié)
通過以上步驟,我們成功地將 torch_light 三方庫適配到了鴻蒙平臺。這個過程涉及到了解插件的基本信息、配置開發(fā)環(huán)境、創(chuàng)建鴻蒙模塊、編寫原生代碼以及測試驗證等多個環(huán)節(jié)。希望這篇博客能夠幫助到需要進行 torch_light 鴻蒙適配的開發(fā)者們,讓大家在鴻蒙生態(tài)的開發(fā)中更加得心應手。
六、參考
- [如何使用 Flutter 與 OpenHarmony 通信 FlutterChannel](https:///openharmony-sig/flutter_samples/blob/master/ohos/docs/04_development/如何使用Flutter與OpenHarmony通信 FlutterChannel.md "如何使用 Flutter 與 OpenHarmony 通信 FlutterChannel")
- [開發(fā) module](https:///openharmony-sig/flutter_samples/blob/master/ohos/docs/04_development/如何使用混合開發(fā) module.md "開發(fā) module")
- [開發(fā) FFI plugin](https:///openharmony-sig/flutter_samples/blob/master/ohos/docs/04_development/開發(fā)FFI plugin.md "開發(fā) FFI plugin")
【感謝大家的認可!】若內(nèi)容對您有幫助,歡迎點贊??關注??支持!對鴻蒙原生開發(fā)以及跨平臺開發(fā)鴻蒙感興趣的小伙伴,歡迎添加我的微信(微信號:17752170152),我們一起探討技術、分享經(jīng)驗,攜手成長!
七、堅果派
堅果派由堅果等人創(chuàng)建,團隊擁有若干華為 HDE,以及若干其他領域的三十余位萬粉博主運營。專注于分享的技術包括 HarmonyOS/OpenHarmony,ArkUI-X,元服務,服務卡片,華為自研語言,BlueOS 操作系統(tǒng)、團隊成員聚集在北京、上海、廣州、深圳、南京、杭州、蘇州、寧夏等地。 聚焦“鴻蒙原生應用”、“智能物聯(lián)”和“AI 賦能”、“人工智能”四大業(yè)務領域,依托華為開發(fā)者專家等強大的技術團隊,以及涵蓋需求、開發(fā)、測試、運維于一體的綜合服務體系,賦能文旅、媒體、社交、家居、消費電子等行業(yè)客戶,滿足社區(qū)客戶數(shù)字化升級轉(zhuǎn)型的需求,幫助客戶實現(xiàn)價值提升。 目前上架鴻蒙原生應用 18 款,三方庫 72 個。
地址:https:///nutpi
https:///nutpi
八、FAQ
[1] 適配倉庫地址: https:///nutpi/flutter_torch_light
[2] Flutter使用指導文檔: https:///openharmony-sig/flutter_samples/blob/master/ohos/docs/07_plugin/ohos%E5%B9%B3%E5%8F%B0%E9%80%82%E9%85%8Dflutter%E4%B8%89%E6%96%B9%E5%BA%93%E6%8C%87%E5%AF%BC.md
[3] Flutter 配置指南: https:///openharmony-sig/flutter_flutter/blob/master/README.md
[4] 官方插件庫: https://v/
[5] torch_light: https://pub-web./packages/torch_light/versions/1.1.0
[6] FlashMode: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V13/js-apis-camera-V13#flashmode
[7] Camera API參考: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V13/js-apis-camera-V13
[8] CameraManager: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V13/js-apis-camera-V13#cameramanager
[9] isTorchSupported: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V13/js-apis-camera-V13#istorchsupported11
[10] CameraManager: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V13/js-apis-camera-V13#cameramanager
[11] isTorchModeSupported: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V13/js-apis-camera-V13#istorchmodesupported11
[12] TorchMode: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V13/js-apis-camera-V13#torchmode11
[13] CameraManager: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V13/js-apis-camera-V13#cameramanager
[14] setTorchMode: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V13/js-apis-camera-V13#settorchmode11
[15] CameraManager: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V13/js-apis-camera-V13#cameramanager
[16] getTorchMode: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V13/js-apis-camera-V13#gettorchmode11
[17] getTorchMode: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V13/js-apis-camera-V13#gettorchmode11
[18] 狀態(tài)監(jiān)聽: https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V13/camera-torch-use-V13#狀態(tài)監(jiān)聽
[19] TorchStatusInfo: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V13/js-apis-camera-V13#torchstatusinfo11
[20] 開發(fā)package: https:///openharmony-sig/flutter_samples/blob/master/ohos/docs/04_development/開發(fā)package.md
[21] 開發(fā)plugin: https:///openharmony-sig/flutter_samples/blob/master/ohos/docs/04_development/開發(fā)plugin.md
[22] developing-packages: https://docs./packages-and-plugins/developing-packages
[23] 適配倉庫地址: https:///nutpi/flutter_exit_app
[24] 手電筒: https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/camera-torch-use