From 25b3722475f2c6bc5ada453d8c727456a4bade47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=8E=E6=9C=88=E6=B8=85=E9=A3=8E?= Date: Fri, 2 Feb 2024 18:32:59 +0800 Subject: [PATCH] =?UTF-8?q?pngWorker=E7=BA=BF=E7=A8=8B=E6=94=B9=E4=B8=BAta?= =?UTF-8?q?skpool?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 明月清风 --- CHANGELOG.md | 1 + README.md | 1 + entry/src/main/ets/pages/pngjTestCasePage.ets | 35 ++-- library/PngWork.ets | 61 +++++++ library/PngWork.js | 47 ------ .../components/imageknife/pngj/PngCallback.ts | 5 + .../ets/components/imageknife/pngj/Pngj.ets | 123 ++++++++++++++ .../ets/components/imageknife/pngj/Pngj.ts | 153 ------------------ 8 files changed, 207 insertions(+), 219 deletions(-) create mode 100644 library/PngWork.ets delete mode 100644 library/PngWork.js create mode 100644 library/src/main/ets/components/imageknife/pngj/Pngj.ets delete mode 100644 library/src/main/ets/components/imageknife/pngj/Pngj.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e4d6c5..6007ae1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## 2.1.2-rc.6 - 修复手机调节显示大小时图片消失 +- pngWorker线程改为taskpool ## 2.1.2-rc.5 - moduleContext新增缓存策略,缓存上限5,缓存策略Lru diff --git a/README.md b/README.md index df64d43..045ea6b 100644 --- a/README.md +++ b/README.md @@ -430,6 +430,7 @@ export default class EntryAbility extends UIAbility { ## 约束与限制 在下述版本验证通过: +DevEco Studio 4.1(4.1.3.520)--SDK:API11( 4.1.0.63) DevEco Studio 4.1(4.1.3.418)--SDK:API11( 4.1.0.56) DevEco Studio 4.1(4.1.3.322)--SDK:API11( 4.1.0.36) DevEco Studio 4.0(4.0.3.700)--SDK:API10( 4.0.10.15) diff --git a/entry/src/main/ets/pages/pngjTestCasePage.ets b/entry/src/main/ets/pages/pngjTestCasePage.ets index a7ff0e1..3969de7 100644 --- a/entry/src/main/ets/pages/pngjTestCasePage.ets +++ b/entry/src/main/ets/pages/pngjTestCasePage.ets @@ -12,15 +12,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import router from '@system.router'; import { Pngj } from '@ohos/libraryimageknife' import resourceManager from '@ohos.resourceManager'; import { FileUtils,ImageKnifeGlobal } from '@ohos/libraryimageknife' -import featureability from '@ohos.ability.featureAbility' -import ArkWorker from '@ohos.worker' -import worker from '@ohos.worker'; import { BusinessError } from '@ohos.base' import common from '@ohos.app.ability.common'; +interface WorkerType { + type: string + name: string +} @Entry @Component struct PngjTestCasePage { @@ -115,17 +115,16 @@ struct PngjTestCasePage { if (!this.pngdecodeRun2) { this.pngdecodeRun2 = true; let pngj = new Pngj(); - let png_worker = new worker.ThreadWorker('entry/ets/workers/upngWorkerTestCase.ets', { + let png_worker: WorkerType = { type: 'classic', name: 'readPngImageAsync' - }) - pngj.readPngImageAsync(png_worker, this.pngSource2!, {pngCallback: (sender:ArrayBuffer, value:Record) => { - this.pngSource2 = sender + } + pngj.readPngImageAsync(png_worker, this.pngSource2!, (value:ESObject) => { 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 = '已经在执行了,请稍等' @@ -161,12 +160,11 @@ struct PngjTestCasePage { if (!this.pngdecodeRun3) { this.pngdecodeRun3 = true; let pngj = new Pngj(); - let png_worker = new worker.ThreadWorker('entry/ets/workers/upngWorkerTestCase.ets', { + let png_worker: WorkerType = { type: 'classic', name: 'writePngWithStringAsync' - }) - pngj.writePngWithStringAsync(png_worker, 'hello world', this.pngSource3, {pngCallback: (sender:ArrayBuffer, value:ArrayBuffer) => { - this.pngSource3 = sender + } + pngj.writePngWithStringAsync(png_worker, this.pngSource3, ( value:ESObject) => { FileUtils.getInstance().createFileProcess( this.rootFolder + '/pngj', this.rootFolder + '/pngj/newPng.png', @@ -174,7 +172,7 @@ struct PngjTestCasePage { let png1 = new Uint8Array(value) this.hint3 = 'png写入后长度' + png1.byteLength + '目录文件:' + this.rootFolder + '/pngj/newPng.png' + '保存后使用2进制查看数据是否新增' this.pngdecodeRun3 = false; - }}) + },'hello world') } else { this.hint9 = '已经在执行了,请稍等' } @@ -210,12 +208,11 @@ struct PngjTestCasePage { if (!this.pngdecodeRun4) { this.pngdecodeRun4 = true; let pngj = new Pngj(); - let png_worker = new worker.ThreadWorker('entry/ets/workers/upngWorkerTestCase.ets', { + let png_worker: WorkerType = { type: 'classic', name: 'writePngAsync' - }) - pngj.writePngAsync(png_worker, this.pngSource4,{pngCallback: (sender:ArrayBuffer, value:ArrayBuffer) => { - this.pngSource4 = sender + } + pngj.writePngAsync(png_worker, this.pngSource4, ( value:ESObject) => { FileUtils.getInstance().createFileProcess( this.rootFolder + '/pngj', this.rootFolder + '/pngj/newPng2.png', @@ -223,7 +220,7 @@ struct PngjTestCasePage { let png2 = new Uint8Array(value) this.hint4 = 'png2未写入长度' + png2.byteLength + '目录文件:' + this.rootFolder + '/pngj/newPng2.png' + '保存后使用2进制查看数据是否新增' this.pngdecodeRun4 = false; - }}) + }) } else { this.hint10 = '已经在执行了,请稍等' } diff --git a/library/PngWork.ets b/library/PngWork.ets new file mode 100644 index 0000000..d6492b8 --- /dev/null +++ b/library/PngWork.ets @@ -0,0 +1,61 @@ +/* + * 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 taskpool from '@ohos.taskpool'; +import { UPNG } from './src/main/ets/components/3rd_party/upng/UPNG' +import { WorkerType } from "./src/main/ets/components/imageknife/pngj/PngCallback" + +export function handler(e: WorkerType, source: ArrayBuffer, pngCallback: (value:ESObject) => void, info?: string) { + let task: taskpool.Task | undefined = undefined + if (info == undefined) { + task = new taskpool.Task(printArgs, e) + } else { + task = new taskpool.Task(printArgs, e, source) + } + let val1: ESObject = taskpool.execute(task) + pngCallback(val1) + try { + taskpool.cancel(task) + } catch (e) { + console.error("taskpool.cancel occur error:" + e) + } +} + +@Concurrent +function printArgs(e: WorkerType, pngSource: ArrayBuffer, info?: string): Object | ArrayBufferLike | void { + let a: Object | ArrayBufferLike | undefined = undefined + let png = UPNG.decode(pngSource) + switch (e.name) { + case 'readPngImageAsync': + let array: Uint8Array = png.data; + let arrayData = array.buffer.slice(array.byteOffset, array.byteLength + array.byteOffset) + png.data = arrayData; + a = png + break; + case 'writePngWithStringAsync': + let addInfo: string | undefined = info; + let newPng = UPNG.encodeWithString(addInfo, UPNG.toRGBA8(png), png.width, png.height, 0) + a = newPng + break; + case 'writePngAsync': + let newPng3 = UPNG.encode(UPNG.toRGBA8(png), png.width, png.height, 0) + a = newPng3 + break; + default: + break + } + if(a != undefined) { return a } +} + + diff --git a/library/PngWork.js b/library/PngWork.js deleted file mode 100644 index 1a86729..0000000 --- a/library/PngWork.js +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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 arkWorker from '@ohos.worker'; -import {UPNG} from './src/main/ets/components/3rd_party/upng/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; - default: - break - } -} - - diff --git a/library/src/main/ets/components/imageknife/pngj/PngCallback.ts b/library/src/main/ets/components/imageknife/pngj/PngCallback.ts index 3d3b297..394b6bb 100644 --- a/library/src/main/ets/components/imageknife/pngj/PngCallback.ts +++ b/library/src/main/ets/components/imageknife/pngj/PngCallback.ts @@ -15,4 +15,9 @@ export interface PngCallback{ pngCallback: (sender:R, receover:T)=>void +} + +export interface WorkerType { + type: string + name: string } \ No newline at end of file diff --git a/library/src/main/ets/components/imageknife/pngj/Pngj.ets b/library/src/main/ets/components/imageknife/pngj/Pngj.ets new file mode 100644 index 0000000..860ed48 --- /dev/null +++ b/library/src/main/ets/components/imageknife/pngj/Pngj.ets @@ -0,0 +1,123 @@ +/* + * 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, WorkerType } from './PngCallback'; +import image from '@ohos.multimedia.image'; +import taskpool from '@ohos.taskpool'; +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); + }); + } + + } + + /** + * + * @param pngBuffer ArrayBuffer containing the PNG file + * @param callback + * returns an image object with following properties: + * width: the width of the image + * height: the height of the image + * depth: number of bits per channel + * ctype: color type of the file (Truecolor, Grayscale, Palette ...) + * frames: additional info about frames (frame delays etc.) + * tabs: additional chunks of the PNG file + * data: pixel data of the image + */ + readPngImage(pngBuffer: ArrayBuffer, callback: PngCallback) { + let png = UPNG.decode(pngBuffer); + callback.pngCallback(pngBuffer, png) + } + + writePngWithString(addInfo: string, pngBuffer: ArrayBuffer, callback: PngCallback) { + let pngDecode = UPNG.decode(pngBuffer); + let newPng = UPNG.encodeWithString(addInfo, UPNG.toRGBA8(pngDecode), pngDecode.width, pngDecode.height, 0) + callback.pngCallback(pngBuffer, newPng); + } + + writePng(pngBuffer: ArrayBuffer, callback: PngCallback) { + let pngDecode = UPNG.decode(pngBuffer); + let newPng = UPNG.encode(UPNG.toRGBA8(pngDecode), pngDecode.width, pngDecode.height, 0) + callback.pngCallback(pngBuffer, newPng); + } + + async readPngImageAsync(e: WorkerType, source: ArrayBuffer, pngCallback: (value: ESObject) => void) { + let task = new taskpool.Task(taskPngImage, e, source) + let val1: ESObject = await taskpool.execute(task) + pngCallback(val1) + try { + taskpool.cancel(task) + } catch (e) { + console.error("taskpool.cancel occur error:" + e) + } + } + + async writePngWithStringAsync(e: WorkerType, source: ArrayBuffer, pngCallback: (value: ESObject) => void, info: string) { + let task = new taskpool.Task(taskPngImage, e, source, info) + let val1: ESObject = await taskpool.execute(task) + pngCallback(val1) + try { + taskpool.cancel(task) + } catch (e) { + console.error("taskpool.cancel occur error:" + e) + } + } + + async writePngAsync(e: WorkerType, source: ArrayBuffer, pngCallback: (value: ESObject) => void) { + let task = new taskpool.Task(taskPngImage, e, source) + let val1: ESObject = await taskpool.execute(task) + pngCallback(val1) + try { + taskpool.cancel(task) + } catch (e) { + console.error("taskpool.cancel occur error:" + e) + } + } +} + +@Concurrent +function taskPngImage(e: WorkerType, pngSource: ArrayBuffer, info?: string): Object | ArrayBufferLike | void { + let a: Object | ArrayBufferLike | undefined = undefined + let png = UPNG.decode(pngSource) + switch (e.name) { + case 'readPngImageAsync': + let array: Uint8Array = png.data; + let arrayData = array.buffer.slice(array.byteOffset, array.byteLength + array.byteOffset) + png.data = arrayData; + a = png + break; + case 'writePngWithStringAsync': + let addInfo: string | undefined = info; + let newPng = UPNG.encodeWithString(addInfo, UPNG.toRGBA8(png), png.width, png.height, 0) + a = newPng + break; + case 'writePngAsync': + let newPng3 = UPNG.encode(UPNG.toRGBA8(png), png.width, png.height, 0) + a = newPng3 + break; + default: + break + } + if(a != undefined) { return a } +} \ No newline at end of file diff --git a/library/src/main/ets/components/imageknife/pngj/Pngj.ts b/library/src/main/ets/components/imageknife/pngj/Pngj.ts deleted file mode 100644 index fb94dfe..0000000 --- a/library/src/main/ets/components/imageknife/pngj/Pngj.ts +++ /dev/null @@ -1,153 +0,0 @@ -/* - * 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); - }); - } - - } - - /** - * - * @param pngBuffer ArrayBuffer containing the PNG file - * @param callback - * returns an image object with following properties: - * width: the width of the image - * height: the height of the image - * depth: number of bits per channel - * ctype: color type of the file (Truecolor, Grayscale, Palette ...) - * frames: additional info about frames (frame delays etc.) - * tabs: additional chunks of the PNG file - * data: pixel data of the image - */ - 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(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 '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) { - - } - - 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]) - - } - -} \ No newline at end of file