【鸿蒙ArkUI实战】基于ImageKit对图片进行处理

104 篇文章 1 订阅
66 篇文章 0 订阅

往期知识点整理

步骤一:获取图片。

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

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);

总是有很多小伙伴反馈说:鸿蒙开发不知道学习哪些技术?不知道需要重点掌握哪些鸿蒙开发知识点? 为了解决大家这些学习烦恼。在这准备了一份很实用的鸿蒙全栈开发学习路线与学习文档给大家用来跟着学习。

针对一些列因素,整理了一套纯血版鸿蒙(HarmonyOS Next)全栈开发技术的学习路线,包含了鸿蒙开发必掌握的核心知识要点,内容有(OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、OpenHarmony驱动开发、系统定制移植……等)技术知识点。

《鸿蒙 (Harmony OS)开发学习手册》(共计892页):https://gitcode.com/HarmonyOS_MN/733GH/overview

如何快速入门?

1.基本概念
2.构建第一个ArkTS应用
3.……

开发基础知识:

1.应用基础知识
2.配置文件
3.应用数据管理
4.应用安全管理
5.应用隐私保护
6.三方应用调用管控机制
7.资源分类与访问
8.学习ArkTS语言
9.……

在这里插入图片描述

基于ArkTS 开发

1.Ability开发
2.UI开发
3.公共事件与通知
4.窗口管理
5.媒体
6.安全
7.网络与链接
8.电话服务
9.数据管理
10.后台任务(Background Task)管理
11.设备管理
12.设备使用信息统计
13.DFX
14.国际化开发
15.折叠屏系列
16.……

在这里插入图片描述

鸿蒙开发面试真题(含参考答案):https://gitcode.com/HarmonyOS_MN/733GH/overview

在这里插入图片描述

OpenHarmony 开发环境搭建

图片

《OpenHarmony源码解析》:https://gitcode.com/HarmonyOS_MN/733GH/overview

  • 搭建开发环境
  • Windows 开发环境的搭建
  • Ubuntu 开发环境搭建
  • Linux 与 Windows 之间的文件共享
  • ……
  • 系统架构分析
  • 构建子系统
  • 启动流程
  • 子系统
  • 分布式任务调度子系统
  • 分布式通信子系统
  • 驱动子系统
  • ……

图片

OpenHarmony 设备开发学习手册:https://gitcode.com/HarmonyOS_MN/733GH/overview

图片

  • 14
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值