今日看点

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

贾姬在厕所与野猪相遇为什么没人敢救她

汉景帝妃子上厕所遭遇野猪,侍卫抗旨不救,太后为何赏大臣百金!。提起来有点荒诞,却是史上真实发生过的事件。上厕所与野猪遭遇的女子就是汉景帝的嫔妃贾姬,翻开史料:这个贾姫大有来头,她是中山靖王刘胜的生母,...

2025-09-15

清朝灭亡后那些深宫里的女人去哪了

1908年11月14日,在位长达34年但年仅38岁的光绪皇帝就英年早逝了,一天以后,统治中国近半个世纪的慈禧老佛爷也撒手人寰,半个月之后,年仅2岁多的溥仪继位,这就是末代皇帝宣统帝!溥仪登基后,尊光绪...

2025-09-15

秦宜禄的妻子杜氏为什么会被吕布和曹操霸占呢

自古以来,有人爱江山却更爱美人,也有人只要江山而舍弃美人的,然而毕竟英雄难过美人关。想当年吴三桂那貌美如花的妻子陈圆圆居然被李自成的手下刘宗敏给抢了去,他一气之下居然投降清军,把多尔衮给引进关中了。这...

2025-09-15

王献之很爱自己的妻子为何最后还是被逼离婚了

一直以来,很多人都认为,古代男子肯定都很想娶公主,毕竟当皇帝的女婿,一下可以少奋斗几十年,甚至都不用奋斗了。但实际并不是如此,古代男子几乎是不愿娶公主的,不仅唐朝如此,很多朝代都这样。而今天的主人公也...

2025-09-15

秀才为什么因为一只鸭子被推上刑场

老一辈的人常说,忍一时风平浪静,退一步海阔天空。这句话说的其实就是让人在遇到事情的时候要冷静,不要因为一时冲动而做出让人后悔莫及的事情来。在清代学者俞樾所著的《右台仙馆笔记》里,讲了这么一个故事。说是...

2025-09-15

伍子胥逃亡的过程中真的一夜愁白了头吗

公元前522年,楚国国君听信费无忌的谗言,将伍子胥的父亲伍奢杀害,派人去杀伍子胥。伍子胥得到消息后连忙逃亡吴国。伍子胥的逃亡路线,从楚国到吴国必须要经过韶关,韶关位于吴楚交界之地,向来有重兵把守,而此...

2025-09-15

古代皇帝吃剩下的饭菜怎么办

古代皇帝吃饭动不动就是数十盘菜,皇帝一般都是吃几口就换,皇帝吃饭剩下的食物一般如何处理?中国曾经历了数千年的封建王朝,只不过每一个朝代都诞生了独特的文明,甚至有很多人事物都成为了时代的象征,由于中国古...

2025-09-15

古代殉葬妃子临死前经历了什么

在古代,帝王逝去之后,宫内会特别注重操办丧事。选择合适的地方将其安葬,还有不少嫔妃进行陪葬。这种方式让人觉得难以接受。殉葬制度原本起源在商代,当时的奴隶主在自己死去之后,就要把自己的私有财产带走,这其...

2025-09-15

同治帝十九岁时病死他的后妃怎么处理

1875年1月12日黄昏时分,十几位王公重臣接到诏命,匆匆入宫,来到养心殿西暖阁候旨。原来清朝的第十位皇帝爱新觉罗·载淳,即同治帝,已经走到了人生的尽头。当晚酉时(17~19时),同治皇帝咽下最后一气...

2025-09-15