From c11d61a577ada2ced6a97cb70fd1badff3e8f826 Mon Sep 17 00:00:00 2001 From: 24186 <2418639820@qq.com> Date: Thu, 1 Feb 2024 00:17:10 +0800 Subject: [PATCH] =?UTF-8?q?work=E6=9B=B4=E6=94=B9=E4=B8=BAyaskpool?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/pages/pngjTestCasePage.ets | 12 +- .../ets/components/imageknife/pngj/Png.ets | 200 ++++++++++++++++++ .../ets/components/imageknife/pngj/Pngj.ts | 69 ++++-- 3 files changed, 253 insertions(+), 28 deletions(-) create mode 100644 library/src/main/ets/components/imageknife/pngj/Png.ets diff --git a/entry/src/main/ets/pages/pngjTestCasePage.ets b/entry/src/main/ets/pages/pngjTestCasePage.ets index a7ff0e1..19680cf 100644 --- a/entry/src/main/ets/pages/pngjTestCasePage.ets +++ b/entry/src/main/ets/pages/pngjTestCasePage.ets @@ -115,18 +115,18 @@ struct PngjTestCasePage { if (!this.pngdecodeRun2) { this.pngdecodeRun2 = true; let pngj = new Pngj(); - let png_worker = new worker.ThreadWorker('entry/ets/workers/upngWorkerTestCase.ets', { - type: 'classic', - name: 'readPngImageAsync' - }) - pngj.readPngImageAsync(png_worker, this.pngSource2!, {pngCallback: (sender:ArrayBuffer, value:Record) => { + // let png_worker = new worker.ThreadWorker('entry/ets/workers/upngWorkerTestCase.ets', ) + let obj ={ + type: 'classic', + name: 'readPngImageAsync' + } + pngj.readPngImageAsync(obj, this.pngSource2!, {pngCallback: (sender:ArrayBuffer, value:Record) => { this.pngSource2 = sender this.hint8 = '重新获取buffer才能测试' this.hint2 = 'img with=' + value.width + ' img height=' + value.height + ' img depth=' + value.depth + ' img ctype=' + value.ctype this.pngdecodeRun2 = false; }}) - } else { this.hint8 = '已经在执行了,请稍等' } diff --git a/library/src/main/ets/components/imageknife/pngj/Png.ets b/library/src/main/ets/components/imageknife/pngj/Png.ets new file mode 100644 index 0000000..02bec03 --- /dev/null +++ b/library/src/main/ets/components/imageknife/pngj/Png.ets @@ -0,0 +1,200 @@ +/* + * 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. + */ +import {UPNG} from '../../3rd_party/upng/UPNG'; +import {PngCallback} from './PngCallback'; +import image from '@ohos.multimedia.image'; +import resourceManager from '@ohos.resourceManager'; +import ArkWorker from '@ohos.worker' +import { BusinessError } from '@ohos.base' +export class Pngj { + readPngImageInfo(arraybuffer: ArrayBuffer, callback:PngCallback) { + let imageSource:image.ImageSource = image.createImageSource(arraybuffer); + if (imageSource != undefined){ + imageSource.getImageInfo((err:BusinessError, value:image.ImageInfo) => { + if (err) { + return; + } + callback.pngCallback(arraybuffer, value); + }); + } + + } + readPngImage(pngBuffer: ArrayBuffer, callback:PngCallback) { + var png = UPNG.decode(pngBuffer); + callback.pngCallback(pngBuffer, png) + } + + writePngWithString(addInfo:string, pngBuffer: ArrayBuffer,callback:PngCallback) { + var pngDecode = UPNG.decode(pngBuffer); + var newPng = UPNG.encodeWithString(addInfo, UPNG.toRGBA8(pngDecode), pngDecode.width, pngDecode.height, 0) + callback.pngCallback(pngBuffer, newPng); + } + + writePng(pngBuffer: ArrayBuffer,callback:PngCallback) { + var pngDecode = UPNG.decode(pngBuffer); + var newPng = UPNG.encode(UPNG.toRGBA8(pngDecode), pngDecode.width, pngDecode.height, 0) + callback.pngCallback(pngBuffer, newPng); + } + + readPngImageAsync(type: any, pngBuffer: ArrayBuffer, callback: PngCallback) { + // 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.pngCallback(data.receiver, data.data) + // break; + // default: + // break + // } + // worker.terminate(); + // } + // var obj = { type: 'readPngImageAsync', data: pngBuffer } + // worker.postMessage(obj, [pngBuffer]) + let task = new taskpool.Task(taskParseImage,type, arrayBuffer, callback) + // task.setTransferList([]) + taskpool.execute(task).then((pixelmap: Object) => { + LogUtil.log('ceshi321 : Succeeded in creating pixelmap Ui .' + (pixelmap as image.PixelMap).getPixelBytesNumber()) + onCompleteFunction(pixelmap as image.PixelMap); + }).catch((err: string) => { + LogUtil.log("ceshi321 : test occur error: " + err) + onErrorFunction(err); + }); + + + + } + // taskPoolExecutePixelMap(arrayBuffer: ArrayBuffer, onCompleteFunction: (value: PixelMap) => void | PromiseLike, onErrorFunction: (reason?: BusinessError | string) => void) { + // let task = new taskpool.Task(taskParseImage, arrayBuffer, scale) + // task.setTransferList([]) + // taskpool.execute(task).then((pixelmap: Object) => { + // + // }).catch((err: string) => { + // LogUtil.log("ceshi321 : " + err) + // }); + // } + + + + + + + + + + + + + + writePngWithStringAsync(worker: any, addInfo: string, pngBuffer: ArrayBuffer, callback: PngCallback) { + 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.pngCallback(data.receiver, data.data) + break; + default: + break + } + worker.terminate(); + } + + var obj = { type: 'writePngWithStringAsync', data:pngBuffer, info: addInfo} + worker.postMessage(obj, [pngBuffer]) + + } + + writePngAsync(worker: any, pngBuffer: ArrayBuffer, callback: PngCallback) { + 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.pngCallback(data.receiver, data.data) + break; + default: + break + } + worker.terminate(); + } + + var obj = { type: 'writePngAsync', data:pngBuffer} + worker.postMessage(obj, [pngBuffer]) + + } + +} +@Concurrent +async function taskParseImage(type, arrayBuffer, callback): Promise { + + switch (type.type) { + case 'readPngImageAsync': + callback(data.receiver, data.data) + break; + default: + break + } + + let obj = { type: 'readPngImageAsync', data: arrayBuffer } + + return (obj,[arrayBuffer]) +} + +// function taskPoolExecutePixelMap(arrayBuffer: ArrayBuffer, scale: number, onCompleteFunction: (value: PixelMap) => void | PromiseLike, onErrorFunction: (reason?: BusinessError | string) => void) { +// LogUtil.log("ceshi321 : arrayBuffer长度" + arrayBuffer.byteLength) +// let task = new taskpool.Task(taskParseImage, arrayBuffer, scale) +// task.setTransferList([]) +// taskpool.execute(task).then((pixelmap: Object) => { +// LogUtil.log('ceshi321 : Succeeded in creating pixelmap Ui .' + (pixelmap as image.PixelMap).getPixelBytesNumber()) +// onCompleteFunction(pixelmap as image.PixelMap); +// }).catch((err: string) => { +// LogUtil.log("ceshi321 : test occur error: " + err) +// onErrorFunction(err); +// }); +// } diff --git a/library/src/main/ets/components/imageknife/pngj/Pngj.ts b/library/src/main/ets/components/imageknife/pngj/Pngj.ts index fb94dfe..a0a3153 100644 --- a/library/src/main/ets/components/imageknife/pngj/Pngj.ts +++ b/library/src/main/ets/components/imageknife/pngj/Pngj.ts @@ -63,33 +63,58 @@ export class Pngj { } readPngImageAsync(worker: any, pngBuffer: ArrayBuffer, callback: PngCallback) { - worker.onerror = function (data) { + // 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.pngCallback(data.receiver, data.data) + // break; + // default: + // break + // } + // worker.terminate(); + // } + // var obj = { type: 'readPngImageAsync', data: pngBuffer } + // worker.postMessage(obj, [pngBuffer]) + let task = new taskpool.Task(taskParseImage, arrayBuffer, scale) + task.setTransferList([]) + taskpool.execute(task).then((pixelmap: Object) => { + LogUtil.log('ceshi321 : Succeeded in creating pixelmap Ui .' + (pixelmap as image.PixelMap).getPixelBytesNumber()) + onCompleteFunction(pixelmap as image.PixelMap); + }).catch((err: string) => { + LogUtil.log("ceshi321 : test occur error: " + err) + onErrorFunction(err); + }); - } - worker.onmessageerror = function (e) { - } - - worker.onexit = function () { - - } - - worker.onmessage = function(e) { - var data = e.data; - switch (data.type) { - case 'readPngImageAsync': - callback.pngCallback(data.receiver, data.data) - break; - default: - break - } - worker.terminate(); - } - var obj = { type: 'readPngImageAsync', data: pngBuffer } - worker.postMessage(obj, [pngBuffer]) } + + + + + + + + + + + + + writePngWithStringAsync(worker: any, addInfo: string, pngBuffer: ArrayBuffer, callback: PngCallback) { worker.onerror = function (data) {