鸿蒙5.0实战案例:基于ImageKit对图片进行处理

news/2025/2/23 8:04:21

往期推文全新看点(文中附带全新鸿蒙5.0全栈学习笔录)

✏️ 鸿蒙(HarmonyOS)北向开发知识点记录~

✏️ 鸿蒙(OpenHarmony)南向开发保姆级知识点汇总~

✏️ 鸿蒙应用开发与鸿蒙系统开发哪个更有前景?

✏️ 嵌入式开发适不适合做鸿蒙南向开发?看完这篇你就了解了~

✏️ 对于大前端开发来说,转鸿蒙开发究竟是福还是祸?

✏️ 鸿蒙岗位需求突增!移动端、PC端、IoT到底该怎么选?

✏️ 记录一场鸿蒙开发岗位面试经历~

✏️ 持续更新中……


步骤一:获取图片。

方法一:通过沙箱路径获取:

const context : Context = getContext(this);

const filePath : string = context.cacheDir + '/test.jpg';

方法二:通过沙箱路径获取图片的文件描述符:

const context = getContext(this);

const filePath = context.cacheDir + '/test.jpg';

const file : fs.File = fs.openSync(filePath, fs.OpenMode.READ_WRITE);

const fd : number = file?.fd;

方法三:通过资源管理器获取资源文件的ArrayBuffer:

const context : Context = getContext(this);

// 获取resourceManager资源管理器

const resourceMgr : resourceManager.ResourceManager = context.resourceManager;

resourceMgr.getRawFileContent('test.jpg').then((fileData : Uint8Array) => {

console.log("Succeeded in getting RawFileContent")

// 获取图片的ArrayBuffer

const buffer = fileData.buffer.slice(0);

}).catch((err : BusinessError) => {

console.error("Failed to get RawFileContent")

});

方法四:通过资源管理器获取资源文件的RawFileDescriptor:

const context : Context = getContext(this);

// 获取resourceManager资源管理器

const resourceMgr : resourceManager.ResourceManager = context.resourceManager;

resourceMgr.getRawFd('test.jpg').then((rawFileDescriptor : resourceManager.RawFileDescriptor) => {

console.log("Succeeded in getting resourceManager")

}).catch((err : BusinessError) => {

console.error("Failed to get resourceManager")

});

步骤二:创建imageSource。

方法一:通过沙箱路径创建ImageSource。沙箱路径可以通过步骤2的方法一获取。

// path为已获得的沙箱路径

const imageSource : image.ImageSource = image.createImageSource(filePath);

方法二:通过文件描述符fd创建ImageSource。文件描述符可以通过步骤2的方法二获取。

// fd为已获得的文件描述符

const imageSource : image.ImageSource = image.createImageSource(fd);

方法三:通过缓冲区数组创建ImageSource。缓冲区数组可以通过步骤2的方法三获取。

const imageSource : image.ImageSource = image.createImageSource(buffer);

方法四:通过资源文件的RawFileDescriptor创建ImageSource。RawFileDescriptor可以通过步骤2的方案四获取。

const imageSource : image.ImageSource = image.createImageSource(rawFileDescriptor);

步骤三:设置解码参数DecodingOptions,解码获取PixelMap图片对象。

let decodingOptions : image.DecodingOptions = {

editable: true,

desiredPixelFormat: 3,

}

// 创建pixelMap并进行简单的旋转和缩放

imageSource.createPixelMap(decodingOptions).then((pixelMap : image.PixelMap) => {

// 顺时针旋转90°

pixelMap.rotate(90);



// 宽为原来的0.5

// 高为原来的0.5

pixelMap.scale(0.5, 0.5);

console.log("Succeeded in creating PixelMap")

}).catch((err : BusinessError) => {

console.error("Failed to create PixelMap")

});

步骤四:图形处理。

场景一:在Ts侧:

创建图像编码ImagePacker对象。

const imagePackerApi = image.createImagePacker();

设置编码输出流和编码参数。

format为图像的编码格式;quality为图像质量,范围从0-100,100为最佳质量。

let packOpts : image.PackingOption = { format:"image/jpeg", quality:98 };

进行图片编码,并保存编码后的图片:

方法一: 通过pixelMap编码。

import {BusinessError} from '@ohos.base'

import fs from '@ohos.file.fs'

const context : Context = getContext(this);

const path : string = context.cacheDir + "/pixel_map.jpg";

let file = fs.openSync(path, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);

imagePackerApi.packToFile(pixelMap, file.fd, packOpts).then(() => {

// 直接打包进文件

}).catch((error : BusinessError) => {

console.error('Failed to pack the image. And the error is: ' + error);

})

方法二:通过imageSource编码。

import {BusinessError} from '@ohos.base'

import fs from '@ohos.file.fs'

const context : Context = getContext(this);

const filePath : string = context.cacheDir + "/image_source.jpg";

let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);

imagePackerApi.packToFile(imageSource, file.fd, packOpts).then(() => {

// 直接打包进文件

}).catch((error : BusinessError) => {

console.error('Failed to pack the image. And the error is: ' + error);

})

场景二:在Native侧:

在ts侧传入pixelmap和文件fd到native侧。

const path = getContext(this).filesDir + "imagenative.jpeg";

let file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);

let fd =file.fd

testNapi.add1(this.pixelMap,fd)

创建编码器实例对象。

napi_value packer;

int32_t result = OH_ImagePacker_Create(env, &packer);

ImagePacker_Native* nativePacker = OH_ImagePacker_InitNative(env, packer);

设置编码参数。

struct ImagePacker_Opts_ opts;

// 配置编码格式(必须)

opts.format = "image/jpeg";

// 配置编码质量(必须)

opts.quality = 98;

进行编码。

int32_t result = OH_ImagePacker_PackToFile(nativePacker, args[0], &opts, fd);

http://www.niftyadmin.cn/n/5863188.html

相关文章

安全面试3

文章目录 一个单位的一级域名可能不止一个,怎么收集某个单位的所有域名,注意不是子域名用转义字符防御时,如果遇到数据库的列名或是表名本身就带着特殊字符,应该怎么做宽字节注入原理防御宽字节注入的方法 基于黑白名单的修复&…

aws(学习笔记第二十九课) aws cloudfront hands on

aws(学习笔记第二十九课) 使用aws cloudfront 学习内容: 什么是aws cloudfront练习使用aws cloudfront 1. 什么是aws cloudfront aws cloudfront的整体架构 这里可以看出,aws引入了edge location的概念,用户的client与edge location进行是…

centos7的yum出错

当执行yum时报如下错误 已加载插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile Could not retrieve mirrorlist http://mirrorlist.centos.org/?release7&archx86_64&repoos&infrastock error was 14: curl#6 - "Cou…

Go、Java、Python、C/C++、PHP、Rust 语言全方位对比分析

简介 在当今多元化的编程世界中,不同的编程语言各有千秋,适用于不同的应用场景。本文聚焦于 Go、Java、Python、C/C、PHP 和 Rust 这六种热门编程语言,深入剖析它们的优劣势以及各自的最佳适用场景,旨在为开发者在选择合适的编程…

Python基于Django的酒店推荐系统【附源码】

博主介绍:✌Java老徐、7年大厂程序员经历。全网粉丝12w、csdn博客专家、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌ 🍅文末获取源码联系🍅 👇🏻 精彩专栏推荐订阅👇&…

Java计算机毕业设计基于SSM宠物美容信息管理系统数据库源代码+LW文档+开题报告+答辩稿+部署教程+代码讲解

源代码数据库LW文档(1万字以上)开题报告答辩稿 部署教程代码讲解代码时间修改教程 一、开发工具、运行环境、开发技术 开发工具 1、操作系统:Window操作系统 2、开发工具:IntelliJ IDEA或者Eclipse 3、数据库存储&#xff1a…

[Android]DialogLifeCycle禁止点击背景关闭弹窗

在 Kotlin 中,可以通过设置 Dialog 的 setCanceledOnTouchOutside 方法来避免点击弹窗之外的背景而关闭弹窗。您可以在 DialogLifeCycle 的 show 方法中设置这一属性。 fun Context.dialogCustom(layout: Int, style: Int R.style.dialogCenter, onInit: DialogMo…

【多模态处理篇八】【DeepSeek增强现实:AR导航实时推理优化】

一、AR导航的「地狱级难度」挑战 搞AR导航就像在钢丝上跳街舞——既要实时感知环境,又得丝滑渲染虚拟指引,还得防着手机发烫罢工。具体难点包括: 1. 算力饥渴症 手机端要同时处理: 摄像头画面(30fps+)SLAM定位(每秒百次坐标计算)3D箭头渲染(每帧万级三角形) 这相当…