ImageKnife/entry/src/main/ets/pages/pngjTestCasePage.ets

258 lines
10 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* 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 { Pngj } from '@ohos/libraryimageknife'
import resourceManager from '@ohos.resourceManager';
import { FileUtils,ImageKnifeGlobal } from '@ohos/libraryimageknife'
import { BusinessError } from '@ohos.base'
import common from '@ohos.app.ability.common';
interface WorkerType {
type: string
name: string
}
@Entry
@Component
struct PngjTestCasePage {
pngSource1?: ArrayBuffer = undefined;
pngSource2?: ArrayBuffer = undefined;
pngSource3?: ArrayBuffer = undefined;
pngSource4?: ArrayBuffer = undefined;
pngdecodeRun1: boolean = false;
pngdecodeRun2: boolean = false;
pngdecodeRun3: boolean = false;
pngdecodeRun4: boolean = false;
rootFolder: string = '';
@State hint1: string = 'readPngImageInfo内容展示'
@State hint2: string = 'readPngImage内容展示'
@State hint3: string = 'writePngWithString内容展示'
@State hint4: string = 'writePngWithString内容展示'
@State hint5: string = '测试readMetadata'
@State hint6: string = '测试writeMetadata'
@State hint7: string = '首先点击获取PNG图片buffer'
@State hint8: string = '首先点击获取PNG图片buffer'
@State hint9: string = '首先点击获取PNG图片buffer'
@State hint10: string = '首先点击获取PNG图片buffer'
build() {
Scroll() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Text('pngj接口测试列表').fontSize(20)
Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Button(this.hint7).fontSize(30)
.onClick(() => {
this.rootFolder = (ImageKnifeGlobal.getInstance().getHapContext() as common.UIAbilityContext).filesDir as string;
((ImageKnifeGlobal.getInstance().getHapContext() as common.UIAbilityContext)
.resourceManager as resourceManager.ResourceManager)
.getMediaContent($r('app.media.pngSample').id)
.then(data => {
this.pngSource1 = FileUtils.getInstance().uint8ArrayToBuffer(data);
this.hint7 = '获取buffer成功可以测试'
})
.catch( (err:BusinessError) => {
console.log('点击获取Png图片buffer err=' + err)
})
})
Button('测试readPng')
.onClick(() => {
if (this.pngSource1!=undefined) {
if (!this.pngdecodeRun1) {
this.pngdecodeRun1 = true;
let pngj = new Pngj();
pngj.readPngImageInfo(this.pngSource1,{pngCallback: (sender, value) => {
this.hint1 = JSON.stringify(value);
this.pngdecodeRun1 = false;
}})
} else {
this.hint7 = '已经在执行了,请稍等'
}
} else {
this.hint7 = '请先点击此处获取buffer'
}
}).margin({ top: 5, left: 10 })
}.width('100%')
.height(60).backgroundColor(Color.Pink)
Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Button(this.hint8).fontSize(30)
.onClick(() => {
this.rootFolder = (ImageKnifeGlobal.getInstance().getHapContext() as common.UIAbilityContext).filesDir as string;
((ImageKnifeGlobal.getInstance().getHapContext() as common.UIAbilityContext)
.resourceManager as resourceManager.ResourceManager)
.getMediaContent($r('app.media.pngSample').id)
.then(data => {
this.pngSource2 = FileUtils.getInstance().uint8ArrayToBuffer(data);
this.hint8 = '获取buffer成功可以测试'
})
.catch((err:BusinessError) => {
console.log('点击获取Png图片buffer err=' + err)
})
})
Button('readPngAsync')
.onClick(() => {
if (this.pngSource2!=undefined) {
if (!this.pngdecodeRun2) {
this.pngdecodeRun2 = true;
let pngj = new Pngj();
let png_worker: WorkerType = {
type: 'classic',
name: 'readPngImageAsync'
}
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 = '已经在执行了,请稍等'
}
} else {
this.hint8 = '请先点击此处获取buffer'
}
}).margin({ top: 5, left: 10 })
}.width('100%')
.height(60).backgroundColor(Color.Pink)
Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Button(this.hint9).fontSize(30)
.onClick(() => {
this.rootFolder = (ImageKnifeGlobal.getInstance().getHapContext() as common.UIAbilityContext).filesDir as string;
((ImageKnifeGlobal.getInstance().getHapContext() as common.UIAbilityContext)
.resourceManager as resourceManager.ResourceManager)
.getMediaContent($r('app.media.pngSample').id)
.then(data => {
this.pngSource3 = FileUtils.getInstance().uint8ArrayToBuffer(data);
this.hint9 = '获取buffer成功可以测试'
})
.catch((err:BusinessError) => {
console.log('点击获取Png图片buffer err=' + err)
})
})
Button('测试writePngWithString')
.onClick(() => {
if (this.pngSource3 != undefined) {
if (!this.pngdecodeRun3) {
this.pngdecodeRun3 = true;
let pngj = new Pngj();
let png_worker: WorkerType = {
type: 'classic',
name: 'writePngWithStringAsync'
}
pngj.writePngWithStringAsync(png_worker, this.pngSource3, ( value:ESObject) => {
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进制查看数据是否新增'
this.pngdecodeRun3 = false;
},'hello world')
} else {
this.hint9 = '已经在执行了,请稍等'
}
} else {
this.hint9 = '请先点击此处获取buffer'
}
}).margin({ top: 5, left: 10 })
}.width('100%')
.height(60).backgroundColor(Color.Pink)
Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Button(this.hint10).fontSize(30)
.onClick(() => {
this.rootFolder = (ImageKnifeGlobal.getInstance().getHapContext() as common.UIAbilityContext).filesDir as string;
((ImageKnifeGlobal.getInstance().getHapContext() as common.UIAbilityContext)
.resourceManager as resourceManager.ResourceManager)
.getMediaContent($r('app.media.pngSample').id)
.then(data => {
this.pngSource4 = FileUtils.getInstance().uint8ArrayToBuffer(data);
this.hint10 = '获取buffer成功可以测试'
})
.catch((err:BusinessError) => {
console.log('点击获取Png图片buffer err=' + err)
})
})
Button('writePng')
.onClick(()=>{
if (this.pngSource4 != undefined) {
if (!this.pngdecodeRun4) {
this.pngdecodeRun4 = true;
let pngj = new Pngj();
let png_worker: WorkerType = {
type: 'classic',
name: 'writePngAsync'
}
pngj.writePngAsync(png_worker, this.pngSource4, ( value:ESObject) => {
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进制查看数据是否新增'
this.pngdecodeRun4 = false;
})
} else {
this.hint10 = '已经在执行了,请稍等'
}
} else {
this.hint10 = '请先点击此处获取buffer'
}
})
.margin({ top: 5, left: 10 })
}.width('100%')
.height(60).backgroundColor(Color.Pink)
Text(this.hint1)
.width('100%')
.height(120)
Text(this.hint2)
.width('100%')
.height(120)
Text(this.hint3)
.width('100%')
.height(120)
Text(this.hint4)
.width('100%')
.height(120)
}
.width('100%')
.height('100%')
}
}
typedArrayToBuffer(array: Uint8Array): ArrayBuffer {
return array.buffer.slice(array.byteOffset, array.byteLength + array.byteOffset)
}
}