场景一:展示系统原生支持的图片格式编解码的代码实现
// 获取resourceManager资源管理
const context : Context = getContext(this);
const resourceMgr : resourceManager.ResourceManager = context.resourceManager;
// 创建ImageSource
resourceMgr.getRawFileContent('test.jpg').then((fileData : Uint8Array) => {
console.log("Succeeded in getting RawFileContent")
// 获取图片的ArrayBuffer
const buffer = fileData.buffer.slice(0);
const imageSource : image.ImageSource = image.createImageSource(buffer);
}).catch((err : BusinessError) => {
console.error("Failed to get RawFileContent")
});
// 创建PixelMap
imageSource.createPixelMap().then((pixelMap: image.PixelMap) => {
console.log("Succeeded in creating PixelMap")
}).catch((err : BusinessError) => {
console.error("Failed to creating PixelMap")
});
场景二:使用第三方库进行系统原生不支持的常用图片格式解码进行解码
avif格式
2.1 将libavif库添加到工程中,参考链接: libavif三方库说明
2.1.1 编译第三方库
下载本仓库
git clone https://gitee.com/openharmony-sig/tpc_c_cplusplus.git --depth=1
三方库目录结构
o tpc_c_cplusplus/thirdparty/libavif #三方库libavif的目录结构如下
o ├── docs #三方库相关文档的文件夹
o ├── HPKBUILD #构建脚本
o ├── HPKCHECK #自动化测试脚本
o ├── SHA512SUM #三方库校验文件
o ├── README.OpenSource #说明三方库源码的下载地址,版本,license等信息
o ├── README_zh.md #三方库说明文档
├── OAT.xml #开源扫描相关文件
- 在tpc_c_cplusplus/lycium目录下编译三方库
编译环境的搭建参考 准备三方库构建环境
cd tpc_c_cplusp