forked from floraachy/ImageKnife
1.pngj操作放入子线程完成。
Signed-off-by: zhoulisheng1 <zhoulisheng1@huawei.com>
This commit is contained in:
parent
476c80dd78
commit
82ad36f241
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue