基于 OpenHarmony compress 三方件使用指南~

2024-05-07 19:12

本文主要是介绍基于 OpenHarmony compress 三方件使用指南~,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

关于

提供了一个轻量级的图像压缩库。将允许您将大照片压缩成小
尺寸的照片,图像质量损失或可以忽略不计

compress 的依赖添加

为你的应用添加 compress-debug.har。将 compress-debug.har 复制到 entry\libs 目录下即可(由于 build.gradle 中已经依赖的 libs 目录下的*.har,因此不需要在做修改)。

使用

(1)ability_main.xml 设置界面布局

<DependentLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:width="match_parent"
ohos:height="match_parent"
ohos:background_element="#FFFFFF">
<Image
ohos:id="$+id:image1"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:image_src="$media:dog1.PNG"/>
<Text
ohos:id="$+id:text"ohos:width="match_content"
ohos:height="match_content"
ohos:text=""
ohos:text_size="19fp"
ohos:text_color="#1C1C1C"
ohos:top_padding="8vp"
ohos:bottom_padding="8vp"
ohos:right_padding="70vp"
ohos:left_padding="70vp"
ohos:center_in_parent="true"
ohos:align_parent_bottom="true"
ohos:bottom_margin="120vp"/>
<Button
ohos:id="$+id:choose_button"
ohos:width="match_content"
ohos:height="match_content"
ohos:text="Choose Image"
ohos:text_size="19fp"
ohos:text_color="#FFFFFF"
ohos:top_padding="8vp"
ohos:bottom_padding="8vp"
ohos:right_padding="70vp"
ohos:left_padding="70vp"
ohos:background_element="$graphic:background_button"
ohos:center_in_parent="true"
ohos:align_parent_bottom="true"
ohos:bottom_margin="75vp"/>
<Button
ohos:id="$+id:button"
ohos:width="match_content"
ohos:height="match_content"
ohos:text="Compress"
ohos:text_size="19fp"
ohos:text_color="#FFFFFF"
ohos:top_padding="8vp"
ohos:bottom_padding="8vp"
ohos:right_padding="70vp"
ohos:left_padding="70vp"
ohos:background_element="$graphic:background_button"
ohos:center_in_parent="true"
ohos:align_parent_bottom="true"
ohos:bottom_margin="15vp"/>
</DependentLayout>

(2)MainAbilitySlice

获取界面的按钮,并分别向按钮绑定点击事件

public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
// 请求文件的读取权限
String[] permissions = {"ohos.permission.READ_USER_STORAGE"};
requestPermissionsFromUser(permissions, 0);
// 获取压缩按钮并绑定事件
Button button = (Button) findComponentById(ResourceTable.Id_button);
if (button != null) {
// 为按钮设置点击回调
button.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
try {
File file = new File(System.getProperty("java.io.tmpdir") + File.separator + tmpName);
HiLog.error(LOG_LABEL, "old size..." + file.length() + " ...b");
// 默认压缩
// File newFile = Compressor.defaultCompress(file);
// 自定义压缩
File newFile = Compressor.customCompress(getContext(), file, 500, 1000, 60);
Text text = (Text) findComponentById(ResourceTable.Id_text);
text.setText("size: " + newFile.length() + " b");
HiLog.error(LOG_LABEL, "new size..." + newFile.length() + " ...b");
PixelMap newPixelMap = Compressor.decode(newFile);
Image image = (Image) findComponentById(ResourceTable.Id_image1);
image.setPixelMap(newPixelMap);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
// 获取选择图片按钮并绑定事件
Button chooseButton = (Button) findComponentById(ResourceTable.Id_choose_button);
if (chooseButton != null) {
// 为按钮设置点击回调
chooseButton.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
DataAbilityHelper helper = DataAbilityHelper.creator(getContext());
try {
ResultSet resultSet = helper.query(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI,
null, null);
while (resultSet != null && resultSet.goToNextRow()) {
// 互殴媒体库的图片
int id =
resultSet.getInt(resultSet.getColumnIndexForName(AVStorage.Images.Media.ID));
HiLog.error(LOG_LABEL, "id:..." + id + " ...");
Uri uri =
Uri.appendEncodedPathToUri(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI, "" + id);
// 根据图片的 uri 打开文件并保存到临时目录中
FileDescriptor fileDescriptor = helper.openFile(uri, "r");
ImageSource.DecodingOptions decodingOpts = new ImageSource.DecodingOptions();
decodingOpts.sampleSize = ImageSource.DecodingOptions.DEFAULT_SAMPLE_SIZE;
ImageSource imageSource = ImageSource.create(fileDescriptor, null);
PixelMap pixelMap = imageSource.createThumbnailPixelmap(decodingOpts, true);
ImagePacker imagePacker = ImagePacker.create();
tmpName = UUID.randomUUID().toString();
File file = new File(System.getProperty("java.io.tmpdir") + File.separator +
tmpName);
FileOutputStream outputStream = new FileOutputStream(file);
ImagePacker.PackingOptions packingOptions = new ImagePacker.PackingOptions();
packingOptions.quality = 100;
boolean result = imagePacker.initializePacking(outputStream, packingOptions);
result = imagePacker.addImage(pixelMap);
long dataSize = imagePacker.finalizePacking();
// 显示图片和图片大小
Text text = (Text) findComponentById(ResourceTable.Id_text);
text.setText("size: " + file.length() + " b");
Image image = (Image) findComponentById(ResourceTable.Id_image1);
image.setPixelMap(pixelMap);
}
} catch (DataAbilityRemoteException | FileNotFoundException e) {
e.printStackTrace();
}
}
});
}
}

(3)结果展示

(4)Compressor 核心方法

defaultCompress(Context context, File file)

输入:用户需要处理的文件

输出:处理后的临时文件

处理流程:传入图片后,先将图片拷贝到临时目录,再按照默认的压缩处理方法,完成后返回处理后图片的临时目录

customCompress(Context context, File file, int width, int height, int quality)

输入:用户需要处理的文件,处理后的宽度、高度以及图片质量。

输出:处理后的临时文件

处理流程:传入图片后,先将图片拷贝到临时目录,再按照指定的处理方法进行

压缩,完成后返回处理后图片的临时目录

decode(File file)

输入:图片的目录

输出:图片的像素矩阵形式

作用:用于界面图片展示

为了能让大家更好的学习鸿蒙(HarmonyOS NEXT)开发技术,这边特意整理了《鸿蒙开发学习手册》(共计890页),希望对大家有所帮助:https://qr21.cn/FV7h05

《鸿蒙开发学习手册》:

如何快速入门:https://qr21.cn/FV7h05

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

开发基础知识:https://qr21.cn/FV7h05

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

基于ArkTS 开发:https://qr21.cn/FV7h05

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

鸿蒙开发面试真题(含参考答案):https://qr18.cn/F781PH

鸿蒙开发面试大盘集篇(共计319页):https://qr18.cn/F781PH

1.项目开发必备面试题
2.性能优化方向
3.架构方向
4.鸿蒙开发系统底层方向
5.鸿蒙音视频开发方向
6.鸿蒙车载开发方向
7.鸿蒙南向开发方向

这篇关于基于 OpenHarmony compress 三方件使用指南~的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/968142

相关文章

Python虚拟环境与Conda使用指南分享

《Python虚拟环境与Conda使用指南分享》:本文主要介绍Python虚拟环境与Conda使用指南,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、python 虚拟环境概述1.1 什么是虚拟环境1.2 为什么需要虚拟环境二、Python 内置的虚拟环境工具

Java JSQLParser解析SQL的使用指南

《JavaJSQLParser解析SQL的使用指南》JSQLParser是一个Java语言的SQL语句解析工具,可以将SQL语句解析成为Java类的层次结构,还支持改写SQL,下面我们就来看看它的具... 目录一、引言二、jsQLParser常见类2.1 Class Diagram2.2 Statement

正则表达式r前缀使用指南及如何避免常见错误

《正则表达式r前缀使用指南及如何避免常见错误》正则表达式是处理字符串的强大工具,但它常常伴随着转义字符的复杂性,本文将简洁地讲解r的作用、基本原理,以及如何在实际代码中避免常见错误,感兴趣的朋友一... 目录1. 字符串的双重翻译困境2. 为什么需要 r?3. 常见错误和正确用法4. Unicode 转换的

Python Selenium动态渲染页面和抓取的使用指南

《PythonSelenium动态渲染页面和抓取的使用指南》在Web数据采集领域,动态渲染页面已成为现代网站的主流形式,本文将从技术原理,环境配置,核心功能系统讲解Selenium在Python动态... 目录一、Selenium技术架构解析二、环境搭建与基础配置1. 组件安装2. 驱动配置3. 基础操作模

Spring Validation中9个数据校验工具使用指南

《SpringValidation中9个数据校验工具使用指南》SpringValidation作为Spring生态系统的重要组成部分,提供了一套强大而灵活的数据校验机制,本文给大家介绍了Spring... 目录1. Bean Validation基础注解常用注解示例在控制器中应用2. 自定义约束验证器定义自

Python Websockets库的使用指南

《PythonWebsockets库的使用指南》pythonwebsockets库是一个用于创建WebSocket服务器和客户端的Python库,它提供了一种简单的方式来实现实时通信,支持异步和同步... 目录一、WebSocket 简介二、python 的 websockets 库安装三、完整代码示例1.

Kotlin 作用域函数apply、let、run、with、also使用指南

《Kotlin作用域函数apply、let、run、with、also使用指南》在Kotlin开发中,作用域函数(ScopeFunctions)是一组能让代码更简洁、更函数式的高阶函数,本文将... 目录一、引言:为什么需要作用域函数?二、作用域函China编程数详解1. apply:对象配置的 “流式构建器”最

2025最新版Python3.13.1安装使用指南(超详细)

《2025最新版Python3.13.1安装使用指南(超详细)》Python编程语言自诞生以来,已经成为全球最受欢迎的编程语言之一,它简单易学易用,以标准库和功能强大且广泛外挂的扩展库,为用户提供包罗... 目录2025最新版python 3.13.1安装使用指南1. 2025年Python语言最新排名2.

Deepseek使用指南与提问优化策略方式

《Deepseek使用指南与提问优化策略方式》本文介绍了DeepSeek语义搜索引擎的核心功能、集成方法及优化提问策略,通过自然语言处理和机器学习提供精准搜索结果,适用于智能客服、知识库检索等领域... 目录序言1. DeepSeek 概述2. DeepSeek 的集成与使用2.1 DeepSeek API

Rsnapshot怎么用? 基于Rsync的强大Linux备份工具使用指南

《Rsnapshot怎么用?基于Rsync的强大Linux备份工具使用指南》Rsnapshot不仅可以备份本地文件,还能通过SSH备份远程文件,接下来详细介绍如何安装、配置和使用Rsnaps... Rsnapshot 是一款开源的文件系统快照工具。它结合了 Rsync 和 SSH 的能力,可以帮助你在 li