今日看点

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

曾国藩的传奇故事

曾国藩在道光年间连年被选拔,升官极快,十年之间连升十级,这是可贵的恩遇:在升为正三品大员后,按规则,轿呢要由蓝色换为绿色,护轿人也要添加俩人,并且乘轿是需求配备引路官和护卫的。但令百官惊讶的是,曾国藩...

2025-06-16

曾国藩家书经典语录

1、勤字功夫,第一贵早起,第二贵有恒;凡将相无种,圣贤豪杰无种,只要人肯立志,都可以做得到的。2、盖士人读书,第一要有志,第二要有识,第三要有恒。有志则不甘为下流,有识则知学问无尽,不能以一得自足,有...

2025-06-16

曾巩《道山亭记》赏析

道山亭记宋代:曾巩闽,故隶周者也。至秦,开其地,列于中国,始并为闽中郡。自粤之太末,与吴之豫章,为其通路。其路在闽者,陆出则阸于两山之间,山相属无间断,累数驿乃一得平地,小为县,大为州,然其四顾亦山也...

2025-06-16

曾巩《醒心亭记》赏析

醒心亭记宋代:曾巩滁州之西南,泉水之涯,欧阳公作州之二年,构亭曰“丰乐”,自为记,以见其名义。既又直丰乐之东几百步,得山之高,构亭曰“醒心”,使巩记之。凡公与州之宾客者游焉,则必即丰乐以饮。或醉且劳矣...

2025-06-16

曾巩《醒心亭记》译文

醒心亭记宋代:曾巩滁州之西南,泉水之涯,欧阳公作州之二年,构亭曰“丰乐”,自为记,以见其名义。既又直丰乐之东几百步,得山之高,构亭曰“醒心”,使巩记之。凡公与州之宾客者游焉,则必即丰乐以饮。或醉且劳矣...

2025-06-16

晴雯撕扇

端午佳节间,宝玉因金钏儿之事,心情很糟糕。恰巧晴雯给宝玉换衣时失手把他扇子跌折,便训斥了她几句,晴雯的自尊心受到伤害,还击了一通,不仅把宝玉“气得浑身乱颤”,而且连来劝架的袭人也落了个灰头土脸。最后,...

2025-06-16

曾巩《赠黎安二生序》鉴赏

赠黎安二生序宋代:曾巩赵郡苏轼,余之同年友也。自蜀以书至京师遗余,称蜀之士,曰黎生、安生者。既而黎生携其文数十万言,安生携其文亦数千言,辱以顾余。读其文,诚闳壮隽伟,善反复驰骋,穷尽事理;而其材力之放...

2025-06-16

曾巩《赠黎安二生序》译文

赠黎安二生序宋代:曾巩赵郡苏轼,余之同年友也。自蜀以书至京师遗余,称蜀之士,曰黎生、安生者。既而黎生携其文数十万言,安生携其文亦数千言,辱以顾余。读其文,诚闳壮隽伟,善反复驰骋,穷尽事理;而其材力之放...

2025-06-16

曾巩《咏柳》译文及赏析

咏柳宋代:曾巩乱条犹未变初黄,倚得东风势便狂。解把飞花蒙日月,不知天地有清霜。译文杂乱的柳枝条还没有变黄,在东风的吹动下便飞快的变绿了。只懂得用它的飞絮蒙住日月,却不知天地之间还有秋霜。注释倚:仗恃,...

2025-06-16