基于 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和相关库 首先,确保你的电脑已经安装了Python运行环境(建议安装Python3版本)。安装完成后,同样需要安装一些相关的库,如requests、beautifulsoup4等。在命令行窗口中输入以下命令进行安装: pip install requestspip install bea

JupyterLab使用指南(十): JupyterLab安全性与配置教程

文章目录 1. 生成详细的配置2. 安全策略2.1 使用 HTTPS 加密通信2.2 设置访问密码2.3 禁用 root 用户启动 3. 修改配置文件4. 将 JupyterLab 作为后台进程运行4.1 使用 `nohup`4.2 使用 `systemd` 1. 生成详细的配置 JupyterLab 的配置文件用于管理和定制 JupyterLab 的各种行为。可以通过以下命

Javassist使用指南1

1.创建了一个非默认的classpool,加入当前线程的上下文类加载器作为额外的类搜索路径 val classPool = ClassPool(false)classPool.appendClassPath(LoaderClassPath(contextClassLoader)) ClassPool ClassPool是CtClass对象的容器,每一个CtClass对象都必须从Class

HiC-Pro的Singularity简明使用指南

关于原理部分和更详细的介绍,见HiC-Pro: Hi-C数据预处理高效工具, 这里只介绍如何快速使用Singularity的HiC-Pro进行数据分析。 关键内容就是,config-hicpro.txt 里的文件路径信息都必须是绝对路径,否则默认都位于annotation目录下。切记,切记,切记。 第零步: Singularity的HiC-Pro镜像下载, # 下载mkdir -p /opt/

Guitar Pro 8.2中文版图文安装激活使用指南

吉他谱曲软件Guitar Pro 8中文版是Arobas Music公司历时5年的一个全新之作,作为专业的吉他软件,能够创建不同的音轨完成不同乐器乐谱的编排和制作,这次在最新版本中新增了音频轨道、效果器视图、音阶示意图和音频音符微调等功能,优化了乐谱的编辑流程,支持批量调整音量。小哥聊软件为您提供Guitar Pro 8破解版下载,附有详细的安装教程。 软件详情 Guitar Pro 8.2是

AI大眼萌探索 AI 新世界:Ollama 使用指南【1】

在人工智能的浪潮中,Ollama 的出现无疑为 Windows 用户带来了一场革命。这款工具平台以其开创性的功能,简化了 AI 模型的开发与应用,让每一位爱好者都能轻松驾驭 AI 的强大力量。大家好,我是AI大眼萌,今天我们将带大家了解这款工具平台。 🤖 什么是 Ollama? Ollama 不仅仅是一个 AI 和 ML (Machine Learning)工具平台,它是技术社区中的一股清流

Arxiv使用指南

https://www.jianshu.com/p/0c634da4634e?utm_source=oschina-app 如果你非常确定自己想要找什么​,​比如知道论文的名字(算法的名字)或者作者的名字,直接去Google Scholar上搜索是最快的。然而如果你并不是很确定自己想要什么,只是想要看看某个领域的最新发展,知道大家都在干什么,然而​却发现​Google Scholar给你的结果多

JupyterLab使用指南(八):更改JupterLab左侧默认打开目录

在JupyterLab中,默认打开路径通常是由其配置文件中的root_dir设置决定的。如果你没有特意设置这个配置项,JupyterLab可能会使用当前用户的主目录或者上一次关闭时的路径作为默认打开路径。 更改JupyterLab默认路径的操作在不同操作系统下大体相似,主要是通过配置文件来实现。下面是针对Windows、macOS和Linux三种常见操作系统的具体步骤: Windows 使

JupyterLab使用指南(七):JupyterLab使用 LaTeX 生成数学公式

在 JupyterLab 中,可以使用 LaTeX 语法生成复杂的数学公式。JupyterLab 内置对 LaTeX 的支持,使得我们可以方便地在 notebook 中编写和展示数学公式。以下是详细的步骤和示例。 1. 使用 LaTeX 生成数学公式 LaTeX 是一种专门用于排版数学公式的语言。JupyterLab 支持在 Markdown 单元格和代码单元格中使用 LaTeX。 1.1

超拟人合成接口使用指南(讯飞)

简介         超拟人合成接口是一种先进的文本转音频技术,通过利用大模型生成拟声词,使合成音频更加拟人化和真实。本文将对该接口的主要功能、请求和响应格式、常见错误码等进行总结归纳,帮助用户快速上手并正确使用该接口。 接口描述         超拟人合成接口支持将文本数据合成为音频,音频结果(audio)以多帧形式返回。由于结果帧的顺序可能无法保证,建议在接入方在一定时间片内根据服务响应