圖像開發(fā)概述
HarmonyOS圖像模塊支持圖像業(yè)務(wù)的開發(fā),常見功能如圖像解碼、圖像編碼、基本的位圖操作、圖像編輯等。當(dāng)然,也支持通過接口組合來實(shí)現(xiàn)更復(fù)雜的圖像處理邏輯。
基本概念
-
圖像解碼
圖像解碼就是不同的存檔格式圖片(如JPEG、PNG等)解碼為無壓縮的位圖格式,以方便在應(yīng)用或者系統(tǒng)中進(jìn)行相應(yīng)的處理。
-
PixelMap
PixelMap是圖像解碼后無壓縮的位圖格式,用于圖像顯示或者進(jìn)一步的處理。
-
漸進(jìn)式解碼
漸進(jìn)式解碼是在無法一次性提供完整圖像文件數(shù)據(jù)的場景下,隨著圖像文件數(shù)據(jù)的逐步增加,通過多次增量解碼逐步完成圖像解碼的模式。
-
預(yù)乘
預(yù)乘時,RGB各通道的值被替換為原始值乘以Alpha通道不透明的比例(0~1)后的值,方便后期直接合成疊加;不預(yù)乘指RGB各通道的數(shù)值是圖像的原始值,與Alpha通道的值無關(guān)。
-
圖像編碼
圖像編碼就是將無壓縮的位圖格式,編碼成不同格式的存檔格式圖片(JPEG、PNG等),以方便在應(yīng)用或者系統(tǒng)中進(jìn)行相應(yīng)的處理。
約束與限制
為及時釋放本地資源,建議在圖像解碼的ImageSource對象、位圖圖像PixelMap對象或圖像編碼的ImagePacker對象使用完成后,主動調(diào)用ImageSource、PixelMap和ImagePacker的release()方法。
圖像解碼開發(fā)
場景介紹
圖像解碼就是將所支持格式的存檔圖片解碼成統(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)建漸進(jìn)式圖像數(shù)據(jù)源。 |
createIncrementalSource(IncrementalSourceOptions opts) |
創(chuàng)建漸進(jìn)式圖像數(shù)據(jù)源,支持設(shè)置漸進(jìn)式數(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) |
更新漸進(jìn)式圖像源數(shù)據(jù)。 |
updateData(byte[] data, int offset, int length, boolean isFinal) |
更新漸進(jìn)式圖像源數(shù)據(jù),支持設(shè)置輸入數(shù)據(jù)的有效數(shù)據(jù)范圍。 |
getImageInfo() |
獲取圖像基本信息。 |
getImageInfo(int index) |
根據(jù)特定的索引獲取圖像基本信息。 |
getSourceInfo() |
獲取圖像源信息。 |
release() |
釋放對象關(guān)聯(lián)的本地資源。 |
普通解碼開發(fā)步驟
1. 創(chuàng)建圖像數(shù)據(jù)源ImageSource對象,可以通過SourceOptions指定數(shù)據(jù)源的格式信息,此格式信息僅為給解碼器的提示,正確提供能幫助提高解碼效率,如果不設(shè)置或設(shè)置不正確,會自動檢測正確的圖像格式。不使用該選項(xiàng)時,可以將create接口傳入的SourceOptions設(shè)置為null。
ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions();
srcOpts.formatHint = "image/png";
// 此處傳入用戶自定義的圖像路徑
String pathName = "/sdcard/image.png";
ImageSource imageSource = ImageSource.create(pathName, srcOpts);
// 不通過SourceOptions指定數(shù)據(jù)源格式信息
ImageSource imageSourceNoOptions = ImageSource.create(pathName, null);
2. 設(shè)置解碼參數(shù),解碼獲取PixelMap圖像對象,解碼過程中同時支持圖像處理操作。
- 設(shè)置desiredSize支持按尺寸縮放,如果設(shè)置為全0,則不進(jìn)行縮放。
- 設(shè)置desiredRegion支持按矩形區(qū)域裁剪,如果設(shè)置為全0,則不進(jìn)行裁剪。
- 設(shè)置rotateDegrees支持旋轉(zhuǎn)角度,以圖像中心點(diǎn)順時針旋轉(zhuǎn)。
如果只需要解碼原始圖像,不使用該選項(xiàng)時,可將給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);
3. 解碼完成獲取到PixelMap對象后,可以進(jìn)行后續(xù)處理,比如渲染顯示等。
漸進(jìn)式解碼開發(fā)步驟
1. 創(chuàng)建漸進(jìn)式圖像數(shù)據(jù)源ImageSource對象。
可以通過SourceOptions指定數(shù)據(jù)源的格式信息,此格式信息僅為提示,如果填寫不正確,會自動檢測正確的圖像格式。使用IncrementalSourceOptions指定圖像數(shù)據(jù)的更新方式為漸進(jìn)式更新。
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 = ImageSource.createIncrementalSource(incOpts);
2. 漸進(jìn)式更新數(shù)據(jù)。在未獲取到全部圖像時,支持先更新部分?jǐn)?shù)據(jù)來嘗試解碼,調(diào)用updateData更新數(shù)據(jù),將參數(shù)isFinal設(shè)置為false;當(dāng)獲取到全部數(shù)據(jù)后,最后一次更新數(shù)據(jù)時設(shè)置isFinal為true,表示數(shù)據(jù)更新完畢。設(shè)置解碼參數(shù)同普通解碼。文章來源:http://www.zghlxwxcb.cn/news/detail-503125.html
// 傳入本地圖片路徑作為圖像數(shù)據(jù)源
File file = new File("/sdcard/test.jpg");
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len = -1;
while((len = fis.read(data)) != -1) {
bos.write(data, 0, len);
}
byte[] fileByte = bos.toByteArray();
// 獲取到一定的數(shù)據(jù)時嘗試解碼
imageSource.updateData(fileByte, 0, fileByte.length / 2, false);
ImageSource.DecodingOptions decodingOpts = new ImageSource.DecodingOptions();
PixelMap pixelMap = imageSource.createPixelmap(decodingOpts);
// 更新數(shù)據(jù)再次解碼,重復(fù)調(diào)用直到數(shù)據(jù)全部更新完成
imageSource.updateData(fileByte, fileByte.length / 2, fileByte.length / 4, false);
pixelMap = imageSource.createPixelmap(decodingOpts);
// 數(shù)據(jù)全部更新完成時需要傳入isFinal為true
imageSource.updateData(fileByte, fileByte.length * 3 / 4, fileByte.length / 4, true);
pixelMap = imageSource.createPixelmap(decodingOpts);
} catch (IOException e) {
e.printStackTrace();
}
3. 解碼完成獲取到PixelMap對象后,可以進(jìn)行后續(xù)處理,比如渲染顯示等。文章來源地址http://www.zghlxwxcb.cn/news/detail-503125.html
到了這里,關(guān)于HarmonyOS學(xué)習(xí)路之開發(fā)篇—多媒體開發(fā)(圖像開發(fā) 一)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!