今日看点

【软通动力】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米的土堆,但却完整地保留了三...

2025-04-30

西井陪葬墓简介

西井,属于北京昌平区明十三陵陵区内,是明长陵的妃嫔陪葬墓之一。位于定陵西南,昭陵之北,占地面积比东井还大。概述 西井的现存状况不如东井,围墙只有北面一段残存。宝城和殿堂仅存遗址可以辨认。宝城下面几十米...

2025-04-30

明悼陵介绍

悼陵,即“世宗三妃墓”,俗称"大宫"。是北京昌平明十三陵的七座陪葬墓之一。位于袄儿峪。原葬明世宗嘉靖皇帝的原配妻子孝洁陈皇后。后来陈皇后迁葬永陵,悼陵成为明世宗其他三位妃子的墓所。墓葬简介世宗三妃墓(...

2025-04-30

东井陪葬墓介绍

东井,当地俗称东娘娘宫,位于北京昌平区明十三陵陵区内,是长陵的妃嫔陪葬墓之一。东井在德陵东南馒头山之南,坐东朝西。在德陵村和德陵之间,与一户农家小院隔路相邻。在一片荒草之中,愕然耸立着一块无字碑,与十...

2025-04-30

郑贵妃墓简介

郑贵妃墓,位于北京昌平区、明十三陵陵区内,万娘坟村(万贵妃墓)而南约1公里处的银钱(泉)山,坐北朝南。是明十三陵的7座妃子墓之一,墓主人为明神宗万历皇帝的皇贵妃郑氏。墓园介绍郑贵妃墓是明十三陵的7座妃...

2025-04-30

神宗四妃墓介绍

神宗四妃墓,又称作神宗四妃坟,俗称东小宫。是明十三陵的7座妃子墓之一,是明神宗万历皇帝四位妃子的合葬墓。神宗四妃墓位于北京市昌平区十三陵,郑贵妃墓西南200米处。规模不大,也就相当于郑贵妃墓的四分之一...

2025-04-30

明茂陵与聚宝山的关系

明茂陵,位于裕陵右侧的聚宝山下,是明朝第八代皇帝宪宗纯皇帝朱见深(年号成化)和王氏、纪氏、邵氏三位皇后的合葬陵寝。茂陵的建筑在清初时保存尚好,而且祾恩殿内的陈设也保存较多。顾炎武《昌平山水记》记载顺治...

2025-04-30

明十三陵神路简介

十三陵神道,即长陵神道,是长陵陵寝建筑的前导部分。总长约7.3公里。明朝时由南而北,依次建有石牌坊、三空桥、大红门、神功圣德碑亭、石像生、棂星门、南五空桥、七空桥、北五空桥等系列神道墓仪设施及桥涵建筑...

2025-04-30

《吕氏春秋·开春论》贵卒赏析

【原文】力贵突①,智贵卒②。得之同则速为上,胜之同则湿③为下。所为贵骥者,为其一日千里也;旬日取之④,与驽骀⑤同。所为贵镞矢者⑥,为其应声而至;终日而至,则与无至同。吴起谓荆王⑦曰:“荆所有余者地也;...

2025-04-30

《庄子·外篇·达生》原文

达生之情者,不务生之所无以为;达命之情者,不务知之所无奈何。养形必先之以物,物有余而形不养者有之矣。有生必先无离形,形不离而生亡者有之矣。生之来不能却,其去不能止。悲夫!世之人以为养形足以存生,而养形...

2025-04-30