Bitmap類及BitmapFactory類中的常用方法

http://blog.csdn.net/coderyue/article/details/50103639#t9      Bitmap總結

https://www.cnblogs.com/wangjiaghe/p/7358445.html

http://blog.csdn.net/zxw136511485/article/details/51957962

http://blog.csdn.net/u012637501/article/details/40832893

1.assets目錄下資源加載  

如果圖片保存到assest目錄下,知道圖片的名稱,則可以通過inputstream獲得圖片Drawable或者 Bitmap
AssetManager asm=getAssets();
        InputStream is;
        try {
            is = asm.open("smiley_smile.png");
            //獲得Drawable
            Drawable da = Drawable.createFromStream(is, null);
            //獲得Bitmap
            Bitmap bitmap=BitmapFactory.decodeStream(is);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

2、圖片保存在sdcard,通過圖片的路徑加載,可以使用BitmapFactory的方法加載,舉個例子

// 圖片路徑
        String imgFilePath = Environment.getExternalStorageDirectory()
                .toString() + "/smiley_smile.png";
        try {
            // 獲得文件輸入流
            FileInputStream fis = new FileInputStream(new File(imgFilePath));
            Bitmap bitmap = BitmapFactory.decodeStream(fis);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Bitmap.createBitmap(bitmap, x, y, pieceWidth, pieceWidth)                          以X,Y座標(左上角)爲起點,而寬與高則是width與height(右下角 )開始截圖    

BitmapFactory類提供了幾個解析方法,每種解碼方法都可以讓你通過BitmapFactory.Options設置標籤。設置inJustDecodeBounds 爲true,解碼的時候會避免內存的分配,返回的bitmap對象爲空,但是卻可以得到 outWidth, outHeight 和outMimeType。


BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;

爲了避免 java.lang.OutOfMemory 異常,在解析前先覈對Bitmap的尺寸,除非你有絕對的把握你的圖片不會造成內存溢出。

是否加載一個按比例縮小的版本到內存中,下面幾點可以考慮:

1.估算加載完整圖片使用的內存大小。
2.加載了完整圖片,給應用其他的內存需求大小
3.目標Imageview尺寸或者加載圖片的UI組建大小
4.當前設備屏幕尺寸或者大小
5.告訴解碼器對Image進行採樣,加載一個較小版本圖片到內存中,設置BitmapFactory.Options 對象inSampleSize爲true。比如一個2048*1536密度的圖片,inSampleSize爲4生成的樣品圖就是512*384。加載這個樣品需要內存0.75MB,而不是原來的12MB。下面提供了方法計算樣本大小。

把任意尺寸圖顯示成100*100的縮略圖:
Bitmap bitmap = decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));
第一步設置inJustDecodeBounds = true,然後通過新的inSampleSize解碼,最後設置inJustDecodeBounds = false。
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int reqHeight) {

    // 第一步inJustDecodeBounds=true 檢查尺寸大小
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // 計算樣本尺寸
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // 進行採樣
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // 原始圖片大小
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

       //計算最大的壓縮比例值都是2的冪次方,而且寬和高大於被要求  
       //的寬和高。           
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

WeakReference<ImageView> imageViewReference = new WeakReference<ImageView>(imageView);// 使用弱引用,確保可以被回收

imageViewReference.get();    //弱引用是爲了確保能被回收,無法保證當任務完成的時候ImageView還在,所以需要覈對。

1.Bitmap

  • public void recycle()——回收位圖佔用的內存空間,把位圖標記爲Dead
  • public final boolean isRecycled() ——判斷位圖內存是否已釋放
  • public final int getWidth()——獲取位圖的寬度
  • public final int getHeight()——獲取位圖的高度
  • public final boolean isMutable()——圖片是否可修改
  • public int getScaledWidth(Canvas canvas)——獲取指定密度轉換後的圖像的寬度
  • public int getScaledHeight(Canvas canvas)——獲取指定密度轉換後的圖像的高度
  • public boolean compress(CompressFormat format, int quality, - - --OutputStream stream)——按指定的圖片格式以及畫質,將圖片轉換爲輸出流。
  • format:Bitmap.CompressFormat.PNG或Bitmap.CompressFormat.JPEG quality:畫質,0-100.0表示最低畫質壓縮,100以最高畫質壓縮。對於PNG等無損格式的圖片,會忽略此項設置

2.BitmapFactory

2.1 BitmapFactory創建Bitmap的幾種方法說明

1.publicstatic Bitmap decodeByteArray (byte[] data,int offset, int length, BitmapFactory.Options opts)
參數
data    壓縮圖像數據的字節數組
offset  圖像數據偏移量,用於解碼器開始從哪兒解析.
length  字節數,以偏移量開始,去解析
opts    可以爲空,控制採樣率和實付圖像完全被解碼的選項,或者僅僅返回大小
返回值
    返回解碼後的位圖,或者如果圖像數據不能被解碼返回爲空,或者 如果選項不是空,如果選項要求僅僅返回大小(opts.outWidth and opts.outHeight)。
從指定的字節數組中解碼一個不可變的位圖。

 

2. public static Bitmap decodeByteArray (byte[] data, int offset, int length)
參數
data 壓縮圖像數據的字節數組
offset 圖像數據偏移量,用於解碼器開始從哪兒解析.
length 字節數,以偏移量開始,去解析
返回值
返回解碼後的位圖,或者如果圖像數據不能被解碼返回爲空
從指定的字節數組中解碼一個不可變的位圖。

 

3.public staticBitmap decodeFile (String pathName)
參數
pathName    解碼文件的全路徑名
返回值
    返回結果是解碼的位圖,或者如果不能解碼則返回空。
從文件中解碼生成一個位圖。如果支付的文件名爲空,或者不能解碼出一個位圖,方法將返回空。

 

4.public staticBitmap decodeFile (String pathName, BitmapFactory.Options opts)
參數
pathName    解碼文件的全路徑名
opts            可以爲空,控制採樣率和實付圖像完全被解碼的選項,或者僅僅返回大小
Returns
    返回解碼後的位圖,或者如果圖像數據不能被解碼返回爲空,或者 如果選項不是空,如果選項要求僅僅返回大小(opts.outWidth and opts.outHeight)。
從文件中解碼生成一個位圖。如果支付的文件名爲空,或者不能解碼出一個位圖,方法將返回空。

 

5.public staticBitmap decodeFileDescriptor (FileDescriptor fd)
參數
fd  包含解碼位圖數據的文件路徑
返回值
   返回解碼的位圖或者空。

 

6.public staticBitmap decodeFileDescriptor (FileDescriptor fd, Rect outPadding, BitmapFactory.Options opts)
參數
fd          包含解碼位圖數據的<span style="font-family: Arial, Helvetica, sans-serif;">文件路徑</span>
outPadding  如果不爲空,返回矩形的內邊距如果位圖存在,否則設置內邊距爲(-1,-1,-1,-1).如果沒有位圖返回空,內邊距不改變
opts            可以爲空,控制採樣率和實付圖像完全被解碼的選項,或者僅僅返回大小
返回值
    返回解碼的位圖或者空。

 

7.public staticBitmap decodeResource (Resources res, intid, BitmapFactory.Options opts)
參數
res     包含圖像數據的資源對象
id  圖像數據的資源的id
opts    可以爲空,控制採樣率和實付圖像完全被解碼的選項,或者僅僅返回大小
返回值

 

8.public staticBitmap decodeResourceStream (Resources res, TypedValue value, InputStream is, Rect pad, BitmapFactory.Options opts)
從輸入流中解碼一個新位圖。輸入了獲得資源,我們可以縮放位圖。

 

9.public staticBitmap decodeResource (Resources res, intid)
參數
res         包含圖像數據的資源對象
id      圖像數據的資源的id
返回值
    返回解碼後的位圖,或者如果圖像數據不能被解碼返回爲空
從輸入流中解碼位圖。與decodeResource(Resources,int, android.graphics.BitmapFactory.Options)當Options是空時同義,

 

10.public staticBitmap decodeResource (Resources res, intid, BitmapFactory.Options opts)
參數
res     包含圖像數據的資源對象
id  圖像數據的資源的id
opts    可以爲空,控制採樣率和實付圖像完全被解碼的選項,或者僅僅返回大小
返回值
   返回解碼後的位圖,或者如果圖像數據不能被解碼返回爲空,或者 如果選項不是空,如果選項要求僅僅返回大小(opts.outWidth and opts.outHeight)。
從資源中解碼一個位圖。與decodeResourceStream(Resources, TypedValue, InputStream, Rect, BitmapFactory.Options)同義.

 

11.public staticBitmap decodeStream (InputStream is, Rect outPadding, BitmapFactory.Options opts)
參數
is          持有原始數據用於解碼位圖的輸入流
outPadding  如果不爲空,返回矩形的內邊距如果位圖存在,否則設置內邊距爲(-1,-1,-1,-1).如果沒有位圖返回空,內邊距不改變
opts            可以爲空,控制採樣率和實付圖像完全被解碼的選項,或者僅僅返回大小
Returns
    返回解碼後的位圖,或者如果圖像數據不能被解碼返回爲空,或者 如果選項不是空,如果選項要求僅僅返回大小(opts.outWidth and opts.outHeight)。
從輸入流中解碼一個位圖。如果輸入了爲空,或者不能解碼位圖,方法返回空。流的位置覺得解碼數據從哪兒讀取。

 

12.public staticBitmap decodeStream (InputStream is)
Decode an input stream into a bitmap. If the input stream isnull, or cannot be used to decode a bitmap, the function returnsnull. The stream's position will be where ever it was after the encoded data was read.
參數
is  持有原始數據用於解碼位圖的輸入流
返回值
    返回解碼後的位圖,或者如果圖像數據不能被解碼返回爲空

2.2 BitmapFactory.Options說明以及使用

位圖在內存中的佔用空間計算:
ALPHA_8:每個像素佔用1byte內存
ARGB_4444:每個像素佔用2byte內存
ARGB_8888:每個像素佔用4byte內存
RGB_565:每個像素佔用2byte內存
舉個例子,如果一個圖片的分辨率是1024*768,採用ARGB_8888,那麼佔用的空間就是1024*768*4=3MB

 

通過BitmapFactory.Options降低bitmap加載到內存中的內存及改變色彩設置防止OOM

BitmapFactory.Options的常用參數

inJustDecodeBounds:

如果將這個值置爲true,那麼在解碼的時候將不會返回bitmap,只會返回這個bitmap的尺寸。這個屬性的目的是,如果你只想知道一個bitmap的尺寸,但又不想將其加載到內存時。這是一個非常有用的屬性。

inSampleSize:

縮放比例。當它小於1的時候,將會被當做1處理,這個參數需要是2的冪函數。例如,width=100,height=100,inSampleSize=2,那麼就會將bitmap處理爲,width=50,height=50,寬高各降爲1 / 2,像素數降爲1 / 4。

inPreferredConfig:

這個值是設置色彩模式,默認值是ARGB_8888,在這個模式下,一個像素點佔用4bytes空間,一般對透明度不做要求的話,一般採用RGB_565模式,這個模式下一個像素點佔用2bytes。

outWidth和outHeight:

表示這個Bitmap的寬和高,一般和inJustDecodeBounds一起使用來獲得Bitmap的寬高,但是不加載到內存。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章