華為鴻蒙HarmonyOS已經(jīng)發(fā)展到4.0,使用ArkTS作為開(kāi)發(fā)語(yǔ)言。這篇文章結(jié)合Dynamsoft Service開(kāi)發(fā)一個(gè)簡(jiǎn)單的鴻蒙應(yīng)用,用來(lái)獲取辦公室里連接PC的掃描儀(惠普,富士通,愛(ài)普生,等),把文檔掃描到手機(jī)里。
準(zhǔn)備工作
-
Dynamsoft Service
-
在連接著掃描儀的電腦上安裝Dynamsoft Service。安裝包可以滿足各種國(guó)產(chǎn)操作系統(tǒng),比如統(tǒng)信UOS,麒麟Kylin OS等。支持的架構(gòu)有:x86,x64,arm64,mips64el。支持的掃描儀協(xié)議包括TWAIN,WIA,SANE,ICA和eSCL(AirPrint)。下載地址:
- Windows: Dynamsoft-Service-Setup.msi
- macOS: Dynamsoft-Service-Setup.pkg
- Linux:
- Dynamsoft-Service-Setup.deb
- Dynamsoft-Service-Setup-arm64.deb
- Dynamsoft-Service-Setup-mips64el.deb
- Dynamsoft-Service-Setup.rpm
然后訪問(wèn)
http://127.0.0.1:18622/DWTAPI/Scanners
。正常安裝可以獲取到掃描儀列表。 -
在瀏覽器中打開(kāi)
http://127.0.0.1:18625/
,把host從127.0.0.1
改成PC的局域網(wǎng)IP地址。比如192.168.8.72
,修改成功可以通過(guò)局域網(wǎng)IP地址訪問(wèn)192.168.8.72:18622/DWTAPI/Scanners
獲取到掃描儀列表。 -
申請(qǐng)一個(gè)免費(fèi)試用序列號(hào),掃描文件的時(shí)候需要用。
-
-
DevEco Studio
下載地址:https://developer.harmonyos.com/cn/develop/deveco-studio/#download。安裝前先安裝Node.js,路徑中不要帶空格,否則安裝DevEco Studio, 下載HarmonyOS SDK可能會(huì)失敗。
鴻蒙程序開(kāi)發(fā)
在DevEco Studio中新建工程。
在entry/src/main/module.json5
中添加網(wǎng)絡(luò)權(quán)限:
{
"module": {
...
"abilities": [
...
],
"requestPermissions": [{"name": "ohos.permission.INTERNET"}]
}
}
打開(kāi)entry/src/main/etc/pages/Index.ets
,導(dǎo)入網(wǎng)絡(luò)和圖像模塊:
import http from '@ohos.net.http';
import image from '@ohos.multimedia.image';
聲明UI組件,包含兩個(gè)按鈕,一個(gè)下拉按鈕和一個(gè)圖片控件:
@Entry
@Component
struct Index {
@State deviceNames: SelectOption[] = [{value: ''}]
@State displayImage: PixelMap = undefined
licenseKey: string = "LICENSE-KEY"; // https://www.dynamsoft.com/customer/license/trialLicense?product=dwt
host: string = 'http://192.168.8.72:18622'
devices = []
index: number = 0
build() {
Column() {
Row() {
Button('Get Devices')
.onClick(() => {
}
);
}).width('30%')
Column() {
Select(this.deviceNames)
.selected(this.index)
.value(this.deviceNames[this.index].value.toString())
.font({size: 14, family: 'serif', style: FontStyle.Normal })
.onSelect((index:number)=>{
this.index = index;
})
}.width('40%').alignItems(HorizontalAlign.Center)
Button('Scan')
.onClick(() => {
}
);
}).width('30%')
}.backgroundColor(0xFFFFFF).padding({ left: 12 }).width('100%').margin({bottom: 5})
Divider()
Image(this.displayImage).height('100%').width('100%')
}.justifyContent(FlexAlign.Start).width('100%').height('100%').padding({left: 5, top: 5, right: 5, bottom: 5})
}
}
這里的licenseKey
和host
需要替換成自己的。
當(dāng)點(diǎn)擊Get Devices
按鈕的時(shí)候,我們通過(guò)HTTP GET來(lái)獲取掃描儀列表:
Button('Get Devices')
.onClick(() => {
let url = this.host + '/DWTAPI/Scanners'
let httpRequest = http.createHttp();
httpRequest.on('headersReceive', (header) => {
console.info('header: ' + JSON.stringify(header));
})
httpRequest.request(
url,
{
method: http.RequestMethod.GET,
header: {
'Content-Type': 'application/json'
}
}, (err, data) => {
if (!err) {
try {
const jsonArray = JSON.parse(data.result.toString())
this.devices = []
let tmp: SelectOption[] = []
for (const obj of jsonArray) {
tmp.push({value: obj.name})
this.devices.push(obj)
}
if (tmp.length > 0) {
this.index = 0
this.deviceNames = tmp
}
} catch (error) {
console.error("Error parsing JSON:", error);
}
console.info('code:' + JSON.stringify(data.responseCode));
} else {
console.info('error:' + JSON.stringify(err));
httpRequest.off('headersReceive');
httpRequest.destroy();
}
}
);
}).width('30%')
當(dāng)點(diǎn)擊Scan
按鈕的時(shí)候,我們通過(guò)HTTP POST來(lái)觸發(fā)掃描儀掃描文檔. 字段extraData用于傳輸內(nèi)容,等同于HTTP請(qǐng)求中的body
。傳輸?shù)膮?shù)可以自定義,具體可以參考在線文檔。
Button('Scan')
.onClick(() => {
if (this.devices.length == 0) {
return;
}
let parameters = {
license: this.licenseKey,
device: this.devices[this.index].device,
config: {
IfShowUI: false,
PixelType: 2,
//XferCount: 1,
//PageSize: 1,
Resolution: 200,
IfFeederEnabled: false,
IfDuplexEnabled: false,
}
};
let url = this.host + '/DWTAPI/ScanJobs';
let httpRequest = http.createHttp();
httpRequest.on('headersReceive', (header) => {
console.info('header: ' + JSON.stringify(header));
})
httpRequest.request(
url,
{
method: http.RequestMethod.POST,
header: {
'Content-Type': 'application/json'
},
extraData: JSON.stringify(parameters),
}, (err, data) => {
if (!err) {
if (data.responseCode == 201) {
let jobId = data.result;
let url = this.host + '/DWTAPI/ScanJobs/' + jobId + '/NextDocument';
let httpRequest = http.createHttp();
httpRequest.request(
url,
{
method: http.RequestMethod.GET,
expectDataType: http.HttpDataType.ARRAY_BUFFER
}, (err, data) => {
if (!err) {
if (data.responseCode == 200) {
// show image
}
} else {
console.info('error:' + JSON.stringify(err));
httpRequest.destroy();
}
}
);
}
} else {
console.info('error:' + JSON.stringify(err));
httpRequest.off('headersReceive');
httpRequest.destroy();
}
}
);
}).width('30%')
獲取到的圖像是一個(gè)ArrayBuffer
,我們通過(guò)image.createImageSource來(lái)創(chuàng)建一個(gè)PixelMap
對(duì)象,然后把它賦值給displayImage
,就可以在UI上顯示出來(lái)了。
let imageData = data.result as ArrayBuffer;
const imageSource = image.createImageSource(imageData);
imageSource.createPixelMap().then(pixelmap => {
this.displayImage = pixelmap;
});
在華為手機(jī)或者鴻蒙模擬器中運(yùn)行文檔掃描程序:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-723100.html
源代碼
https://gitee.com/yushulx/harmonyos-document-scanner文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-723100.html
到了這里,關(guān)于鴻蒙HarmonyOS應(yīng)用開(kāi)發(fā):掃描儀文件掃描的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!