OpenHarmony开发者论坛

标题: [Experience Sharing] OpenHarmony Print Driver Development Guide [打印本页]

作者: itDyh12    时间: 2024-4-15 19:36
标题: [Experience Sharing] OpenHarmony Print Driver Development Guide
[md]### 1. DevEco Studio download and installation

#### (1) Download DevEco Studio (version 3.1.0.501)

[Download link:](https://developer.harmonyos.com/cn/develop/deveco-studio#download)

#### (2) When installing, you can customize the check installation selection

\*![](https://wdcdn.qpic.cn/MTMxMDI2Nj ... 7609eb1418ede433c50)

### 2. Create a demo project

#### (1) After successful installation, open the DevEco option to download the node.js and ohpm options, execute Next, and then install the SDK according to the prompts

![](https://wdcdn.qpic.cn/MTMxMDI2Nj ... 7145070af466700fcba)

#### (2) Create a project after the SDK is successfully downloaded, select Empty Ability, and select the basic information of the project (the SDK uses the latest 9, and the Model uses the default Stage)

![](https://wdcdn.qpic.cn/MTMxMDI2Nj ... 49617f1edfa6ed16ae5)

![](https://wdcdn.qpic.cn/MTMxMDI2Nj ... 65bfee4951bb14f8799)

#### (3)The following error may occur when successfully initializing project resources. If it appears open C:\Users\xxx\.ohpm,Editor.ohpm file,add strict_ssl=false,Then restart DecEco Studio![](https://wdcdn.qpic.cn/MTMxMDI2Nj ... db1fd9b8626c9734143)

![](https://wdcdn.qpic.cn/MTMxMDI2Nj ... 57c61095421e7a4cbbe)

#### (4)download OpenHarmony SDK,The default download is HarmonyOS SDK,write demo can use OpenHarmony SDK,This facilitates automatic signature configuration

![](https://wdcdn.qpic.cn/MTMxMDI2Nj ... 557b989ea50666090d7)

![](https://wdcdn.qpic.cn/MTMxMDI2Nj ... acc3829042627d9fca1)

![](https://wdcdn.qpic.cn/MTMxMDI2Nj ... 3391fb1397f4cdfe9ca)

### 三、write OpenHarmony Print the driver configuration information

#### (1)create DirverExtensionAbility:In the project root directory entry/src/main/ets Create by right-clicking the directory extensionAbility file,in extensionAbility Right click Create folder DirverExtensionAbility.ts file

```
// @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/MTMxMDI2Nj ... 72da6c328f8ebd89509)

#### (2)Will compile the driver file(filter/backend/ppd)File into entry/src/main/resources/rawfile , ppd  filter The name needs to be concatenated with the package name of the current application and entry name(Ensure the uniqueness of the drive)

![](https://wdcdn.qpic.cn/MTMxMDI2Nj ... 8cb23d9230904a3cd56)

#### (3)edit entry/src/build-profile.json5  runtimeOS as OpenHarmony

![](https://wdcdn.qpic.cn/MTMxMDI2Nj ... 3ce72976f8e0b6bea74)

#### (4)in entry/src/main/module.json5 Configuring driver Information

```
{
  "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)Direct execution at this time build/build hap/build hap will error,The SDK configuration information needs to be modified

##### (5.1)ctrl+ Left mouse click extensionAbilities  type Stats,Add driver under preview type in SDK module.json (pop up whether to edit is OK)

![](https://wdcdn.qpic.cn/MTMxMDI2Nj ... 22ce06cdf2a606bcd86)

![](https://wdcdn.qpic.cn/MTMxMDI2Nj ... ed7bf6cf6fc9cd48381)

##### (5.2)ctrl+ left mouse click on the resource property to change the pattern of the matching rule to: (Note the transition character before w- and the \\n character that may be at the end)

(^[\$]profile:[0-9a-zA-Z\_.]+|(?=.\*[{])(?=.\*[}])[0-9a-zA-Z\_.{}]+)|(^(/[\\w-]+)+(.[a-z]+)?)\$

![](https://wdcdn.qpic.cn/MTMxMDI2Nj ... 044f6f1e1dc2069efbf)

![](https://wdcdn.qpic.cn/MTMxMDI2Nj ... 1559a49989bf4d20f68)

![](https://wdcdn.qpic.cn/MTMxMDI2Nj ... b60c2a1c049ba45d3f9)

##### (5.3)After the two changes are complete, restart DevEco Studio and configure automatic signature

![](https://wdcdn.qpic.cn/MTMxMDI2Nj ... a301e83f046a37b9314)

![](https://wdcdn.qpic.cn/MTMxMDI2Nj ... b4a9d9ab101a5499734)

![](https://wdcdn.qpic.cn/MTMxMDI2Nj ... 73032322690b672aa4b)

4. Compile the driver HAP package

#### (1)Run build-->build hap-->build hap

![](https://wdcdn.qpic.cn/MTMxMDI2Nj ... d8a2c2e708b62661b7a)

![](https://wdcdn.qpic.cn/MTMxMDI2Nj ... 9ea8f2d9266068abc1d)

![](https://wdcdn.qpic.cn/MTMxMDI2Nj ... 71189f78b0b7b8e1a5f)

### 6. Drive HAP installation

1. #### RK3568 Environment Preparation

Download the OpenHarmony 4.0 Release flash package, select the dayu200 image package, and [download the link](http://ci.openharmony.cn/workbench/cicd/dailybuild/dailylist)

![Picture .png](https://forums.openharmony.cn/%5 ... xeewd.png%5B/img%5D "图片.png")

#### (2)In Terminal, run hdc install -r.\\ entry-default-sign.hap

![](https://wdcdn.qpic.cn/MTMxMDI2Nj ... 6dcb525f30baf4dc9c7)

The results show that the installation was successful
[/md]




欢迎光临 OpenHarmony开发者论坛 (https://forums.openharmony.cn/) Powered by Discuz! X3.5