新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)鴻蒙OS教程:鴻蒙OS 圖像解碼開發(fā)指導(dǎo)
場景介紹

圖像解碼就是將所支持格式的存檔圖片解碼成統(tǒng)一的 PixelMap 圖像,用于后續(xù)圖像顯示或其他處理,比如旋轉(zhuǎn)、縮放、裁剪等。當(dāng)前支持格式包括 JPEG、PNG、GIF、HEIF、WebP、BMP。
接口說明
ImageSource 主要用于圖像解碼。
| 接口名 | 描述 |
|---|---|
| create(String pathName, SourceOptions opts) | 從圖像文件路徑創(chuàng)建圖像數(shù)據(jù)源。 |
| create(InputStream is, SourceOptions opts) | 從輸入流創(chuàng)建圖像數(shù)據(jù)源。 |
| create(byte[] data, SourceOptions opts) | 從字節(jié)數(shù)組創(chuàng)建圖像源。 |
| create(byte[] data, int offset, int length, SourceOptions opts) | 從字節(jié)數(shù)組指定范圍創(chuàng)建圖像源。 |
| create(File file, SourceOptions opts) | 從文件對象創(chuàng)建圖像數(shù)據(jù)源。 |
| create(FileDescriptor fd, SourceOptions opts) | 從文件描述符創(chuàng)建圖像數(shù)據(jù)源。 |
| createIncrementalSource(SourceOptions opts) | 創(chuàng)建漸進式圖像數(shù)據(jù)源。 |
| createIncrementalSource(IncrementalSourceOptions opts) | 創(chuàng)建漸進式圖像數(shù)據(jù)源,支持設(shè)置漸進式數(shù)據(jù)更新模式。 |
| createPixelmap(DecodingOptions opts) | 從圖像數(shù)據(jù)源解碼并創(chuàng)建 PixelMap 圖像。 |
| createPixelmap(int index, DecodingOptions opts) | 從圖像數(shù)據(jù)源解碼并創(chuàng)建 PixelMap 圖像,如果圖像數(shù)據(jù)源支持多張圖片的話,支持指定圖像索引。 |
| updateData(byte[] data, boolean isFinal) | 更新漸進式圖像源數(shù)據(jù)。 |
| updateData(byte[] data, int offset, int length, boolean isFinal) | 更新漸進式圖像源數(shù)據(jù),支持設(shè)置輸入數(shù)據(jù)的有效數(shù)據(jù)范圍。 |
| getImageInfo() | 獲取圖像基本信息。 |
| getImageInfo(int index) | 根據(jù)特定的索引獲取圖像基本信息。 |
| getSourceInfo() | 獲取圖像源信息。 |
| release() | 釋放對象關(guān)聯(lián)的本地資源。 |
普通解碼開發(fā)步驟
- 創(chuàng)建圖像數(shù)據(jù)源 ImageSource 對象,可以通過 SourceOptions 指定數(shù)據(jù)源的格式信息,此格式信息僅為給解碼器的提示,正確提供能幫助提高解碼效率,如果不設(shè)置或設(shè)置不正確,會自動檢測正確的圖像格式。不使用該選項時,可以將 create 接口傳入的 SourceOptions 設(shè)置為 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);- 設(shè)置解碼參數(shù),解碼獲取 PixelMap 圖像對象,解碼過程中同時支持圖像處理操作。設(shè)置 desiredRegion 支持按矩形區(qū)域裁剪,如果設(shè)置為全 0,則不進行裁剪。設(shè)置 desiredSize 支持按尺寸縮放,如果設(shè)置為全 0,則不進行縮放。設(shè)置 rotateDegrees 支持旋轉(zhuǎn)角度,以圖像中心點順時針旋轉(zhuǎn)。如果只需要解碼原始圖像,不使用該選項時,可將給 createPixelMap 傳入的 DecodingOptions 設(shè)置為 null。
// 普通解碼疊加旋轉(zhuǎn)、縮放、裁剪
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);- 解碼完成獲取到 PixelMap 對象后,可以進行后續(xù)處理,比如渲染顯示等。
漸進式解碼開發(fā)步驟
- 創(chuàng)建漸進式圖像數(shù)據(jù)源 ImageSource 對象,可以通過 SourceOptions 指定數(shù)據(jù)源的格式信息,此格式信息僅為提示,如果填寫不正確,會自動檢測正確的圖像格式,使用 IncrementalSourceOptions 指定圖像數(shù)據(jù)的更新方式為漸進式更新。
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);- 漸進式更新數(shù)據(jù),在未獲取到全部圖像時,支持先更新部分數(shù)據(jù)來嘗試解碼,更新數(shù)據(jù)時設(shè)置 isFinal 為 false,當(dāng)獲取到全部數(shù)據(jù)后,最后一次更新數(shù)據(jù)時設(shè)置 isFinal 為 true,表示數(shù)據(jù)更新完畢。設(shè)置解碼參數(shù)同普通解碼。
// 獲取到一定的數(shù)據(jù)時嘗試解碼
imageSource.updateData(data, 0, bytes, false);
ImageSource.DecodingOptions decodingOpts = new ImageSource.DecodingOptions();
PixelMap pixelMap = imageSource.createPixelmap(decodingOpts);
// 更新數(shù)據(jù)再次解碼,重復(fù)調(diào)用直到數(shù)據(jù)全部更新完成
imageSource.updateData(data, 0, bytes, false);
PixelMap pixelMap = imageSource.createPixelmap(decodingOpts);
// 數(shù)據(jù)全部更新完成時需要傳入isFinal為true
imageSource.updateData(data, 0, bytes, true);
PixelMap pixelMap = imageSource.createPixelmap(decodingOpts);- 解碼完成獲取到 PixelMap 對象后,可以進行后續(xù)處理,比如渲染顯示等。
標題名稱:創(chuàng)新互聯(lián)鴻蒙OS教程:鴻蒙OS 圖像解碼開發(fā)指導(dǎo)
標題鏈接:http://www.dlmjj.cn/article/cdgojci.html


咨詢
建站咨詢
