成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

HarmonyOS Sample之PixelMap圖像功能開發

開發 OpenHarmony
HarmonyOS圖像模塊支持圖像業務的開發,常見功能如圖像解碼、圖像編碼、基本的位圖操作、圖像編輯等。當然,也支持通過接口組合來實現更復雜的圖像處理邏輯。

[[432512]]

想了解更多內容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區

https://harmonyos.51cto.com

1.介紹

HarmonyOS圖像模塊支持圖像業務的開發,常見功能如圖像解碼、圖像編碼、基本的位圖操作、圖像編輯等。當然,也支持通過接口組合來實現更復雜的圖像處理邏輯。

那么什么叫圖像解碼,什么叫圖像編碼,什么叫位圖?

PixelMap是圖像解碼后無壓縮的位圖格式

圖像解碼就是不同的存檔格式圖片(如JPEG、PNG等)解碼為無壓縮的位圖格式

圖像編碼就是將無壓縮的位圖格式,編碼成不同格式的存檔格式圖片(JPEG、PNG等)

不管是編碼還是解碼的目的是方便在應用或者系統中進行相應的處理。

本文正在參與優質創作者激勵

2.搭建環境

安裝DevEco Studio,詳情請參考DevEco Studio下載。

設置DevEco Studio開發環境,DevEco Studio開發環境需要依賴于網絡環境,需要連接上網絡才能確保工具的正常使用,可以根據如下兩種情況來配置開發環境:

  • 如果可以直接訪問Internet,只需進行下載HarmonyOS SDK操作。
  • 如果網絡不能直接訪問Internet,需要通過代理服務器才可以訪問,請參考配置開發環境。

下載源碼后,使用DevEco Studio 打開項目,模擬器運行即可。

真機運行需要將config.json中的buddleName修改為自己的,如果沒有請到AGC上進行配置,參見 使用模擬器進行調試 。

3.代碼結構

HarmonyOS Sample 之 PixelMap 圖像功能開發-鴻蒙HarmonyOS技術社區

4.實例講解

4.1.界面布局

布局只有幾個操作按鈕,見后面的效果圖,不是本文的重點。

4.2.后臺代碼

4.2.1 圖像解碼功能

圖像解碼就是將所支持格式的存檔圖片解碼成統一的PixelMap圖像,用于后續圖像顯示或其他處理,比如旋轉、縮放、裁剪等。當前支持格式包括JPEG、PNG、GIF、HEIF、WebP、BMP。

主要用到 ImageSource等

ImageSource.SourceOptions 指定數據源的格式信息,非必填

ImageSource.DecodingOptions 用來支持在解碼過程中的圖像處理操作,例如縮放、裁剪、旋轉,非必填。

接下來看個最簡單的圖像解碼的例子:

  1. /** 
  2.  * 圖形解碼 
  3.  * png -->ImageSource -->PixelMap 
  4.  * 
  5.  * @param component 
  6.  */ 
  7. private void commonDecode(Component component) { 
  8.     cleanComponents(); 
  9.     //1.獲取應用程序在設備內部存儲器上存放文件的目錄。 
  10.     String pathName = new File(getFilesDir(), "test.png").getPath(); 
  11.     /* 
  12.     InputStream inputStream=null
  13.     try { 
  14.         //2.讀取 media目錄的圖片 
  15.         inputStream = getContext().getResourceManager().getResource(ResourceTable.Media_icon); 
  16.         ImageSource imageSource = ImageSource.create(inputStream, null); 
  17.         inputStream.close(); 
  18.     } catch (IOException e) { 
  19.         e.printStackTrace(); 
  20.     } catch (NotExistException e) { 
  21.         e.printStackTrace(); 
  22.     }*/ 
  23.  
  24.     //--------------------最簡單的方式----------------------- 
  25.     ImageSource imageSource = ImageSource.create(pathName, null); 
  26.     //解碼為位圖格式 
  27.     PixelMap pixelMap = imageSource.createPixelmap(null); 
  28.     //設置圖片顯示 
  29.     showFirstImage.setPixelMap(pixelMap); 
  30.  
  31.  
  32.     //--------------------使用SourceOptions、DecodingOptions 選項------------- 
  33.     //指定數據源的格式信息,提高解碼效率 
  34.     ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions(); 
  35.     sourceOptions.formatHint = "image/png"
  36.     //image 源 
  37.     imageSource = ImageSource.create(pathName, sourceOptions); 
  38.  
  39.  
  40.     //提供圖像解碼選項 
  41.     ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions(); 
  42.  
  43.     //裁剪,如果設置為全0,則不進行裁剪。358/227-448/279 
  44.     decodingOptions.desiredRegion = new Rect(358, 227, 90, 52); 
  45.  
  46.     //縮放,如果選擇的尺寸與原始圖像尺寸不同,則將圖像放大或縮小后輸出。如果設置為全0,則不進行縮放 
  47.     decodingOptions.desiredSize = new Size(90 * 3, 52 * 3); 
  48.     //旋轉角度,取值范圍為0~360。以原圖為旋轉中心,順時針旋轉圖像。 
  49.     decodingOptions.rotateDegrees = 180; 
  50.  
  51.     //解碼為位圖格式 
  52.     pixelMap = imageSource.createPixelmap(decodingOptions); 
  53.  
  54.     //設置圖片先死 
  55.     showSecondImage.setPixelMap(pixelMap); 
  56.  
  57.     //記得釋放資源 
  58.     imageSource.release(); 
  59.     pixelMap.release(); 

效果:

HarmonyOS Sample 之 PixelMap 圖像功能開發-鴻蒙HarmonyOS技術社區
  1. 10-26 19:02:01.594 13148-13148/? D 00000/=>MainAbilitySlice:  pngCachePath:/data/user/0/com.buty.samples/files/test.png 
  2. 10-26 19:02:01.594 13148-13148/? D 00000/=>MainAbilitySlice:  jpgCachePath:/data/user/0/com.buty.samples/files/test.jpg 

還有一個漸進式解碼,

官方文檔的描述:在未獲取到全部圖像時,支持先更新部分數據來嘗試解碼,調用updateData更新數據,將參數isFinal設置為false;當獲取到全部數據后,最后一次更新數據時設置isFinal為true,表示數據更新完畢。

又去網上搜索了一下關于漸進式解碼的內容,是這么說的。

“漸進式解碼提供在整個圖像完成下載之前以增量方式解碼和呈現圖像部分的能力。 此功能極大地改進了從 Internet 查看圖像時用戶的體驗,因為用戶無需等待整個圖像下載,就可以開始解碼。 在下載整個映像之前,用戶可以查看包含可用數據的映像預覽。 此功能對于用于從 Internet 或帶寬有限的數據源查看圖像的任何應用程序都至關重要。”

“漸進式解碼是一種從不完整的圖像文件中以增量方式解碼圖像部分的能力。 傳統解碼需要完整的圖像文件才能開始解碼。 漸進式解碼在圖像的漸進式級別完成下載后開始。 解碼器對圖像的當前漸進式級別執行解碼傳遞。 然后,它會在下載每個漸進式級別時對圖像執行多個解碼傳遞。 每次解碼傳遞都顯示更多圖像,直到完全下載并解碼圖像。 解碼完整圖像所需的傳遞數取決于圖像文件格式和用于創建圖像的編碼過程。

使用漸進式解碼的要求:

1.圖像文件必須支持漸進式解碼。 大多數圖像格式不支持漸進式解碼,盡管常用圖像格式為 JPEG、PNG 和 GIF。

2.圖像文件必須編碼為漸進式圖像。 未使用漸進式圖像編碼創建的圖像文件無法實現漸進式解碼,即使文件格式會支持漸進式解碼。

3.支持漸進式解碼的編解碼器必須可用。 如果編解碼器不支持漸進式解碼,則編碼為漸進式圖像的圖像將被解碼為傳統圖像。”

一起看個漸進式解碼的例子,

點擊 “漸進式解碼” 按鈕,執行一次imageSource.updateData

  1. /** 
  2.  * 漸進式解碼 
  3.  * 
  4.  * @param component 
  5.  */ 
  6. private void regionDecode(Component component) { 
  7.  
  8.     if (buttonClickNum < 10) { 
  9.         // 獲取到一定的數據時嘗試解碼, 
  10.         imageSource.updateData(fileByte, fileByte.length * buttonClickNum / 10, fileByte.length / 10, 
  11.                 buttonClickNum==9?true:false); 
  12.         pixelMap = imageSource.createPixelmap(null); 
  13.         showResultText.setText( (buttonClickNum + 1) + "/10"); 
  14.         showSecondImage.setPixelMap(pixelMap); 
  15.         buttonClickNum++; 
  16.     }else
  17.         pixelMap.release(); 
  18.         imageSource.release(); 
  19.         readFileForInitFileByte(); 
  20.         buttonClickNum=0; 
  21.         cleanComponents(); 
  22.     } 
  23.  
  24. /** 
  25.  * 讀取文件初始化 fileByte變量 
  26.  * 初始化 imageSource 
  27.  */ 
  28. private void readFileForInitFileByte() { 
  29.     File file = new File(jpgCachePath); 
  30.     try { 
  31.         FileInputStream fileInputStream = new FileInputStream(file); 
  32.         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
  33.         byte[] data = new byte[1024]; 
  34.         int len = -1; 
  35.         while ((len = fileInputStream.read(data)) != -1) { 
  36.             byteArrayOutputStream.write(data, 0, len); 
  37.         } 
  38.         fileByte = byteArrayOutputStream.toByteArray(); 
  39.  
  40.         ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions(); 
  41.         srcOpts.formatHint = "image/jpeg"
  42.         //增量源選項 IncrementalSourceOptions 
  43.         ImageSource.IncrementalSourceOptions incrementalSourceOptions = new ImageSource.IncrementalSourceOptions(); 
  44.         incrementalSourceOptions.opts = srcOpts; 
  45.         //表示只輸入增量數據來更新源。 
  46.         incrementalSourceOptions.mode = ImageSource.UpdateMode.INCREMENTAL_DATA; 
  47.         //創建增量數據源 
  48.         imageSource = ImageSource.createIncrementalSource(incrementalSourceOptions); 
  49.     } catch (IOException e) { 
  50.         e.printStackTrace(); 
  51.     } 

效果:

4.2.2 圖像編碼功能

圖像編碼主要用到ImagePacker

ImagePacker.PackingOptions 設置編碼選項,目前沒有太多可選。

  1. /** 
  2.  * 使用 ImagePacker  來打包圖片 
  3.  * 表示將壓縮圖像打包成文件或其他對象的圖像打包器。 
  4.  * 可以調用create創建圖片打包器,調用initializePacking設置打包選項,調用addImage添加要打包的圖片數據,調用finalizePacking完成打包輸出目標對象。 
  5.  * 
  6.  * @param component 
  7.  */ 
  8. private void encode(Component component) { 
  9.     cleanComponents(); 
  10.     //創建ImagePacker對象 
  11.     ImagePacker imagePacker = ImagePacker.create(); 
  12.     //設置編碼選項 
  13.     ImagePacker.PackingOptions packingOptions = new ImagePacker.PackingOptions(); 
  14.     //圖像質量,范圍從0-100,100為最佳質量。 
  15.     packingOptions.quality = 90; 
  16.  
  17.  
  18.     //文件輸出流, test_encode.jpg 
  19.     try (FileOutputStream outputStream = new FileOutputStream(encodeOutPath)) { 
  20.  
  21.         //初始化將結果輸出到 OutputStream 對象的打包任務。 
  22.         imagePacker.initializePacking(outputStream, packingOptions); 
  23.  
  24.         //圖像數據源,test.png 
  25.         ImageSource imageSource = ImageSource.create(pngCachePath, null); 
  26.  
  27.         //轉為位圖格式 
  28.         PixelMap pixelMap = imageSource.createPixelmap(null); 
  29.  
  30.         //將pixelMap添加到編碼器,進行編碼 
  31.         boolean result = imagePacker.addImage(pixelMap); 
  32.  
  33.         showResultText.setText( 
  34.                 "Encode result : " + result + System.lineSeparator() + "OutputFilePath:" + encodeOutPath); 
  35.  
  36.         //釋放圖像數據源 
  37.         imageSource.release(); 
  38.         pixelMap.release(); 
  39.     } catch (IOException e) { 
  40.         HiLog.info(LABEL_LOG, "%{public}s""encode IOException "); 
  41.     } 
  42.     //釋放imagePacker 
  43.     imagePacker.release(); 

4.2.3 編輯位圖功能

a.通過imageSource創建縮略圖,createThumbnailPixelmap

  1. //解碼 ImageSource 實例中包含的縮略圖數據以生成縮略圖并創建縮略圖像素圖。 
  2. //allowFromImage - 如果 ImageSource 不包含縮略圖數據,則指定是否允許基于原始圖像創建。 
  3. PixelMap thumbnailPixelMap = imageSource.createThumbnailPixelmap(decodingOpts, true); 

 b.讀寫位圖像素數據,畫個像素小人

  1. /** 
  2.  * 編輯位圖 
  3.  * @param component 
  4.  */ 
  5. private void edit(Component component) { 
  6.     cleanComponents(); 
  7.  
  8.     int colorsWidth = 552; 
  9.     int colorsHeight = 310; 
  10.  
  11.     //PixelMap 選項 
  12.     PixelMap.InitializationOptions initializationOptions = new PixelMap.InitializationOptions(); 
  13.     //指示要創建的像素圖的預期大小。 
  14.     initializationOptions.size = new Size(colorsWidth, colorsHeight); 
  15.     initializationOptions.pixelFormat = PixelFormat.ARGB_8888; 
  16.  
  17.     //PixelMap是否允許修改 
  18.     initializationOptions.editable = true
  19.  
  20.     //表示像素顏色的 int 數組。 數組中的每個元素都是 PixelFormat#ARGB_8888 格式。 
  21.     int[] colors = new int[colorsWidth * colorsHeight]; 
  22.     Arrays.fill(colors, Color.RED.getValue()); 
  23.  
  24.     //創建PixelMap 
  25.     PixelMap pixelMap = PixelMap.create(colors, initializationOptions); 
  26.     //顯示圖片 
  27.     showFirstImage.setPixelMap(pixelMap); 
  28.  
  29.  
  30.     // 以另外一個PixelMap作為數據源創建 
  31.     PixelMap pixelMap2 = PixelMap.create(pixelMap, initializationOptions); 
  32.  
  33.     //讀取指定位置的顏色值。 
  34.     int color = pixelMap2.readPixel(new Position(1, 1)); 
  35.     HiLog.info(LABEL_LOG, "%{public}s""pixelMapEdit readPixel color :" + color); 
  36.  
  37.  
  38.     //背上一把寶劍 
  39.     for(int i=130;i<180 ;i++){ 
  40.         //在指定位置寫入像素 
  41.         pixelMap2.writePixel(new Position(i, i), 0xFF112233); 
  42.     } 
  43.  
  44.     //在指定區域寫入像素 
  45.     int[] pixelArray = new int[1024*1024]; 
  46.     Arrays.fill(pixelArray, Color.BLACK.getValue()); 
  47.     //頭 
  48.     Rect region = new Rect(160, 110, 50, 50); 
  49.     //stride - 表示數組每行中的像素數。 該值必須大于或等于此 PixelMap 中目標區域的寬度。 
  50.     pixelMap2.writePixels(pixelArray, 0, 100, region); 
  51.  
  52.     Arrays.fill(pixelArray, Color.GREEN.getValue()); 
  53.     //身體 
  54.     Rect region2= new Rect(150, 160, 70, 60); 
  55.     //stride - 表示數組每行中的像素數。 該值必須大于或等于此 PixelMap 中目標區域的寬度。 
  56.     pixelMap2.writePixels(pixelArray, 0, 100, region2); 
  57.  
  58.     Arrays.fill(pixelArray, Color.YELLOW.getValue()); 
  59.     //胳膊 
  60.     Rect region3= new Rect(130, 160, 20, 70); 
  61.     //stride - 表示數組每行中的像素數。 該值必須大于或等于此 PixelMap 中目標區域的寬度。 
  62.     pixelMap2.writePixels(pixelArray, 0, 100, region3); 
  63.     //胳膊 
  64.     Rect region4= new Rect(220, 160, 20, 70); 
  65.     //stride - 表示數組每行中的像素數。 該值必須大于或等于此 PixelMap 中目標區域的寬度。 
  66.     pixelMap2.writePixels(pixelArray, 0, 100, region4); 
  67.  
  68.     Arrays.fill(pixelArray, Color.GRAY.getValue()); 
  69.     //腿 
  70.     Rect region5= new Rect(160, 200, 20, 70); 
  71.     //stride - 表示數組每行中的像素數。 該值必須大于或等于此 PixelMap 中目標區域的寬度。 
  72.     pixelMap2.writePixels(pixelArray, 0, 100, region5); 
  73.  
  74.     //腿 
  75.     Rect region6= new Rect(190, 200, 20, 70); 
  76.     //stride - 表示數組每行中的像素數。 該值必須大于或等于此 PixelMap 中目標區域的寬度。 
  77.     pixelMap2.writePixels(pixelArray, 0, 100, region6); 
  78.  
  79.     showSecondImage.setPixelMap(pixelMap2); 
  80.  
  81.     //從位圖對象中獲取信息 
  82.     long capacity = pixelMap.getPixelBytesCapacity(); 
  83.     long bytesNumber = pixelMap.getPixelBytesNumber(); 
  84.     int rowBytes = pixelMap.getBytesNumberPerRow(); 
  85.     byte[] ninePatchData = pixelMap.getNinePatchChunk(); 
  86.  
  87.     showResultText.setText( 
  88.             "This pixelMap detail info :" + System.lineSeparator() + "capacity = " + capacity + System.lineSeparator() 
  89.                     + "bytesNumber = " + bytesNumber + System.lineSeparator() + "rowBytes = " + rowBytes 
  90.                     + System.lineSeparator() + "ninePatchData = " + Arrays.toString(ninePatchData) + System.lineSeparator()); 
  91.     pixelMap.release(); 
  92.     pixelMap2.release(); 

效果是這樣的:

HarmonyOS Sample 之 PixelMap 圖像功能開發-鴻蒙HarmonyOS技術社區

4.2.4 獲取圖像屬性

圖像屬性解碼就是獲取圖像中包含的屬性信息,PropertyKey 存儲了常用的屬性KEY信息。

看代碼:

  1. /** 
  2.  * 獲取圖片的縮略圖和位置信息 
  3.  * 
  4.  * @param component 
  5.  */ 
  6. private void attribute(Component component) { 
  7.     cleanComponents(); 
  8.  
  9.     ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions(); 
  10.     srcOpts.formatHint = "image/jpeg"
  11.  
  12.     HiLog.debug(LABEL_LOG,"jpgCachePath="+jpgCachePath); 
  13.     ImageSource imageSource = ImageSource.create(jpgCachePath, srcOpts); 
  14.  
  15.     ImageSource.DecodingOptions decodingOpts = new ImageSource.DecodingOptions(); 
  16.  
  17.     //解碼 ImageSource 實例中包含的縮略圖數據以生成縮略圖并創建縮略圖像素圖。 
  18.     //allowFromImage - 如果 ImageSource 不包含縮略圖數據,則指定是否允許基于原始圖像創建。 
  19.     PixelMap thumbnailPixelMap = imageSource.createThumbnailPixelmap(decodingOpts, true); 
  20.  
  21.  
  22.     //位置信息 
  23.     String location = imageSource.getImagePropertyString(PropertyKey.Exif.SUBJECT_LOCATION); 
  24.  
  25.     HiLog.info(LABEL_LOG, "%{public}s""imageExif location : " + location); 
  26.  
  27.     showResultText.setText("ImageSource attribute : createThumbnailPixelMap"); 
  28.     showSecondImage.setPixelMap(thumbnailPixelMap); 
  29.  
  30.     // 
  31.     imageSource.release(); 
  32.     thumbnailPixelMap.release(); 

效果:

HarmonyOS Sample 之 PixelMap 圖像功能開發-鴻蒙HarmonyOS技術社區

5.思考總結

通過這次實踐,可以學到的內容:

  • 位圖的概念,如何進行圖像的編碼、解碼、編輯。
  • 漸進式解碼的概念,如何生成縮略圖,獲取其他圖像屬性等。

文章相關附件可以點擊下面的原文鏈接前往下載

https://harmonyos.51cto.com/resource/1419

想了解更多內容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區

https://harmonyos.51cto.com

 

責任編輯:jianghua 來源: 鴻蒙社區
相關推薦

2021-09-24 09:25:01

鴻蒙HarmonyOS應用

2021-08-19 09:56:20

Windows 11操作系統微軟

2012-03-07 14:37:03

JavaJavaMail

2021-12-10 15:06:56

鴻蒙HarmonyOS應用

2022-08-09 16:01:24

應用開發鴻蒙

2012-11-05 10:36:40

IBMdw

2021-09-15 14:55:49

鴻蒙HarmonyOS應用

2021-08-17 10:20:14

鴻蒙HarmonyOS應用

2021-09-17 14:43:54

鴻蒙HarmonyOS應用

2023-04-26 09:37:25

智駕開發

2023-02-28 15:49:09

鴻蒙應用開發

2021-11-23 09:58:35

鴻蒙HarmonyOS應用

2017-12-11 14:50:34

前端Javascript文本朗讀

2021-09-22 09:42:41

鴻蒙HarmonyOS應用

2021-07-08 09:42:04

鴻蒙HarmonyOS應用

2021-07-29 14:03:35

鴻蒙HarmonyOS應用

2021-11-30 14:51:11

鴻蒙HarmonyOS應用

2021-12-02 10:11:44

鴻蒙HarmonyOS應用

2021-08-24 15:13:06

鴻蒙HarmonyOS應用
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 91在线网 | 久久久久国产 | 久久在线看 | 欧美高清视频一区 | av日韩精品| 国产又色又爽又黄又免费 | 国产精品综合久久 | 日韩精品无码一区二区三区 | av在线伊人 | 久久久精品视频免费看 | 欧美成人一区二区三区 | 久久久久亚洲国产| 国产欧美一区二区三区免费 | 亚洲最新网址 | 国产精品久久久久久久久免费丝袜 | 免费观看的av毛片的网站 | 日韩视频在线观看中文字幕 | 成人免费在线播放视频 | 国产精品精品久久久久久 | 日韩在线精品 | 欧美视频一区二区三区 | 精品一区二区三区91 | 蜜月aⅴ免费一区二区三区 99re在线视频 | 国产极品车模吞精高潮呻吟 | 国产精品久久久久久久久久久久久 | 日韩在线视频一区 | 国产一区免费视频 | 日韩五月天| 久久av一区 | 日日骚网 | 激情久久网 | 99热精品在线 | 亚洲天堂影院 | 亚洲视频在线看 | 精品国产91 | 久久久青草婷婷精品综合日韩 | 亚洲精品视频二区 | 成人在线观看中文字幕 | 三级av在线 | 国产一区免费 | 国产高潮好爽受不了了夜色 |