自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

鴻蒙HarmonyOS三方件開(kāi)發(fā)指南(7)-compress組件

開(kāi)發(fā) OpenHarmony
文章由鴻蒙社區(qū)產(chǎn)出,想要了解更多內(nèi)容請(qǐng)前往:51CTO和華為官方戰(zhàn)略合作共建的鴻蒙技術(shù)社區(qū)https://harmonyos.51cto.com/#zz

 [[380276]]

想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

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

1. 組件compress功能介紹

1.1. 組件介紹:

compress是一個(gè)輕量級(jí)圖像壓縮庫(kù)。compress允許將大照片壓縮成小尺寸的照片,圖像質(zhì)量損失非常小或可以忽略不計(jì)。

1.2. 手機(jī)模擬器上運(yùn)行效果:


2. 組件compress使用方法

2.1. 添加依賴

將compress-debug.har復(fù)制到應(yīng)用的entry\libs目錄下即可(由于build.gradle中已經(jīng)依賴的libs目錄下的*.har,因此不需要再做修改)。

2.2. 設(shè)置布局

  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  

參數(shù):

context - 應(yīng)用程序上下文

file - 待壓縮圖片抽象路徑名

width - 壓縮后寬度

height - 壓縮后高度

quality - 圖片壓縮質(zhì)量,范圍0~100

結(jié)果:

返回壓縮后圖片抽象路徑名。

異常:

發(fā)生I/O異常

(2)默認(rèn)壓縮:

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

參數(shù):

context - 應(yīng)用程序上下文

file - 待壓縮圖片抽象路徑名

結(jié)果:

返回壓縮后圖片抽象路徑名。

異常:

發(fā)生I/O異常

簡(jiǎn)單示例:

運(yùn)行示例前需要在模擬器保存一張截圖或使用相機(jī)功能照一張照片

  1. public void onStart(Intent intent) { 
  2.  
  3.     super.onStart(intent); 
  4.  
  5.     super.setUIContent(ResourceTable.Layout_ability_main); 
  6.  
  7.  
  8.  
  9.     // 請(qǐng)求文件的讀取權(quán)限 
  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.         // 為按鈕設(shè)置點(diǎn)擊回調(diào) 
  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.                     // 默認(rèn)壓縮 
  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.         // 為按鈕設(shè)置點(diǎn)擊回調(diào) 
  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.                         // 互毆媒體庫(kù)的圖片 
  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.                         // 根據(jù)圖片的uri打開(kāi)文件并保存到臨時(shí)目錄中 
  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開(kāi)發(fā)實(shí)現(xiàn)

3.1. 拷貝圖片制臨時(shí)目錄

傳入的圖片路徑拷貝臨時(shí)文件到應(yīng)用的臨時(shí)目錄。

  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; // 壓縮質(zhì)量 
  10.  
  11.     refreshTmpFile(pixelMap, cacheFile, quality); 
  12.  
  13.     return cacheFile; 
  14.  

 3.2. 圖片解碼

對(duì)臨時(shí)目錄里的圖片進(jìn)行解碼

  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. 圖片編碼

按照開(kāi)發(fā)人員設(shè)定的規(guī)則進(jìn)行編碼,生成新圖片

  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.  

 項(xiàng)目源代碼地址:https://github.com/isoftstone-dev/Compressor_Harmony

歡迎交流:HOS@isoftstone.com

©著作權(quán)歸作者和HarmonyOS技術(shù)社區(qū)共同所有,如需轉(zhuǎn)載,請(qǐng)注明出處,否則將追究法律責(zé)任。

想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

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

 

責(zé)任編輯:jianghua 來(lái)源: 鴻蒙社區(qū)
相關(guān)推薦

2021-06-28 14:48:03

鴻蒙HarmonyOS應(yīng)用

2021-01-18 09:52:20

鴻蒙HarmonyOS開(kāi)發(fā)

2021-01-12 12:04:40

鴻蒙HarmonyOS應(yīng)用開(kāi)發(fā)

2021-01-21 13:21:18

鴻蒙HarmonyOSPhotoview組件

2021-01-20 09:54:56

鴻蒙HarmonyOS開(kāi)發(fā)

2021-02-24 15:22:47

鴻蒙HarmonyOS應(yīng)用開(kāi)發(fā)

2021-03-01 09:48:24

鴻蒙HarmonyOS應(yīng)用開(kāi)發(fā)

2021-01-13 09:40:31

鴻蒙HarmonyOS開(kāi)發(fā)

2021-02-04 13:06:38

鴻蒙HarmonyOS應(yīng)用開(kāi)發(fā)

2021-04-16 09:28:18

鴻蒙HarmonyOS應(yīng)用

2021-01-22 17:33:03

鴻蒙HarmonyOS應(yīng)用開(kāi)發(fā)

2021-02-26 14:15:27

鴻蒙HarmonyOS應(yīng)用開(kāi)發(fā)

2021-05-12 15:17:39

鴻蒙HarmonyOS應(yīng)用

2021-03-01 14:01:41

鴻蒙HarmonyOS應(yīng)用開(kāi)發(fā)

2021-03-19 17:42:01

鴻蒙HarmonyOS應(yīng)用開(kāi)發(fā)

2021-04-20 09:42:20

鴻蒙HarmonyOS應(yīng)用開(kāi)發(fā)

2021-03-31 09:50:25

鴻蒙HarmonyOS應(yīng)用開(kāi)發(fā)

2021-04-12 09:36:54

鴻蒙HarmonyOS應(yīng)用

2021-03-10 15:03:40

鴻蒙HarmonyOS應(yīng)用

2021-08-02 14:54:50

鴻蒙HarmonyOS應(yīng)用
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)