今日看点

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

相关阅读

沉浸式恐怖!《破墓》体验展6月限定开幕:还原跳大神&太平间等场景,周边商品是棺材钥匙链、经文雨伞

...

2024-05-13

乾嘉三大家:文学巨匠袁枚、蒋士铨、赵翼的辉煌时代

在中国文学史上,“乾嘉三大家”是一个响当当的名字。他们是清朝乾隆至嘉庆年间的三位文学巨匠,分别是袁枚、蒋士铨和赵翼。这三位文学家的作品,无论是在诗歌、散文还是戏曲方面,都有着极高的艺术成就,对后世产生...

2024-05-13

SEVENTEEN的Hiphop小分队〈LALALI〉MV深夜满满福利!珉奎脱光泡澡、S.COUPS包紧紧待汗蒸

...

2024-05-13

康熙帝的辉煌统治:缔造盛世的长久君王

在中国历史上,清朝的康熙皇帝是一个杰出的统治者,他的统治不仅时间长达数十年之久,更在政治、经济、文化等多个领域创造了辉煌成就。本文将探讨康熙皇帝作为君主的执政时长以及他如何带领大清帝国进入一个被后世称...

2024-05-13

甜瓜:和奥巴马打球不能对他犯规 他的NBA模版是卡梅隆-佩恩

5月12日讯 最近,NBA著名球员卡梅隆·安东尼在7PM in Brooklyn》在节目中谈到了奥巴马。安东尼说:“他以前在白宫举行过球赛。我和勒布朗都去了,奥巴马也打了那场比赛,但我们不能对他犯规(...

2024-05-13

欧文:华盛顿让我们很安心 打出团队性的时候我们是很难被击败的

5月12日讯 在今天结束的NBA季后赛中,小牛队在主场以105-101击败雷霆队,在系列赛中以2-1领先。赛后,欧文在场边接受了媒体的采访。Q:欧文,你如何评价自己和队友在比赛后期的执行力?A:我想说...

2024-05-13

王猛大赞独行侠:除了角色球员硬 本不擅长防守的东欧在拼命地防

5月12日讯 独行侠主场105-101击败雷霆队,今天是NBA西部季后赛半决赛G3。评论员王猛评论了这场比赛:现在独行侠太硬了!除了角色球员的硬,一定是不够的。很少有领导人不擅长防守。欧文和卢卡拼命防...

2024-05-13

真来自宇宙文班:我跟孩子们聊了暗物质 那是一种无形的力量

05月12日讯 今天,文班亚马从马刺总经理布莱恩·莱特手中获得了圣安东尼奥Scobee天文馆最佳新秀奖杯。许多当地男孩和女孩俱乐部的孩子和附近一所小学的学生来到了现场。 当被问及与现场的孩子们交谈时,...

2024-05-13

身体哪里疼?东契奇:哪儿哪儿都很疼 我只是在场上拼

5月12日讯 东契奇赛后接受媒体采访,NBA季后赛第二轮G3,独行侠主场105-101击败雷霆,大比分2-1领先。赛后新闻发布会上,当被问及身体哪里疼时,东契奇说:“到处都疼,我只是在场上拼。”东契奇...

2024-05-13

一人扛着球队走!亚历山大半场10投6中 独砍17分3板4助1断2帽

5月12日讯 西部半决赛G3,NBA季后赛,雷霆半场52-51暂时领先独行侠。截至半场,雷霆球星亚历山大19分钟,10投6中(3分1中1),4罚4中,17分3篮板4助攻1抢断2盖帽。...

2024-05-13