1.pngj add Async Methods

2.worker1.js add decode png code with multiThread

Signed-off-by: zhoulisheng <635547767@qq.com>
This commit is contained in:
zhoulisheng 2022-03-25 11:43:56 +08:00
parent d19f2fbc88
commit 8811341b75
4 changed files with 196 additions and 13 deletions

View File

@ -0,0 +1,18 @@
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export interface PngCallback<R,T>{
(sender:R, receover:T)
}

View File

@ -1,16 +1,18 @@
import {UPNG} from '../pngj/UPNG.js';
import {UPNG} from '../pngj/UPNG';
import {PngCallback} from '../pngj/PngCallback';
import image from '@ohos.multimedia.image';
import resourceManager from '@ohos.resourceManager';
import featureability from '@ohos.ability.featureAbility'
import ArkWorker from '@ohos.worker'
export class Pngj {
readPngImageInfo(arraybuffer: ArrayBuffer, callback) {
readPngImageInfo(arraybuffer: ArrayBuffer, callback:PngCallback<ArrayBuffer, any>) {
let imageSource = image.createImageSource(arraybuffer);
imageSource.getImageInfo((err, value) => {
if (err) {
return;
}
callback(value);
callback(arraybuffer, value);
});
}
@ -27,21 +29,116 @@ export class Pngj {
* tabs: additional chunks of the PNG file
* data: pixel data of the image
*/
readPngImage(pngBuffer: ArrayBuffer, callback) {
readPngImage(pngBuffer: ArrayBuffer, callback:PngCallback<ArrayBuffer, any>) {
var png = UPNG.decode(pngBuffer);
callback(png)
callback(pngBuffer, png)
}
writePngWithString(addInfo:string, pngBuffer: ArrayBuffer,callback) {
writePngWithString(addInfo:string, pngBuffer: ArrayBuffer,callback:PngCallback<ArrayBuffer, any>) {
var pngDecode = UPNG.decode(pngBuffer);
var newPng = UPNG.encodeWithString(addInfo, UPNG.toRGBA8(pngDecode), pngDecode.width, pngDecode.height, 0)
callback(newPng);
callback(pngBuffer, newPng);
}
writePng(pngBuffer: ArrayBuffer,callback) {
writePng(pngBuffer: ArrayBuffer,callback:PngCallback<ArrayBuffer, any>) {
var pngDecode = UPNG.decode(pngBuffer);
var newPng = UPNG.encode(UPNG.toRGBA8(pngDecode), pngDecode.width, pngDecode.height, 0)
callback(newPng);
callback(pngBuffer, newPng);
}
readPngImageAsync(pngBuffer: ArrayBuffer, callback:PngCallback<ArrayBuffer, any>){
let worker = new ArkWorker.Worker('workers/worker1.js', { type: 'classic', name: 'readPngImageAsync'})
worker.onerror = function(data){
}
worker.onmessageerror = function(e){
}
worker.onexit = function(){
}
worker.onmessage = function(e) {
var data = e.data;
switch (data.type) {
case 'readPngImageAsync':
callback(data.receiver, data.data)
break;
default:
break
}
worker.terminate();
}
var obj = { type: 'readPngImageAsync', data:pngBuffer}
worker.postMessage(obj, [pngBuffer])
}
writePngWithStringAsync(addInfo:string, pngBuffer:ArrayBuffer, callback:PngCallback<ArrayBuffer,any>) {
let worker = new ArkWorker.Worker('workers/worker1.js', { type: 'classic', name: 'writePngWithStringAsync'})
worker.onerror = function(data){
}
worker.onmessageerror = function(e){
}
worker.onexit = function(){
}
worker.onmessage = function(e) {
var data = e.data;
switch (data.type) {
case 'writePngWithStringAsync':
callback(data.receiver, data.data)
break;
default:
break
}
worker.terminate();
}
var obj = { type: 'writePngWithStringAsync', data:pngBuffer, info: addInfo}
worker.postMessage(obj, [pngBuffer])
}
writePngAsync(pngBuffer:ArrayBuffer, callback:PngCallback<ArrayBuffer,any>) {
let worker = new ArkWorker.Worker('workers/worker1.js', { type: 'classic', name: 'writePngAsync'})
worker.onerror = function(data){
}
worker.onmessageerror = function(e){
}
worker.onexit = function(){
}
worker.onmessage = function(e) {
var data = e.data;
switch (data.type) {
case 'writePngAsync':
callback(data.receiver, data.data)
break;
default:
break
}
worker.terminate();
}
var obj = { type: 'writePngAsync', data:pngBuffer}
worker.postMessage(obj, [pngBuffer])
}
}

View File

@ -15,7 +15,7 @@
import router from '@system.router';
import {Pngj} from '../glide/pngj/Pngj'
import resourceManager from '@ohos.resourceManager';
import {FileUtils} from '../cache/FileUtils.ets'
import {FileUtils} from '../cache/FileUtils'
import featureability from '@ohos.ability.featureAbility'
@Entry
@ -70,7 +70,7 @@ struct PngjTestCasePage {
Button('测试readPngImage')
.onClick(() => {
let pngj = new Pngj();
pngj.readPngImage(this.pngSource, (value) => {
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);
@ -79,7 +79,7 @@ struct PngjTestCasePage {
Button('测试writePngWithString')
.onClick(() => {
let pngj = new Pngj();
pngj.writePngWithString('hello world', this.pngSource, (value) => {
pngj.writePngWithString('hello world', this.pngSource, (sender, value) => {
FileUtils.getInstance().createFileProcess(
this.rootFolder + '/pngj',
@ -92,7 +92,53 @@ struct PngjTestCasePage {
Button('测试writePng')
.onClick(() => {
let pngj = new Pngj();
pngj.writePng(this.pngSource, (value) => {
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, (value) => {
this.hint1 = JSON.stringify(value);
})
}).margin({ top: 5, left: 10 })
Button('测试readPngImageAsync')
.onClick(() => {
let pngj = new Pngj();
pngj.readPngImage(this.pngSource, (sender, value) => {
this.pngSource = sender
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('测试writePngWithStringAsync')
.onClick(() => {
let pngj = new Pngj();
pngj.writePngWithString('hello world', this.pngSource, (sender, value) => {
this.pngSource = sender
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('测试writePngAsync')
.onClick(() => {
let pngj = new Pngj();
pngj.writePng(this.pngSource, (sender, value) => {
this.pngSource = sender
FileUtils.getInstance().createFileProcess(
this.rootFolder + '/pngj',
this.rootFolder + '/pngj/newPng2.png',

View File

@ -13,10 +13,32 @@
* limitations under the License.
*/
import arkWorker from '@ohos.worker';
import {UPNG} from '../glide/pngj/UPNG'
arkWorker.parentPort.onmessage = function (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;