今日看点

【软通动力】HarmonyOS三方件开发指南(7)

发表于话题:HarmonyOS 2
发布时间:2021-06-02

1. 组件compress功能介绍
1.1.  组件介绍:
        compress是一个轻量级图像压缩库。compress允许将大照片压缩成小尺寸的照片,图像质量损失非常小或可以忽略不计。

1.2.  手机模拟器上运行效果:
   

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

2.2.  设置布局

2.3.  图像压缩
核心类:Compressor

核心方法:

(1)自定义压缩:

public static File customCompress(Context context, File file, int width, int height, int ) throws IOException

参数:

context - 应用程序上下文

file - 待压缩图片抽象路径名

width - 压缩后宽度

height - 压缩后高度

- 图片压缩质量,范围0~100

结果:

返回压缩后图片抽象路径名。

异常:

发生I/O异常

(2)默认压缩:

public static File defaultCompress(Context context, File file) throws IOException

参数:

context - 应用程序上下文

file - 待压缩图片抽象路径名

结果:

返回压缩后图片抽象路径名。

异常:

发生I/O异常

简单示例:

运行示例前需要在模拟器保存一张截图或使用相机功能照一张照片

public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); // 请求文件的读取权限 String[] permissions = {"ohos.permission.READ_USER_STORAGE"}; reser(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. = 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. 组件compress开发实现
3.1.  拷贝图片制临时目录
传入的图片路径拷贝临时文件到应用的临时目录。

private static File copyToCache(Context context, File imageFile) throws IOException { PixelMap pixelMap = decode(imageFile); String cachePath = context.getCacheDir() + File.separator + imageFile.getName(); File cacheFile = new File(cachePath); int = 100; // 压缩质量 refreshTmpFile(pixelMap, cacheFile, ); return cacheFile; }

 3.2.  图片解码
对临时目录里的图片进行解码

private static PixelMap decode(File file, int width, int height) { ImageSource imageSource = ImageSource.create(file, null); mageSource.DecodingOptions decodingOpts = new ImageSource.DecodingOptions(); decodingOpts.desiredSize = new Size(width, height); return imageSource.createPixelmap(decodingOpts); }

 3.3.  图片编码
按照开发人员设定的规则进行编码,生成新图片

private static void refreshTmpFile(PixelMap pixelMap, File file, int ) throws IOException { ImagePacker imagePacker = ImagePacker.create(); ImagePacker.PackingOptions options = new ImagePacker.PackingOptions(); options. = ; imagePacker.initializePacking(new FileOutputStream(file), options); imagePacker.addImage(pixelMap); imagePacker.finalizePacking(); }

 

项目源代码地址:https://github.com/isoftstone-dev/Compressor_Harmony

欢迎交流:

 

 

标签组:[context

本文来源:https://www.kandian5.com/articles/23980.html

相关阅读

孕期四项检查属过度产检,这些产检项目真没必要

怀孕做产检可以了解胎儿的健康情况,但是产检也是有一定的规定的。根据《孕产期保健技术指南(第2版)》妈妈在整个孕期做的产检次数是7~11次。但有一些妈妈就觉得这个产检没用,检查了反而是浪费钱,有些小问题...

2025-10-30

周公解梦:胎梦生男孩的20种常见场景,第7个最准!

准妈妈们看过来!这些梦境可能是小王子的神秘预告...一、胎梦的神秘力量:为什么准妈妈总做关于宝宝的梦?在中国传统文化中, 胎梦 被视为胎儿向母亲传递信息的重要方式。古人认为,孕妇在怀孕期间所做的梦往往...

2025-10-30

梦见摘果子、爬楼梯=怀男孩?胎梦生男孩的奇怪但准确的预兆

怀孕期间,准妈妈们常常会做一些生动而奇特的梦。其中, 梦见摘果子、爬楼梯等梦境 被民间广泛认为是怀男孩的征兆。这些看似普通的梦境真的能预测胎儿性别吗?本文将深入探讨胎梦与生男孩之间的神秘联系,分析那些...

2025-10-30

梦见太阳、月亮=怀男孩?胎梦生男孩的天体象征解读

从古至今,胎梦中的天体现象被赋予特殊含义。太阳与月亮作为最显著的天象,其出现在孕妇梦境中常被解读为性别预兆。本文将深入解析这一传统观念的科学依据与文化内涵。一、胎梦文化概述:天体象征的历史渊源胎梦,即...

2025-10-30

科学还是迷信?大数据分析胎梦生男孩的准确率到底有多高

在中国传统文化中, 胎梦 一直被视为预测胎儿性别和健康状况的重要方式。特别是关于梦见什么预示着生男孩的说法,更是流传甚广。老一辈常说"梦见龙、老虎、蛇等动物是生男孩的征兆",而现代人则对这些说法半信半...

2025-10-30

二胎妈妈经验谈:生男孩胎梦和一胎有何不同?对比太明显了

我是一位拥有两个可爱孩子的二胎妈妈,大宝是6岁的小公主,二宝是4岁的小王子。在两次怀孕过程中,我经历了各种神奇的胎梦,尤其是关于宝宝性别的梦境,让我印象深刻。通过对比一胎和二胎时生男孩的胎梦,我发现了...

2025-10-30

胎梦生男孩的科学解释?心理学家揭秘孕期梦境与胎儿性别的关联

在怀孕期间,许多准妈妈都会经历各种生动而奇特的梦境,其中关于胎儿性别的梦境尤为常见。有趣的是, 统计数据显示约有65%的孕妇报告做过与胎儿性别相关的梦,其中梦见生男孩的比例高达72% 。 这些梦境往往...

2025-10-30

孕期反复做同一个梦?可能是胎梦生男孩的强烈信号

怀孕期间,许多准妈妈都会经历 反复做同一个梦境 的奇妙体验。这些梦境往往生动鲜明,甚至会在一段时间内不断重复出现。民间自古就有" 胎梦预测胎儿性别 "的说法,特别是当准妈妈们反复梦见某些特定场景或符...

2025-10-30

孕早期生男孩的胎梦准吗?妇产科医生告诉你梦境预测的可靠性

在中国传统文化中, 胎梦 一直被视为预测胎儿性别和健康状况的重要方式。特别是孕早期,当准妈妈们开始经历各种生动梦境时,"梦见蛇、老虎、黑桃A等象征物"常被解读为怀男孩的征兆。但这种流传已久的民间说法,...

2025-10-30

胎梦生男孩VS生女孩:5大关键区别,帮你提前判断胎儿性别

自古以来,胎梦就被视为预测胎儿性别的重要线索。本文将深入解析胎梦中生男孩与生女孩的5大关键区别,帮助准父母们从梦境中寻找蛛丝马迹。一、胎梦文化背景与科学认知胎梦是指孕妇在怀孕期间所做的与胎儿相关的梦境...

2025-10-30