Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 圖片處理工具類:Bitmap、位元組與 Drawable 的互轉與縮放

編輯:關於 Android 程式設計

在 Android 開發中,圖片處理幾乎是每個專案都會遇到的環節。無論是從網路載入圖片、在清單中顯示縮圖,還是在記憶體與磁碟之間轉換格式,我們往往需要一套好用的工具方法來簡化操作。這篇文章整理了一個名為 ImageUtils 的圖片工具類,主要負責在 Bitmap、位元組陣列(byte array)與 Drawable 之間互相轉換,同時提供圖片縮放的能力。

工具類提供的核心功能

這個工具類把常用的圖片操作都封裝成了靜態方法,呼叫起來相當直覺:

  • bitmapToByte(Bitmap b):將 Bitmap 轉換為位元組陣列
  • byteToBitmap(byte[] b):將位元組陣列還原為 Bitmap
  • bitmapToDrawable(Bitmap b):將 Bitmap 包裝成 Drawable
  • drawableToBitmap(Drawable d):將 Drawable 還原為 Bitmap
  • drawableToByte(Drawable d):將 Drawable 直接轉為位元組陣列
  • byteToDrawable(byte[] b):將位元組陣列轉為 Drawable
  • scaleImage(Bitmap org, float scaleWidth, float scaleHeight):按比例縮放圖片
  • scaleImageTo(Bitmap org, int newWidth, int newHeight):縮放到指定的寬高

除了轉換與縮放,這個類還能透過網路位址直接取得圖片,例如 getInputStreamFromUrl、getBitmapFromUrl 與 getDrawableFromUrl,讓載入遠端資源的流程變得相當省事。

完整原始碼

以下為 ImageUtils 的完整實作,包含所有轉換、縮放與網路載入的方法:

package cn.trinea.android.common.util; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.Map; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; /** * ImageUtils * @author Trinea 2012-6-27 */ public class ImageUtils { private ImageUtils() { throw new AssertionError(); } /** convert Bitmap to byte array */ public static byte[] bitmapToByte(Bitmap b) { if (b == null) { return null; } ByteArrayOutputStream o = new ByteArrayOutputStream(); b.compress(Bitmap.CompressFormat.PNG, 100, o); return o.toByteArray(); } /** convert byte array to Bitmap */ public static Bitmap byteToBitmap(byte[] b) { return (b == null || b.length == 0) ? null : BitmapFactory.decodeByteArray(b, 0, b.length); } /** convert Drawable to Bitmap */ public static Bitmap drawableToBitmap(Drawable d) { return d == null ? null : ((BitmapDrawable)d).getBitmap(); } /** convert Bitmap to Drawable */ public static Drawable bitmapToDrawable(Bitmap b) { return b == null ? null : new BitmapDrawable(b); } /** convert Drawable to byte array */ public static byte[] drawableToByte(Drawable d) { return bitmapToByte(drawableToBitmap(d)); } /** convert byte array to Drawable */ public static Drawable byteToDrawable(byte[] b) { return bitmapToDrawable(byteToBitmap(b)); } /** get input stream from network by imageurl */ public static InputStream getInputStreamFromUrl(String imageUrl, int readTimeOutMillis) { return getInputStreamFromUrl(imageUrl, readTimeOutMillis, null); } public static InputStream getInputStreamFromUrl(String imageUrl, int readTimeOutMillis, Map requestProperties) { InputStream stream = null; try { URL url = new URL(imageUrl); HttpURLConnection con = (HttpURLConnection)url.openConnection(); HttpUtils.setURLConnection(requestProperties, con); if (readTimeOutMillis > 0) { con.setReadTimeout(readTimeOutMillis); } stream = con.getInputStream(); } catch (MalformedURLException e) { closeInputStream(stream); throw new RuntimeException("MalformedURLException occurred. ", e); } catch (IOException e) { closeInputStream(stream); throw new RuntimeException("IOException occurred. ", e); } return stream; } /** get drawable by imageUrl */ public static Drawable getDrawableFromUrl(String imageUrl, int readTimeOutMillis) { return getDrawableFromUrl(imageUrl, readTimeOutMillis, null); } public static Drawable getDrawableFromUrl(String imageUrl, int readTimeOutMillis, Map requestProperties) { InputStream stream = getInputStreamFromUrl(imageUrl, readTimeOutMillis, requestProperties); Drawable d = Drawable.createFromStream(stream, "src"); closeInputStream(stream); return d; } /** get Bitmap by imageUrl */ public static Bitmap getBitmapFromUrl(String imageUrl, int readTimeOut) { return getBitmapFromUrl(imageUrl, readTimeOut, null); } public static Bitmap getBitmapFromUrl(String imageUrl, int readTimeOut, Map requestProperties) { InputStream stream = getInputStreamFromUrl(imageUrl, readTimeOut, requestProperties); Bitmap b = BitmapFactory.decodeStream(stream); closeInputStream(stream); return b; } /** scale image to specific size */ public static Bitmap scaleImageTo(Bitmap org, int newWidth, int newHeight) { return scaleImage(org, (float)newWidth / org.getWidth(), (float)newHeight / org.getHeight()); } /** scale image by ratio */ public static Bitmap scaleImage(Bitmap org, float scaleWidth, float scaleHeight) { if (org == null) { return null; } Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); return Bitmap.createBitmap(org, 0, 0, org.getWidth(), org.getHeight(), matrix, true); } /** close inputStream */ private static void closeInputStream(InputStream s) { if (s == null) { return; } try { s.close(); } catch (IOException e) { throw new RuntimeException("IOException occurred. ", e); } } }

使用提醒

這個工具類的設計相對輕量,適合用在中小型專案裡快速處理圖片的格式轉換與縮放。縮放方法透過 Matrix 的 postScale 完成,並啟用了 filter 參數以獲得較平滑的縮圖效果。而在網路載入部分,由於涉及 I/O 操作,實際使用時仍建議放到非主執行緒中執行,避免造成畫面卡頓。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved