OpenHarmony开发者论坛
标题:
OpenHarmony打印驱动开发指导
[打印本页]
作者:
dongzhengkuan
时间:
2023-11-6 15:24
标题:
OpenHarmony打印驱动开发指导
[md]### 一、DevEco Studio下载和安装
#### (1)下载DevEco Studio(3.1.0.501版本)
[下载链接](
https://developer.harmonyos.com/cn/develop/deveco-studio#download
)
#### (2)安装时可以自定义勾选安装选
*![](
https://wdcdn.qpic.cn/MTMxMDI2NjEwMjU1OTk5ODM_626319_7KqlHyDVi-3mJTzi_1693972116?sign=1699253579-1606329328-0-b8446dbb231837609eb1418ede433c50
)
### 二、创建demo工程
#### (1)安装成功后打开DevEco选项下载node.js和ohpm选项,执行Next然后根据提示安装SDK
![](
https://wdcdn.qpic.cn/MTMxMDI2NjEwMjU1OTk5ODM_807501_9DV7nmCPIxWDYf-4_1693972326?sign=1699253579-605772822-0-03884aea74f5d7145070af466700fcba
)
#### (2)SDK下载成功后创建项目,选择Empty Ability,选择项目基本信息(SDK使用最新的9,Model使用默认的Stage)
![](
https://wdcdn.qpic.cn/MTMxMDI2NjEwMjU1OTk5ODM_343192_ws5NgwpsNTHb9WX6_1693972747?sign=1699253579-1378355908-0-8bc5c94c7372749617f1edfa6ed16ae5
)
![](
https://wdcdn.qpic.cn/MTMxMDI2NjEwMjU1OTk5ODM_113264_fyl-Zl51F41trGsU_1693972769?sign=1699253579-1036000723-0-6501cfe33986e65bfee4951bb14f8799
)
#### (3)创建项目成功初始化项目资源时可能会出现以下报错,如果出现打开C:\\Users\\xxx\\.ohpm,编辑.ohpm文件,添加strict\_ssl=false,然后重启DecEco Studio![](
https://wdcdn.qpic.cn/MTMxMDI2NjEwMjU1OTk5ODM_228086_54JQ8jL3EFEhRiHg_1693980290?sign=1699253579-1494414433-0-f7e810a31c37cdb1fd9b8626c9734143
)
![](
https://wdcdn.qpic.cn/MTMxMDI2NjEwMjU1OTk5ODM_496517_zScgVPc7QvFFJ19H_1693980508?sign=1699253579-206669593-0-7d25f94a4a99457c61095421e7a4cbbe
)
#### (4)下载OpenHarmony SDK,默认下载的是HarmonyOS SDK,写demo时可以使用OpenHarmony SDK,方便后面配置自动签名
![](
https://wdcdn.qpic.cn/MTMxMDI2NjEwMjU1OTk5ODM_759095_OVbQp_7aMQElhkn9_1693983235?sign=1699253579-1500022687-0-47422a94ed13d557b989ea50666090d7
)
![](
https://wdcdn.qpic.cn/MTMxMDI2NjEwMjU1OTk5ODM_438675_Ot4bpiNDC075YmP1_1693983253?sign=1699253579-1089934982-0-9de2b17ec7902acc3829042627d9fca1
)
![](
https://wdcdn.qpic.cn/MTMxMDI2NjEwMjU1OTk5ODM_606203_13eBCBUZijBcmhGT_1693983270?sign=1699253579-139263954-0-54c7dd0cb9f243391fb1397f4cdfe9ca
)
### 三、编写OpenHarmony打印驱动配置信息
#### (1)创建DirverExtensionAbility:在项目根目录entry/src/main/ets目录下右键创建extensionAbility文件夹,在extensionAbility文件夹右键创建DirverExtensionAbility.ts文件
```
// @ts-nocheck
import DriverExtension from '@ohos.app.ability.DriverExtensionAbility'
export default class DriverExtensionAbility extends DriverExtension{
onInit(want) {
console.log('onInit, want: ' + want);
}
onRelease() {
console.log('onRelease');
}
}
```
![](
https://wdcdn.qpic.cn/MTMxMDI2NjEwMjU1OTk5ODM_810944_v4rZERjI2s-25XoQ_1693985289?sign=1699253579-2058710668-0-d02d48f8dcfca72da6c328f8ebd89509
)
#### (2)将编译好的驱动文件(filter/backend/ppd)文件放入到entry/src/main/resources/rawfile文件夹中,其中ppd中对应filter的名字需要拼接上当前应用的包名和entry名(确保驱动的唯一性)
![](
https://wdcdn.qpic.cn/MTMxMDI2NjEwMjU1OTk5ODM_655631_xso9gxsr_ncxVF5G_1693981480?sign=1699253579-830936737-0-1f9a75b90dc988cb23d9230904a3cd56
)
#### (3)修改entry/src/build-profile.json5 的 runtimeOS 为 OpenHarmony
![](
https://wdcdn.qpic.cn/MTMxMDI2NjEwMjU1OTk5ODM_566927_1d_5p2NMl-vETdJS_1693984349?sign=1699253579-664730850-0-79db982ef7f273ce72976f8e0b6bea74
)
#### (4)在entry/src/main/module.json5配置驱动信息
```
{
"module": {
"name": "entry",
"type": "entry",
"description": "$string:module_desc",
"mainElement": "EntryAbility",
"deviceTypes": [
"default",
"tablet"
],
"deliveryWithInstall": true,
"installationFree": false,
"pages": "$profile:main_pages",
"abilities": [
{
"name": "EntryAbility",
"srcEntry": "./ets/entryability/EntryAbility.ts",
"description": "$string:EntryAbility_desc",
"icon": "$media:icon",
"label": "$string:EntryAbility_label",
"startWindowIcon": "$media:icon",
"startWindowBackground": "$color:start_window_background",
"exported": true,
}
],
// **注意** 以下信息为新增
"extensionAbilities": [
{
"name": "DriverExtensionAbility",
"srcEntry": "./ets/extensionAbility/DriverExtensionAbility.ts",
"label": "$string:EntryAbility_label",
"icon": "$media:icon",
"description": "driver",
"type": "driver",
"exported": false,
"metadata": [
{
"name": "desc",
"value": "xxx printer driver"
},
{
"name": "vendor",
"value": "xx company"
},
{
"name": "cupsFilter",
"value": "/print_service/cups/serverbin/filter", // cups驱动配置路径,固定写法
"resource": "/resources/rawfile/rastertopwg" // filter名称
},
{
"name": "cupsPpd",
"value": "/print_service/cups/datadir/model", // cups驱动配置路径,固定写法
"resource": "/resources/rawfile/HUAWEI_PixLab_X1.ppd" // ppd名称
}
]
}
]
}
}
```
#### (5)此时直接执行build/build hap/build hap会报错,需要修改SDK配置信息
##### (5.1)ctrl+鼠标左键点击extensionAbilities下的type属性,在SDK的module.json里面的preview类型下面新增driver(弹出是否编辑选是确定)
![](
https://wdcdn.qpic.cn/MTMxMDI2NjEwMjU1OTk5ODM_677764_o9OpnBlriOAjonGM_1693982276?sign=1699253579-2113455624-0-8117ee202edc322ce06cdf2a606bcd86
)
![](
https://wdcdn.qpic.cn/MTMxMDI2NjEwMjU1OTk5ODM_887105_bekXCrd7gXV8nZWS_1693982389?sign=1699253579-1326216580-0-51360f446940fed7bf6cf6fc9cd48381
)
##### (5.2)ctrl+鼠标左键点击resource属性,将匹配规则pattern修改为:(注意w-前面的转移字符以及结尾处可能会有的\\n字符)
(^[\$]profile:[0-9a-zA-Z\_.]+|(?=.\*[{])(?=.\*[}])[0-9a-zA-Z\_.{}]+)|(^(/[\\w-]+)+(.[a-z]+)?)\$
![](
https://wdcdn.qpic.cn/MTMxMDI2NjEwMjU1OTk5ODM_388079__Dh2z0wyPhLmwD42_1693982414?sign=1699253579-873295179-0-fa602a6997dee044f6f1e1dc2069efbf
)
![](
https://wdcdn.qpic.cn/MTMxMDI2NjEwMjU1OTk5ODM_514484_rdwMjzpklmB4ppc1_1693982454?sign=1699253579-2023575725-0-054ff47c1d36a1559a49989bf4d20f68
)
![](
https://wdcdn.qpic.cn/MTMxMDI2NjEwMjU1OTk5ODM_272444_CuhgA3uTJ1kbYJ6U_1693982737?sign=1699253579-632523101-0-6c0c589e8f956b60c2a1c049ba45d3f9
)
##### (5.3)两处修改完毕,重启DevEco Studio,配置自动签名
![](
https://wdcdn.qpic.cn/MTMxMDI2NjEwMjU1OTk5ODM_101784_Uv9uRVrJM484LGeV_1693983444?sign=1699253579-1297071096-0-0d37926fc6d62a301e83f046a37b9314
)
![](
https://wdcdn.qpic.cn/MTMxMDI2NjEwMjU1OTk5ODM_85586_ujFwUjKP3B1TibYt_1693983459?sign=1699253579-1567884551-0-7296182a435f4b4a9d9ab101a5499734
)
![](
https://wdcdn.qpic.cn/MTMxMDI2NjEwMjU1OTk5ODM_752033_Pf81hBo1uJ4U3T7f_1693983501?sign=1699253579-503692679-0-2538977afb74a73032322690b672aa4b
)
### 四、编译驱动HAP包
#### (1)执行build-->build hap-->build hap
![](
https://wdcdn.qpic.cn/MTMxMDI2NjEwMjU1OTk5ODM_24372_n5cPJ3xSXwQaxMny_1693983574?sign=1699253579-359366357-0-58a15709a9a8dd8a2c2e708b62661b7a
)
![](
https://wdcdn.qpic.cn/MTMxMDI2NjEwMjU1OTk5ODM_316606_INzG8nurxvi4y9tP_1693983598?sign=1699253579-637329635-0-e4aa9a1ae7b4b9ea8f2d9266068abc1d
)
![](
https://wdcdn.qpic.cn/MTMxMDI2NjEwMjU1OTk5ODM_764421_ueknxSNWd9K4C826_1693983626?sign=1699253579-583346839-0-0d21ca24e45dc71189f78b0b7b8e1a5f
)
### 六、驱动HAP安装
1. #### rk3568环境准备
下载OpenHarmony 4.0 Release刷机包,选择dayu200的镜像包,[下载链接](
http://ci.openharmony.cn/workbench/cicd/dailybuild/dailylist
)
![图片.png](
"图片.png")
#### (2)在Terminal执行hdc install -r .\\entry-default-signed.hap
![](
https://wdcdn.qpic.cn/MTMxMDI2NjEwMjU1OTk5ODM_596509_UMO3OjRrSZT367lh_1693983815?sign=1699253579-947535346-0-2ac9a70049e616dcb525f30baf4dc9c7
)
结果显示安装成功
[/md]
欢迎光临 OpenHarmony开发者论坛 (https://forums.openharmony.cn/)
Powered by Discuz! X3.5