From 5e9c3401b9ace8510c81ff088635e4622f9d95a4 Mon Sep 17 00:00:00 2001 From: fan-congcong01 Date: Tue, 2 Apr 2024 10:44:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=94=AF=E6=8C=81=E6=B2=99?= =?UTF-8?q?=E7=AE=B1=E8=B7=AF=E5=BE=84=E4=B8=8B=E5=9B=BE=E7=89=87=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fan-congcong01 --- CHANGELOG.md | 3 ++ README.md | 17 ++++--- .../main/ets/entryability/EntryAbility.ets | 2 - entry/src/main/ets/pages/Index.ets | 12 +++++ library/index.ets | 3 +- library/oh-package.json5 | 2 +- .../src/main/ets/components/GlobalContext.ets | 44 ------------------- library/src/main/ets/components/apng.ets | 33 ++++++++------ library/src/main/ets/components/manager.ets | 32 +++++++++++--- oh-package.json5 | 2 +- 10 files changed, 70 insertions(+), 80 deletions(-) delete mode 100644 library/src/main/ets/components/GlobalContext.ets diff --git a/CHANGELOG.md b/CHANGELOG.md index 0179155..fb08a21 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 1.0.0-rc.2 + 1.新增支持沙箱路径下图片功能 + ## 1.0.0-rc.1 1.apng用canvas实现时出现部分残影及图形缺失问题修改 2.修改倍速不生效问题 diff --git a/README.md b/README.md index de681a3..3655c3c 100644 --- a/README.md +++ b/README.md @@ -16,14 +16,9 @@ ohpm install @ohos/apng OpenHarmony ohpm 环境配置等更多内容,请参考 [如何安装 OpenHarmony ohpm 包](https://gitee.com/openharmony-tpc/docs/blob/master/OpenHarmony_har_usage.md) ## 使用说明 + ``` - 1、需要在EntryAbility文件引入 import { GlobalContext } from '@ohos/apng' - 在onCreate函数中调用,传入上下文对象,用作后续读取本地图片资源文件 - 示例: - GlobalContext.getContext().setObject('MainContext',this.context); -``` -``` - 2.引入 import apng from '@ohos/apng'; + 引入 import apng from '@ohos/apng'; 示例1: apng({ src: $r('app.media.stack'), //图片资源 @@ -40,13 +35,18 @@ OpenHarmony ohpm 环境配置等更多内容,请参考 [如何安装 OpenHarmo src: this.srcUint8Array, // Uint8Array对象资源 speedRate: 1 //动画倍速 }) + + 示例4: + apng({ + src: getContext().filesDir + '/stack.png', // 沙箱路径 + speedRate: 1 //动画倍速 + }) ``` ## 接口说明 | **接口** | 参数 | 功能 | |-----------------------------------------------------------------------|-----------------------------------------------------------------------|-----------| | apng(src: Resource/Uint8Array/string, speedRate: number) | src:图片资源,支持本地资源,网络图片以及Uint8Array三种格式
speedRate:动画倍速,默认值为1 | apng图片展示。 | -| GlobalContext.getContext().setObject(key: string,objectClass: Object) | key:上下文对象对应的key,固定值 "MainContext"
objectClass:上下文对象(this.context) | 在EntryAbility文件设置上下文对象 | ## 约束与限制 @@ -69,7 +69,6 @@ DevEco Studio: 5.0.3.121, SDK: API12 (5.0.0.16) | |---- apng.ets # 处理apng拆解后的每一帧,每一帧通过canvas绘制成apng | |---- crc32.ets # 用作数据传输和存储中的错误检测 | |---- manager.ets # 读取本地apng文件,获取到文件buffer对象 -| |---- GlobalContext.ets # 创建了一个全局类,用来获取数据对象或者设置对象的值 | |---- parser.ets # 对buffer对象进行拆解 | |---- structs.ets # 创建了两个类,APNG类指的是整个APNG动画,包括宽度、高度、播放次数、播放时间和帧列表等属性,Frame类指的是APNG动画中的每一帧 | |---- README.MD # 安装使用方法 diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index a31be17..aa5f32b 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -23,13 +23,11 @@ import UIAbility from '@ohos.app.ability.UIAbility'; import hilog from '@ohos.hilog'; import window from '@ohos.window'; -import { GlobalContext } from '@ohos/apng'; import Want from '@ohos.app.ability.Want'; import AbilityConstant from '@ohos.app.ability.AbilityConstant'; export default class EntryAbility extends UIAbility { onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { - GlobalContext.getContext().setObject('MainContext', this.context); hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); } diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index bdd073e..9a035f5 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -33,6 +33,7 @@ struct Index { loadApng() { Scroll() { Column() { + Text('资源文件图片') Row() { apng({ src: $r('app.media.stack'), @@ -42,6 +43,17 @@ struct Index { .width('100%') .height('30%') + Text('沙箱路径图片') + Row() { + apng({ + src: getContext().filesDir + '/stack.png', + speedRate: this.speedRate + }) + } + .width('100%') + .height('30%') + + Text('网络路径图片') Row() { apng({ src: 'https://gitee.com/openharmony-sig/ohos_apng/raw/master/entry/src/main/resources/base/media/stack.png', diff --git a/library/index.ets b/library/index.ets index 23719b1..1a6d5a8 100644 --- a/library/index.ets +++ b/library/index.ets @@ -21,6 +21,5 @@ */ import { apng } from './src/main/ets/components/apng'; -import { GlobalContext } from './src/main/ets/components/GlobalContext'; -export { apng, GlobalContext }; \ No newline at end of file +export { apng }; \ No newline at end of file diff --git a/library/oh-package.json5 b/library/oh-package.json5 index 451d58b..8b1c344 100644 --- a/library/oh-package.json5 +++ b/library/oh-package.json5 @@ -10,7 +10,7 @@ ], "repository": "https://gitee.com/openharmony-sig/ohos_apng", "name": "@ohos/apng", - "version": "1.0.0-rc.1", + "version": "1.0.0-rc.2", "description": "为微博等厂商打造的一款适配OH的APNG格式动画三方应用,调用后OH就可支持APNG格式图片的编解码", "main": "index.ets", "author": "ohos_tpc", diff --git a/library/src/main/ets/components/GlobalContext.ets b/library/src/main/ets/components/GlobalContext.ets deleted file mode 100644 index b4715b3..0000000 --- a/library/src/main/ets/components/GlobalContext.ets +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (C) 2024 Huawei Device Co., Ltd. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -export class GlobalContext { - private static instance: GlobalContext - private _objects = new Map() - - private constructor() { - } - - public static getContext(): GlobalContext { - if (!GlobalContext.instance) { - GlobalContext.instance = new GlobalContext() - } - return GlobalContext.instance - } - - getObject(value: string): Object | undefined { - return this._objects.get(value) - } - - setObject(key: string, objectClass: Object): void { - this._objects.set(key, objectClass as Object) - } -} \ No newline at end of file diff --git a/library/src/main/ets/components/apng.ets b/library/src/main/ets/components/apng.ets index 3263b64..b3ef684 100644 --- a/library/src/main/ets/components/apng.ets +++ b/library/src/main/ets/components/apng.ets @@ -149,21 +149,26 @@ export struct apng { if (this.src instanceof Uint8Array) { apng = apngManager.getAPNGInstance(this.src as Uint8Array); } else if (typeof this.src === 'string') { - APngManager.loadData(this.src as string, (data) => { - console.log('arrayBuffer length: ' + data.byteLength); - if (data) { - apng = apngManager.getAPNGInstance(new Uint8Array(data)) as APNG; - if (!apng) { - this.timeTaken = errText; + if ((this.src as string).startsWith('http')) { + APngManager.loadData(this.src as string, (data) => { + console.log('arrayBuffer length: ' + data.byteLength); + if (data) { + apng = apngManager.getAPNGInstance(new Uint8Array(data)) as APNG; + if (!apng) { + this.timeTaken = errText; + } + this.getImageArr(apng); + } else { + console.log(">>> data is empty or http request failed"); } - this.getImageArr(apng); - } else { - console.log(">>> data is empty or http request failed"); - } - }, (err) => { - this.timeTaken = errText; - console.log(">>>> error message : " + err); - }) + }, (err) => { + this.timeTaken = errText; + console.log(">>>> error message : " + err); + }) + } else { + apng = await apngManager.getAPNGInstanceFormSandbox(this.src as string); + this.getImageArr(apng); + } } else if ((this.src as Resource).id) { let buffer = await apngManager.getResource(this.src as Resource); apng = apngManager.getAPNGInstance(buffer) as APNG; diff --git a/library/src/main/ets/components/manager.ets b/library/src/main/ets/components/manager.ets index 003cf68..b0a0a3a 100644 --- a/library/src/main/ets/components/manager.ets +++ b/library/src/main/ets/components/manager.ets @@ -21,17 +21,14 @@ */ import fs from '@ohos.file.fs'; -import { GlobalContext } from './GlobalContext'; -import parseAPNG from "./parser"; -import { APNG } from "./structs"; +import parseAPNG from './parser'; +import { APNG } from './structs'; import http from '@ohos.net.http'; +import { buffer } from '@kit.ArkTS'; +import { BusinessError } from '@kit.BasicServicesKit'; type File = fs.File; -function getContext(): Context { - return GlobalContext.getContext().getObject('MainContext') as Context; -} - class RequestData { receiveSize: number = 2000 totalSize: number = 2000 @@ -64,6 +61,27 @@ class APngManager { return file; } + async getAPNGInstanceFormSandbox(filePath: string): Promise { + let apng = {} as APNG; + try { + let file = fs.openSync(filePath, fs.OpenMode.READ_ONLY); + let stat = fs.lstatSync(filePath); + let arrayBuffer = new ArrayBuffer(stat.size); + await fs.read(file.fd, arrayBuffer).then((readLen: number) => { + console.log('read file data succeed'); + let buf = buffer.from(arrayBuffer, 0, readLen); + }).catch((err: BusinessError) => { + console.error('read file data failed whit error message :' + err.message + ', error code: ' + err.code); + }).finally(() => { + fs.closeSync(file); + }) + return this.getAPNGInstance(new Uint8Array(arrayBuffer)); + } catch (err) { + console.log('get Sandbox apng err: ' + err); + return apng; + } + } + static loadData(src: string, onComplete: (img: ArrayBuffer) => void, onError: (err: string) => void) { try { let httpRequest = http.createHttp(); diff --git a/oh-package.json5 b/oh-package.json5 index e2dd0c7..d3a8041 100644 --- a/oh-package.json5 +++ b/oh-package.json5 @@ -1,6 +1,6 @@ { "name": "apng", - "version": "1.0.0-rc.1", + "version": "1.0.0-rc.2", "description": "Please describe the basic information.", "main": "", "author": "", -- Gitee