当前位置:主页 > 查看内容

鸿蒙HarmonyOS三方件开发指南(7)-compress组件

发布时间:2021-06-11 00:00| 位朋友查看

简介:想了解更多内容,请访问: 51CTO和华为官方战略合作共建的鸿蒙技术社区 https://harmonyos.51cto.com/#zz 1. 组件compress功能介绍 1.1. 组件介绍: compress是一个轻量级图像压缩库。compress允许将大照片压缩成小尺寸的照片,图像质量损失非常小或可以忽略……

 

想了解更多内容,请访问:

51CTO和华为官方战略合作共建的鸿蒙技术社区

https://harmonyos.51cto.com/#zz

1. 组件compress功能介绍

1.1. 组件介绍:

compress是一个轻量级图像压缩库。compress允许将大照片压缩成小尺寸的照片,图像质量损失非常小或可以忽略不计。

1.2. 手机模拟器上运行效果:


2. 组件compress使用方法

2.1. 添加依赖

将compress-debug.har复制到应用的entry\libs目录下即可(由于build.gradle中已经依赖的libs目录下的*.har,因此不需要再做修改)。

2.2. 设置布局

  1. <DependentLayout 
  2.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  3.     ohos:width="match_parent" 
  4.     ohos:height="match_parent" 
  5.     ohos:background_element="#FFFFFF"
  6.     <Image 
  7.         ohos:id="$+id:image1" 
  8.         ohos:height="match_parent" 
  9.         ohos:width="match_parent" 
  10.         ohos:image_src="$media:dog1.PNG"/> 
  11.     <Text 
  12.         ohos:id="$+id:text" 
  13.         ohos:width="match_content" 
  14.         ohos:height="match_content" 
  15.         ohos:text="" 
  16.         ohos:text_size="19fp" 
  17.         ohos:text_color="#1C1C1C" 
  18.         ohos:top_padding="8vp" 
  19.         ohos:bottom_padding="8vp" 
  20.         ohos:right_padding="70vp" 
  21.         ohos:left_padding="70vp" 
  22.         ohos:center_in_parent="true" 
  23.         ohos:align_parent_bottom="true" 
  24.         ohos:bottom_margin="120vp"/> 
  25.     <Button 
  26.         ohos:id="$+id:choose_button" 
  27.         ohos:width="match_content" 
  28.         ohos:height="match_content" 
  29.         ohos:text="Choose Image" 
  30.         ohos:text_size="19fp" 
  31.         ohos:text_color="#FFFFFF" 
  32.         ohos:top_padding="8vp" 
  33.         ohos:bottom_padding="8vp" 
  34.         ohos:right_padding="70vp" 
  35.         ohos:left_padding="70vp" 
  36.         ohos:background_element="$graphic:background_button" 
  37.         ohos:center_in_parent="true" 
  38.         ohos:align_parent_bottom="true" 
  39.         ohos:bottom_margin="75vp"/> 
  40.     <Button 
  41.         ohos:id="$+id:button" 
  42.         ohos:width="match_content" 
  43.         ohos:height="match_content" 
  44.         ohos:text="Compress" 
  45.         ohos:text_size="19fp" 
  46.         ohos:text_color="#FFFFFF" 
  47.         ohos:top_padding="8vp" 
  48.         ohos:bottom_padding="8vp" 
  49.         ohos:right_padding="70vp" 
  50.         ohos:left_padding="70vp" 
  51.         ohos:background_element="$graphic:background_button" 
  52.         ohos:center_in_parent="true" 
  53.         ohos:align_parent_bottom="true" 
  54.         ohos:bottom_margin="15vp"/> 
  55. </DependentLayout> 

2.3. 图像压缩

核心类:Compressor

核心方法:

(1)自定义压缩:

  1. public static File customCompress(Context context, File file, int width, int height, int quality) throws IOException  

参数:

context - 应用程序上下文

file - 待压缩图片抽象路径名

width - 压缩后宽度

height - 压缩后高度

quality - 图片压缩质量,范围0~100

结果:

返回压缩后图片抽象路径名。

异常:

发生I/O异常

(2)默认压缩:

  1. public static File defaultCompress(Context context, File file) throws IOException 

参数:

context - 应用程序上下文

file - 待压缩图片抽象路径名

结果:

返回压缩后图片抽象路径名。

异常:

发生I/O异常

简单示例:

运行示例前需要在模拟器保存一张截图或使用相机功能照一张照片

  1. public void onStart(Intent intent) { 
  2.  
  3.     super.onStart(intent); 
  4.  
  5.     super.setUIContent(ResourceTable.Layout_ability_main); 
  6.  
  7.  
  8.  
  9.     // 请求文件的读取权限 
  10.  
  11.     String[] permissions = {"ohos.permission.READ_USER_STORAGE"}; 
  12.  
  13.     requestPermissionsFromUser(permissions, 0); 
  14.  
  15.  
  16.  
  17.     // 获取压缩按钮并绑定事件 
  18.  
  19.     Button button = (Button) findComponentById(ResourceTable.Id_button); 
  20.  
  21.     if (button != null) { 
  22.  
  23.         // 为按钮设置点击回调 
  24.  
  25.         button.setClickedListener(new Component.ClickedListener() { 
  26.  
  27.             @Override 
  28.  
  29.             public void onClick(Component component) { 
  30.  
  31.                 try { 
  32.  
  33.                     File file = new File(System.getProperty("java.io.tmpdir") + File.separator + tmpName); 
  34.  
  35.                     HiLog.error(LOG_LABEL, "old size..." + file.length() +  " ...b"); 
  36.  
  37.  
  38.  
  39.                     // 默认压缩 
  40.  
  41.                     // File newFile = Compressor.defaultCompress(file); 
  42.  
  43.  
  44.  
  45.                     // 自定义压缩 
  46.  
  47.                     File newFile = Compressor.customCompress(getContext(), file, 500, 1000, 60); 
  48.  
  49.                     Text text = (Text) findComponentById(ResourceTable.Id_text); 
  50.  
  51.                     text.setText("size: " + newFile.length() + " b"); 
  52.  
  53.                     HiLog.error(LOG_LABEL, "new size..." + newFile.length() +  " ...b"); 
  54.  
  55.                     PixelMap newPixelMap = Compressor.decode(newFile); 
  56.  
  57.                     Image image = (Image) findComponentById(ResourceTable.Id_image1); 
  58.  
  59.                     image.setPixelMap(newPixelMap); 
  60.  
  61.                 } catch (IOException e) { 
  62.  
  63.                     e.printStackTrace(); 
  64.  
  65.                 } 
  66.  
  67.             } 
  68.  
  69.         }); 
  70.  
  71.     } 
  72.  
  73.     // 获取选择图片按钮并绑定事件 
  74.  
  75.     Button chooseButton = (Button) findComponentById(ResourceTable.Id_choose_button); 
  76.  
  77.     if (chooseButton != null) { 
  78.  
  79.         // 为按钮设置点击回调 
  80.  
  81.         chooseButton.setClickedListener(new Component.ClickedListener() { 
  82.  
  83.             @Override 
  84.  
  85.             public void onClick(Component component) { 
  86.  
  87.                 DataAbilityHelper helper = DataAbilityHelper.creator(getContext()); 
  88.  
  89.                 try { 
  90.  
  91.                     ResultSet resultSet = helper.query(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI, nullnull); 
  92.  
  93.                     while (resultSet != null && resultSet.goToNextRow()) { 
  94.  
  95.                         // 互殴媒体库的图片 
  96.  
  97.                         int id = resultSet.getInt(resultSet.getColumnIndexForName(AVStorage.Images.Media.ID)); 
  98.  
  99.                         HiLog.error(LOG_LABEL, "id:..." + id +  " ..."); 
  100.  
  101.                         Uri uri = Uri.appendEncodedPathToUri(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI, "" + id); 
  102.  
  103.                         // 根据图片的uri打开文件并保存到临时目录中 
  104.  
  105.                         FileDescriptor fileDescriptor = helper.openFile(uri, "r"); 
  106.  
  107.                         ImageSource.DecodingOptions decodingOpts = new ImageSource.DecodingOptions(); 
  108.  
  109.                         decodingOpts.sampleSize = ImageSource.DecodingOptions.DEFAULT_SAMPLE_SIZE; 
  110.  
  111.                         ImageSource imageSource = ImageSource.create(fileDescriptor, null); 
  112.  
  113.                         PixelMap pixelMap = imageSource.createThumbnailPixelmap(decodingOpts, true); 
  114.  
  115.                         ImagePacker imagePacker = ImagePacker.create(); 
  116.  
  117.                         tmpName = UUID.randomUUID().toString(); 
  118.  
  119.                         File file = new File(System.getProperty("java.io.tmpdir") + File.separator + tmpName); 
  120.  
  121.                         FileOutputStream outputStream = new FileOutputStream(file); 
  122.  
  123.                         ImagePacker.PackingOptions packingOptions = new ImagePacker.PackingOptions(); 
  124.  
  125.                         packingOptions.quality = 100; 
  126.  
  127.                         boolean result = imagePacker.initializePacking(outputStream, packingOptions); 
  128.  
  129.                         result = imagePacker.addImage(pixelMap); 
  130.  
  131.                         long dataSize = imagePacker.finalizePacking(); 
  132.  
  133.                         // 显示图片和图片大小 
  134.  
  135.                         Text text = (Text) findComponentById(ResourceTable.Id_text); 
  136.  
  137.                         text.setText("size: " + file.length() + " b"); 
  138.  
  139.                         Image image = (Image) findComponentById(ResourceTable.Id_image1); 
  140.  
  141.                         image.setPixelMap(pixelMap); 
  142.  
  143.                     } 
  144.  
  145.                 } catch (DataAbilityRemoteException | FileNotFoundException e) { 
  146.  
  147.                     e.printStackTrace(); 
  148.  
  149.                 } 
  150.  
  151.             } 
  152.  
  153.         }); 
  154.  
  155.     } 
  156.  

3. 组件compress开发实现

3.1. 拷贝图片制临时目录

传入的图片路径拷贝临时文件到应用的临时目录。

  1. private static File copyToCache(Context context, File imageFile) throws IOException { 
  2.  
  3.     PixelMap pixelMap = decode(imageFile); 
  4.  
  5.     String cachePath = context.getCacheDir() + File.separator + imageFile.getName(); 
  6.  
  7.     File cacheFile = new File(cachePath); 
  8.  
  9.     int quality = 100; // 压缩质量 
  10.  
  11.     refreshTmpFile(pixelMap, cacheFile, quality); 
  12.  
  13.     return cacheFile; 
  14.  

3.2. 图片解码

对临时目录里的图片进行解码

  1. private static PixelMap decode(File file, int width, int height) { 
  2.  
  3.     ImageSource imageSource = ImageSource.create(file, null); 
  4.     mageSource.DecodingOptions decodingOpts = new 
  5.  
  6. ImageSource.DecodingOptions(); 
  7.     decodingOpts.desiredSize = new Size(width, height); 
  8.     return imageSource.createPixelmap(decodingOpts); 
  9.  

3.3. 图片编码

按照开发人员设定的规则进行编码,生成新图片

  1. private static void refreshTmpFile(PixelMap pixelMap, File file, int quality) 
  2.  
  3. throws IOException { 
  4.  
  5.     ImagePacker imagePacker = ImagePacker.create(); 
  6.  
  7.     ImagePacker.PackingOptions options = new ImagePacker.PackingOptions(); 
  8.  
  9.     options.quality = quality; 
  10.  
  11.     imagePacker.initializePacking(new FileOutputStream(file), options); 
  12.  
  13.     imagePacker.addImage(pixelMap); 
  14.  
  15.     imagePacker.finalizePacking(); 
  16.  

项目源代码地址:https://github.com/isoftstone-dev/Compressor_Harmony

欢迎交流:HOS@isoftstone.com

©著作权归作者和HarmonyOS技术社区共同所有,如需转载,请注明出处,否则将追究法律责任。

想了解更多内容,请访问:

51CTO和华为官方战略合作共建的鸿蒙技术社区

https://harmonyos.51cto.com/#zz


本文转载自网络,原文链接:https://harmonyos.51cto.com/#zz
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文

  • 周排行
  • 月排行
  • 总排行

随机推荐