今日看点

【软通动力】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

相关阅读

《商君书农战》

需要在战国那个大环境下来进行讨论,农业社会,战火连绵,生存是最基本的需求,所以才有了《农战》一篇,确定治国之要。“圣人知治国之要,故令民归心于农”,一句概括了全篇的精髓。在当时的大环境下,如何发展农耕...

2025-06-24

民生为甚欲墨子名言赏析

出自《尚贤中》:今王公大人,欲王天下,正诸侯,夫无德义,将何以哉?其说将必挟震威强,今王公大人,将焉取挟震威强哉?倾者民之死也!民生为甚欲,死为甚憎。所欲不得,而所憎屡至。自古及今,未尝有以此(指民所...

2025-06-24

《商君书赏刑》

《赏刑》篇应该算作商君执政、变法的核心指导思想。统一赏赐、统一刑罚、统一教化,能将这三点做到极致的,便能治理好国家成为圣人一样的明君。统一赏赐,就是将法律规定的赏赐作为唯一的赏赐标准,而不进行任何额外...

2025-06-24

明小不明大墨子名言赏析

出自《尚贤下》而今天下之士君子,居处言语皆尚贤,逮至其临众发政而治民,莫知尚贤而使能,我以此知天下之士君子,明于小而不明于大也。何以知其然乎?今王公大人,有一牛羊之财不能杀,必索良宰。有一衣裳之财不能...

2025-06-24

江河水非一源墨子名言赏析

出自《亲士》:江河之水,非一源之水也;千镒之裘,非一狐之白也。夫恶有同方不取,而取同己者乎?盖非兼王之道也。是故天地不昭昭,大水不潦潦,大火不燎燎,王德不尧尧者,乃千人之长也。其直如矢,其平如砥,不足...

2025-06-24

太盛难守墨子名言赏析

出自《亲士》:今有五锥,此其铦,铦者必先挫。有五刀,此其错,错者必先靡。是以甘井近竭,招木近伐,灵龟近灼,神蛇近暴。是故比干之殪,其抗也。孟贲之杀,其勇也。西施之沈,其美也。吴起之裂,其事也。故彼人者...

2025-06-24

贾探春的人物介绍

贾探春,曹雪芹所著《红楼梦》中人物,金陵十二钗之一,荣国府贾政与奴婢出身的妾室赵姨娘所生的女儿,贾宝玉同父异母的妹妹,贾府通称三姑娘,宝玉及三春均在贾母身边抚养长大,所受教育极好,与嫡姐元春无异,林黛...

2025-06-24

贾探春的生平经历

贾探春虽受良好教育,因系奴婢所生,又因赵姨娘生了个儿子,所以得不到太太重用,继而不认没有背景的亲娘及其亲戚,维护太太的利益,得到太太认可。虽在凤姐病假期间暂执权杖发动大观园改革,但权力有限,为时已晚,...

2025-06-24

贾元春的人物评价

贾元春。首先,这个名字的由来。贾元春生于正月初一而起名元春,是应乎时的一种体现,后面生的姑娘都叠这个春字。迎春、探春、惜春。好比现在的人,生在十月一日国庆节,就取名国庆。后来家族子弟中的男丁就叠这个国...

2025-06-24

《墨子·非攻》原文

《墨子·非攻》原文:[上]今有一人,入人园圃,窃其桃李,众闻则非之,上为政者得则罚之。此何也?以亏人自利也。至攘人犬豕鸡豚者,其不义又甚入人园圃窃桃李。是何故也?以亏人愈多,其不仁滋甚,罪益厚。至入人...

2025-06-24