今日看点

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

《庄子·杂篇·让王》简介

“让王”,意思是禅让王位。本篇文章的主旨在于阐述重生,提倡不因外物妨碍生命的思想。利禄不可取,王位可以让,全在于看重生命,保全生命。“轻物重生”的观点历来多有指斥,认为与庄子思想不合,但其间亦有相通之...

2025-04-28

世宗贤妃墓简介

世宗贤妃墓,又称“世宗贤妃坟”、"世宗六妃、二太子墓"或“四妃、二太子墓”。是明十三陵的7座妃嫔墓之一。内葬有郑贤妃等至少四位妃子、二位太子。墓园简介位于神宗四妃墓及悼陵之间,俗称"小宫"。坟园坐北朝...

2025-04-28

《庄子·杂篇·徐无鬼》简介

“徐无鬼”是开篇的人名,以人名作为篇名。全篇大体可分为十四个部分。第一部分至“莫以真人之言謦吾君之侧乎”,写徐无鬼拜见魏武侯,用相马之术引发魏武侯的喜悦,借此讥讽诗、书、礼、乐的无用。第二部分至“君将...

2025-04-28

《庄子·杂篇·庚桑楚》简介

《庄子·杂篇·庚桑楚》:“庚桑楚”是首句里的一个人名,这里以人名为篇名。全篇涉及许多方面的内容,有讨论顺应自然倡导无为的,有讨论认知的困难和是非难以认定的,但多数段落还是在讨论养生。全文大体可以分为五...

2025-04-28

庄子《知北游》译文赏析

知向北游历来到玄水岸边,登上名叫隐弅的山丘,正巧在那里遇上了无为谓。知对无为谓说:“我想向你请教一些问题:怎样思索、怎样考虑才能懂得道?怎样居处、怎样行事才符合于道?依从什么、采用什么方法才能获得道?...

2025-04-28

《庄子·杂篇·庚桑楚》原文

老聃之役有庚桑楚者,偏得老聃之道,以北居畏垒之山。其臣之画然知者去之,其妾之挈然仁者远之。拥肿之与居,鞅掌之为使。居三年,畏垒大壤。畏垒之民相与言曰:“庚桑子之始来,吾洒然异之。今吾日计之而不足,岁计...

2025-04-28

《庄子·外篇·知北游》简介

《庄子》是战国时期著名思想家庄周的毕生精华之作,《知北游》是《庄子·外篇》中的最后一篇,也是具有重要地位的一篇,对于了解《庄子》的哲学思想体系也较为重要。本篇是“外篇”的最后一篇,以篇首的三个字作为篇...

2025-04-28

《庄子·外篇·知北游》原文

知北游于玄水之上,登隐弅之丘,而适遭无为谓焉。知谓无为谓曰:“予欲有问乎若:何思何虑则知道?何处何服则安道?何从何道则得道?”三问而无为谓不答也。非不答,不知答也。知不得问,反于白水之南,登狐阕之上,...

2025-04-28

《庄子·外篇·田子方》简介

田子方是篇首的人名。全篇内容比较杂,具有随笔、杂记的特点,不过从一些重要章节看,主要还是表现虚怀无为、随应自然、不受外物束缚的思想。全文自然分成长短不一、各不相连的十一个部分,第一部分至“夫魏真为我累...

2025-04-28