From 82ad36f241996d6b6d80b4fd277b04e08907b867 Mon Sep 17 00:00:00 2001 From: zhoulisheng1 Date: Mon, 14 Nov 2022 03:38:08 -0800 Subject: [PATCH] =?UTF-8?q?1.pngj=E6=93=8D=E4=BD=9C=E6=94=BE=E5=85=A5?= =?UTF-8?q?=E5=AD=90=E7=BA=BF=E7=A8=8B=E5=AE=8C=E6=88=90=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhoulisheng1 --- entry/src/main/ets/pages/pngjTestCasePage.ets | 107 ++++++++++-------- .../ets/components/imageknife/pngj/PngWork.js | 62 ++++------ 2 files changed, 82 insertions(+), 87 deletions(-) diff --git a/entry/src/main/ets/pages/pngjTestCasePage.ets b/entry/src/main/ets/pages/pngjTestCasePage.ets index ce406f6..661da94 100644 --- a/entry/src/main/ets/pages/pngjTestCasePage.ets +++ b/entry/src/main/ets/pages/pngjTestCasePage.ets @@ -30,13 +30,14 @@ struct PngjTestCasePage { @State hint4: string = 'writePngWithString内容展示' @State hint5: string = '测试readMetadata' @State hint6: string = '测试writeMetadata' + @State hint7: string = '首先点击获取PNG图片buffer' build() { Scroll() { Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { Text('pngj接口测试列表').fontSize(20) - Button('点击获取Png图片buffer').fontSize(20) + Button(this.hint7).fontSize(30) .onClick(() => { this.rootFolder = globalThis.ImageKnife.getImageKnifeContext().filesDir; @@ -45,6 +46,7 @@ struct PngjTestCasePage { globalThis.ImageKnife.getImageKnifeContext().resourceManager.getMedia($r('app.media.pngSample').id) .then(data => { this.pngSource = FileUtils.getInstance().uint8ArrayToBuffer(data); + this.hint7 = '获取buffer成功,可以测试' }) .catch(err => { console.log('点击获取Png图片buffer err=' + err) @@ -52,60 +54,59 @@ struct PngjTestCasePage { }) +// Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { +// Button('测试readPngImageInfo') +// .onClick(() => { +// let pngj = new Pngj(); +// pngj.readPngImageInfo(this.pngSource, (sender, value) => { +// this.hint1 = JSON.stringify(value); +// }) +// }).margin({ top: 5, left: 10 }) +// Button('测试readPngImage') +// .onClick(() => { +// let pngj = new Pngj(); +// pngj.readPngImage(this.pngSource, (sender, value) => { +// this.hint2 = 'img with=' + value.width + ' img height=' + value.height +// + ' img depth=' + value.depth + ' img ctype=' + value.ctype +// console.log('png data =' + value.data); +// }) +// }).margin({ top: 5, left: 10 }) +// Button('测试writePngWithString') +// .onClick(() => { +// let pngj = new Pngj(); +// pngj.writePngWithString('hello world', this.pngSource, (sender, value) => { +// +// FileUtils.getInstance().createFileProcess( +// this.rootFolder + '/pngj', +// this.rootFolder + '/pngj/newPng.png', +// value) +// let png1 = new Uint8Array(value) +// this.hint3 = 'png写入后长度' + png1.byteLength + '目录文件:' + this.rootFolder + '/pngj/newPng.png' + '保存后使用2进制查看数据是否新增' +// }) +// }).margin({ top: 5, left: 10 }) +// Button('测试writePng') +// .onClick(() => { +// let pngj = new Pngj(); +// pngj.writePng(this.pngSource, (sender, value) => { +// FileUtils.getInstance().createFileProcess( +// this.rootFolder + '/pngj', +// this.rootFolder + '/pngj/newPng2.png', +// value) +// let png2 = new Uint8Array(value) +// this.hint4 = 'png2未写入长度' + png2.byteLength + '目录文件:' + this.rootFolder + '/pngj/newPng2.png' + '保存后使用2进制查看数据是否新增' +// }) +// }).margin({ top: 5, left: 10 }) +// }.width('100%') +// .height(60).backgroundColor(Color.Pink) Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { - Button('测试readPngImageInfo') + Button('测试readPng') .onClick(() => { let pngj = new Pngj(); pngj.readPngImageInfo(this.pngSource, (sender, value) => { this.hint1 = JSON.stringify(value); }) }).margin({ top: 5, left: 10 }) - Button('测试readPngImage') - .onClick(() => { - let pngj = new Pngj(); - pngj.readPngImage(this.pngSource, (sender, value) => { - this.hint2 = 'img with=' + value.width + ' img height=' + value.height - + ' img depth=' + value.depth + ' img ctype=' + value.ctype - console.log('png data =' + value.data); - }) - }).margin({ top: 5, left: 10 }) - Button('测试writePngWithString') - .onClick(() => { - let pngj = new Pngj(); - pngj.writePngWithString('hello world', this.pngSource, (sender, value) => { - - FileUtils.getInstance().createFileProcess( - this.rootFolder + '/pngj', - this.rootFolder + '/pngj/newPng.png', - value) - let png1 = new Uint8Array(value) - this.hint3 = 'png写入后长度' + png1.byteLength + '目录文件:' + this.rootFolder + '/pngj/newPng.png' + '保存后使用2进制查看数据是否新增' - }) - }).margin({ top: 5, left: 10 }) - Button('测试writePng') - .onClick(() => { - let pngj = new Pngj(); - pngj.writePng(this.pngSource, (sender, value) => { - FileUtils.getInstance().createFileProcess( - this.rootFolder + '/pngj', - this.rootFolder + '/pngj/newPng2.png', - value) - let png2 = new Uint8Array(value) - this.hint4 = 'png2未写入长度' + png2.byteLength + '目录文件:' + this.rootFolder + '/pngj/newPng2.png' + '保存后使用2进制查看数据是否新增' - }) - }).margin({ top: 5, left: 10 }) - }.width('100%') - .height(60).backgroundColor(Color.Pink) - - Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { - Button('测试readPngImageInfo') - .onClick(() => { - let pngj = new Pngj(); - pngj.readPngImageInfo(this.pngSource, (sender, value) => { - this.hint1 = JSON.stringify(value); - }) - }).margin({ top: 5, left: 10 }) - Button('测试readPngImageAsync') + Button('readPngAsync') .onClick(() => { let pngj = new Pngj(); let worker = new ArkWorker.Worker('entry/ets/pages/workers/worker1.js', { @@ -118,7 +119,12 @@ struct PngjTestCasePage { + ' img depth=' + value.depth + ' img ctype=' + value.ctype }) }).margin({ top: 5, left: 10 }) - Button('测试writePngWithStringAsync') + + }.width('100%') + .height(60).backgroundColor(Color.Pink) + Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { + + Button('测试writePngWithString') .onClick(() => { let pngj = new Pngj(); let worker = new ArkWorker.Worker('entry/ets/pages/workers/worker1.js', { @@ -135,7 +141,7 @@ struct PngjTestCasePage { this.hint3 = 'png写入后长度' + png1.byteLength + '目录文件:' + this.rootFolder + '/pngj/newPng.png' + '保存后使用2进制查看数据是否新增' }) }).margin({ top: 5, left: 10 }) - Button('测试writePngAsync') + Button('writePng') .onClick(() => { let pngj = new Pngj(); let worker = new ArkWorker.Worker('entry/ets/pages/workers/worker1.js', { @@ -155,6 +161,7 @@ struct PngjTestCasePage { }.width('100%') .height(60).backgroundColor(Color.Pink) + Text(this.hint1) .width('100%') .height(120) diff --git a/imageknife/src/main/ets/components/imageknife/pngj/PngWork.js b/imageknife/src/main/ets/components/imageknife/pngj/PngWork.js index a51628e..f646717 100644 --- a/imageknife/src/main/ets/components/imageknife/pngj/PngWork.js +++ b/imageknife/src/main/ets/components/imageknife/pngj/PngWork.js @@ -16,43 +16,31 @@ import arkWorker from '@ohos.worker'; import {UPNG} from '../../imageknife/pngj/UPNG' export function handler (e) { - var data = e.data; - switch (data.type) { - case 'readPngImageAsync': - var png = UPNG.decode(data.data); - let array = png.data; - let arrayData = array.buffer.slice(array.byteOffset, array.byteLength + array.byteOffset) - png.data = arrayData; - let dataObj = { type: 'readPngImageAsync', data: png, receiver: data.data} - arkWorker.parentPort.postMessage(dataObj, [png.data, data.data]); - break; - case 'writePngWithStringAsync': - let addInfo = data.info; - let pngDecode = UPNG.decode(data.data); - let newPng = UPNG.encodeWithString(addInfo, UPNG.toRGBA8(pngDecode), pngDecode.width, pngDecode.height, 0) - let dataObj2 = { type: 'writePngWithStringAsync', data: newPng, receiver: data.data} - arkWorker.parentPort.postMessage(dataObj2, [newPng, data.data]); - break; - case 'writePngAsync': - let pngDecode3 = UPNG.decode(data.data); - let newPng3 = UPNG.encode(UPNG.toRGBA8(pngDecode3), pngDecode3.width, pngDecode3.height, 0) - let dataObj3 = { type: 'writePngAsync', data: newPng3, receiver: data.data} - arkWorker.parentPort.postMessage(dataObj3, [newPng3, data.data]); - break; - case 'normal': - arkWorker.parentPort.postMessage(data); - break; - case 'error': - throw new Error('123'); - break; - case 'buffer': - let uint8Array = new Uint8Array(data.data); - arkWorker.parentPort.postMessage(data, [data.data]); - - break; - default: - - break + var data = e.data; + switch (data.type) { + case 'readPngImageAsync': + var png = UPNG.decode(data.data); + let array = png.data; + let arrayData = array.buffer.slice(array.byteOffset, array.byteLength + array.byteOffset) + png.data = arrayData; + let dataObj = { type: 'readPngImageAsync', data: png, receiver: data.data} + arkWorker.parentPort.postMessage(dataObj, [png.data, data.data]); + break; + case 'writePngWithStringAsync': + let addInfo = data.info; + let pngDecode = UPNG.decode(data.data); + let newPng = UPNG.encodeWithString(addInfo, UPNG.toRGBA8(pngDecode), pngDecode.width, pngDecode.height, 0) + let dataObj2 = { type: 'writePngWithStringAsync', data: newPng, receiver: data.data} + arkWorker.parentPort.postMessage(dataObj2, [newPng, data.data]); + break; + case 'writePngAsync': + let pngDecode3 = UPNG.decode(data.data); + let newPng3 = UPNG.encode(UPNG.toRGBA8(pngDecode3), pngDecode3.width, pngDecode3.height, 0) + let dataObj3 = { type: 'writePngAsync', data: newPng3, receiver: data.data} + arkWorker.parentPort.postMessage(dataObj3, [newPng3, data.data]); + break; + default: + break } }