鴻蒙OS 圖像解碼開發(fā)指導

2020-09-18 14:17 更新

場景介紹

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

接口說明

ImageSource 主要用于圖像解碼。

接口名 描述
create(String pathName, SourceOptions opts) 從圖像文件路徑創(chuàng)建圖像數據源。
create(InputStream is, SourceOptions opts) 從輸入流創(chuàng)建圖像數據源。
create(byte[] data, SourceOptions opts) 從字節(jié)數組創(chuàng)建圖像源。
create(byte[] data, int offset, int length, SourceOptions opts) 從字節(jié)數組指定范圍創(chuàng)建圖像源。
create(File file, SourceOptions opts) 從文件對象創(chuàng)建圖像數據源。
create(FileDescriptor fd, SourceOptions opts) 從文件描述符創(chuàng)建圖像數據源。
createIncrementalSource(SourceOptions opts) 創(chuàng)建漸進式圖像數據源。
createIncrementalSource(IncrementalSourceOptions opts) 創(chuàng)建漸進式圖像數據源,支持設置漸進式數據更新模式。
createPixelmap(DecodingOptions opts) 從圖像數據源解碼并創(chuàng)建 PixelMap 圖像。
createPixelmap(int index, DecodingOptions opts) 從圖像數據源解碼并創(chuàng)建 PixelMap 圖像,如果圖像數據源支持多張圖片的話,支持指定圖像索引。
updateData(byte[] data, boolean isFinal) 更新漸進式圖像源數據。
updateData(byte[] data, int offset, int length, boolean isFinal) 更新漸進式圖像源數據,支持設置輸入數據的有效數據范圍。
getImageInfo() 獲取圖像基本信息。
getImageInfo(int index) 根據特定的索引獲取圖像基本信息。
getSourceInfo() 獲取圖像源信息。
release() 釋放對象關聯(lián)的本地資源。

普通解碼開發(fā)步驟

  1. 創(chuàng)建圖像數據源 ImageSource 對象,可以通過 SourceOptions 指定數據源的格式信息,此格式信息僅為給解碼器的提示,正確提供能幫助提高解碼效率,如果不設置或設置不正確,會自動檢測正確的圖像格式。不使用該選項時,可以將 create 接口傳入的 SourceOptions 設置為 null。

   ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions();
   srcOpts.formatHint = "image/png";
   String pathName = "/path/to/image.png";
   ImageSource imageSource = ImageSource.create(pathName, srcOpts);
   ImageSource imageSourceNoOptions = ImageSource.create(pathName, null);

  1. 設置解碼參數,解碼獲取 PixelMap 圖像對象,解碼過程中同時支持圖像處理操作。設置 desiredRegion 支持按矩形區(qū)域裁剪,如果設置為全 0,則不進行裁剪。設置 desiredSize 支持按尺寸縮放,如果設置為全 0,則不進行縮放。設置 rotateDegrees 支持旋轉角度,以圖像中心點順時針旋轉。如果只需要解碼原始圖像,不使用該選項時,可將給 createPixelMap 傳入的 DecodingOptions 設置為 null。

   // 普通解碼疊加旋轉、縮放、裁剪
   ImageSource.DecodingOptions decodingOpts = new ImageSource.DecodingOptions();
   decodingOpts.desiredSize = new Size(100, 2000);
   decodingOpts.desiredRegion = new Rect(0, 0, 100, 100);
   decodingOpts.rotateDegrees = 90;
   PixelMap pixelMap = imageSource.createPixelmap(decodingOpts); 

    
   // 普通解碼
   PixelMap pixelMapNoOptions = imageSource.createPixelmap(null);

  1. 解碼完成獲取到 PixelMap 對象后,可以進行后續(xù)處理,比如渲染顯示等。

漸進式解碼開發(fā)步驟

  1. 創(chuàng)建漸進式圖像數據源 ImageSource 對象,可以通過 SourceOptions 指定數據源的格式信息,此格式信息僅為提示,如果填寫不正確,會自動檢測正確的圖像格式,使用 IncrementalSourceOptions 指定圖像數據的更新方式為漸進式更新。

   ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions();
   srcOpts.formatHint = "image/jpeg";
   ImageSource.IncrementalSourceOptions incOpts = new ImageSource.IncrementalSourceOptions();
   incOpts.opts = srcOpts;
   incOpts.mode = ImageSource.UpdateMode.INCREMENTAL_DATA;
   imageSource = ImageSource.createIncrementalSource(incOpts);

  1. 漸進式更新數據,在未獲取到全部圖像時,支持先更新部分數據來嘗試解碼,更新數據時設置 isFinal 為 false,當獲取到全部數據后,最后一次更新數據時設置 isFinal 為 true,表示數據更新完畢。設置解碼參數同普通解碼。

   // 獲取到一定的數據時嘗試解碼
   imageSource.updateData(data, 0, bytes, false);
   ImageSource.DecodingOptions decodingOpts = new ImageSource.DecodingOptions();
   PixelMap pixelMap = imageSource.createPixelmap(decodingOpts);

    
   // 更新數據再次解碼,重復調用直到數據全部更新完成
   imageSource.updateData(data, 0, bytes, false);
   PixelMap pixelMap = imageSource.createPixelmap(decodingOpts);

    
   // 數據全部更新完成時需要傳入isFinal為true
   imageSource.updateData(data, 0, bytes, true);
   PixelMap pixelMap = imageSource.createPixelmap(decodingOpts);

  1. 解碼完成獲取到 PixelMap 對象后,可以進行后續(xù)處理,比如渲染顯示等。
以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號