1.新增pngLoadWorker.ets 和 pngWorkers.ts

Signed-off-by: zhoulisheng <635547767@qq.com>
This commit is contained in:
zhoulisheng 2023-11-21 15:44:46 +08:00
parent 185cff282c
commit 6875090633
2 changed files with 67 additions and 0 deletions

View File

@ -0,0 +1,23 @@
/*
* Copyright (C) 2023 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 worker, { ThreadWorkerGlobalScope, MessageEvents, ErrorEvent } from '@ohos.worker';
import { UPNG } from '@ohos/libraryimageknife'
import {resolvePngWorker} from './pngWorker'
const workerPort: ThreadWorkerGlobalScope = worker.workerPort;
workerPort.onmessage = (e: MessageEvents)=> {
resolvePngWorker(workerPort,UPNG,e)
}

View File

@ -0,0 +1,44 @@
/*
* Copyright (C) 2023 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 worker, { ThreadWorkerGlobalScope, MessageEvents, ErrorEvent } from '@ohos.worker';
export function resolvePngWorker (workerPort,UPNG, e: MessageEvents) {
let data = e.data;
switch (data.type) {
case 'readPngImageAsync':
let 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 }
workerPort.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 }
workerPort.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 }
workerPort.postMessage(dataObj3, [newPng3, data.data]);
break;
default:
break
}
}