標籤:des android style http io ar os 使用 sp
Bitmap是Android系統中的影像處理的最重要類之一。用它可以擷取影像檔資訊,進行映像剪下、旋轉、縮放等操作,並可以指定格式儲存影像檔。本文從應用的角度,著重介紹怎麼用Bitmap來實現這些功能。
一、Bitmap的產生
1.1 BitmapFactory decode出Bitmap
Bitmap實現在android.graphics包中。但是Bitmap類的建構函式是私人的,外面並不能執行個體化,只能是通過JNI執行個體化。這必然是 某個輔助類提供了建立Bitmap的介面,而這個類的實現通過JNI介面來執行個體化Bitmap的,這個類就是BitmapFactory。
圖一、BitmapFactory主要方法及Options選項
利用BitmapFactory可以從一個指定檔案中,利用decodeFile()解出Bitmap;也可以定義的圖片資源中,利用decodeResource()解出Bitmap。
1.2 decode時的選項
在使用方法decodeFile()/decodeResource()時,都可以指定一個BitmapFacotry.Options。
利用Options的下列屬性,可以指定decode的選項:
inPreferredConfig 指定decode到記憶體中,手機中所採用的編碼,可選值定義在Bitmap.Config中。預設值是ARGB_8888。
inJustDecodeBounds 如果設定為true,並不會把映像的資料完全解碼,亦即decodeXyz()傳回值為null,但是Options的outAbc中解出了映像的基本資料。
inSampleSize 設定decode時的縮放比例。
利用Options的這些值就可以高效的得到一幅縮圖。
圖二、BitmapFactory.decodeFile()
先設定inJustDecodeBounds= true,調用decodeFile()得到映像的基本資料[Step#2~4];
利用映像的寬度(或者高度,或綜合)以及目標的寬度,得到inSampleSize值,再設定inJustDecodeBounds= false,調用decodeFile()得到完整的映像資料[Step#5~8]。
先擷取比例,再讀入資料,如果欲讀入大比例縮小的圖,將顯著的節約內容資源。有時候還會讀入大量的縮圖,這效果就更明顯了。
二、利用Bitmap和Matrix實現映像變換
Bitmap可以和Matrix結合實現映像的剪下、旋轉、縮放等操作。
圖三、Bitmap方法
用源Bitmap通過變換產生新的Bitmap的方法:
?
| 1 2 3 4 5 |
public static Bitmap createBitmap(Bitmap source, int x, int y, intwidth, int height, Matrix m, boolean filter) public static Bitmap createBitmap(Bitmap source, int x, int y, intwidth, int height) public static Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter) |
第一個方法是最終的實現,後兩種只是對第一種方法的封裝。
第二個方法可以從源Bitmap中指定地區(x,y, width, height)中挖出一塊來實現剪下;第三個方法可以把源Bitmap縮放為dstWidth x dstHeight的Bitmap。
設定Matrix的Rotate(通過setRotate())或者Scale(通過setScale()),傳入第一個方法,可實現旋轉或縮放。
圖四、Bitmap實現旋轉
三、儲存影像檔
經過映像變換之後的Bitmap裡的資料可以儲存到映像壓縮檔裡(JPG/PNG)。
圖五、儲存Bitmap資料到檔案
這個操作過程中,Bitmap.compress()方法的參數format可設定JPEG或PNG格式;quality可選擇壓縮品質;fOut是輸出資料流(OutputStream),這裡的FileOutputStream是OutputStream的一個子類。
總結一下,本文介紹Bitmap的使用方法——用Bitmap實現影像檔的讀取和寫入,並用Bitmap實現映像的剪下、旋轉和縮放變換。
下面主要介紹載入位元影像的5中方式:
1 .【載入位元影像】通過檔案路徑載入位元影像,顯示原圖,大小比例不變
Bitmap bmp=BitmapFactory.decodeFile("/mnt/sdcard/dog.jpg");
imageView.setImageBitmap(bmp);
2.【載入位元影像】通過檔案路徑載入位元影像,(若scale 值為n>1) 則圖片長,寬變為原來的1/n,相當於把圖片壓縮到原來的1/(n*n),載入到手機記憶體佔用的空間小,我們可以再xml檔案中設定ImageView的scaleType=fitCenter屬性, 進行展開自適應操作,展開後的映像不是很清楚,但還是可以接受的。
Options options=new Options();
options.inSampleSize=2;
Bitmap bm=BitmapFactory.decodeFile("/mnt/sdcard/dog.jpg", options);
imageView.setImageBitmap(bm);
3.【載入位元影像】通過檔案路徑載入位元影像,此種方式,可以把原圖縮小,或放大。下面例子是先壓縮再放大。
Options opts=new Options(); //設定僅載入位元影像邊界資訊(相當於位元影像的資訊,但沒有載入位元影像)
opts.inJustDecodeBounds=true; //載入指定路徑圖片的邊界資訊,儲存到opts中
BitmapFactory.decodeFile("/mnt/sdcard/dog.jpg", opts); //計算收縮比例
int xScale=opts.outWidth/200;
int yScale=opts.outHeight/200;
opts.inSampleSize=xScale>yScale?xScale:yScale; //設定載入邊界資訊為false
opts.inJustDecodeBounds=false;
Bitmap bm=BitmapFactory.decodeFile("/mnt/sdcard/dog.jpg", opts);
imageView.setImageBitmap(bm);
4.【載入位元影像】通過輸入資料流載入位元影像
FileInputStream is;
try {
is = new FileInputStream("/mnt/sdcard/dog.jpg");
Bitmap bm=BitmapFactory.decodeStream(is);
imageView.setImageBitmap(bm);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
5.【載入位元影像】通過位元組數組載入位元影像,圖片長,寬減半;
ByteArrayOutputStream out;
try {
FileInputStream fis=new FileInputStream("/mnt/sdcard/dog.jpg");
BufferedInputStream bis=new BufferedInputStream(fis);
out = new ByteArrayOutputStream();
int hasRead=0;
byte[] buffer=new byte[1024*2];
while((hasRead=bis.read(buffer))>0){
//讀出多少資料,向輸出資料流中寫入多少
out.write(buffer);
out.flush();
}
out.close();
fis.close();
bis.close();
byte[] data=out.toByteArray();
//長寬減半
Options opts=new Options();
opts.inSampleSize=2;
Bitmap bm=BitmapFactory.decodeByteArray(data, 0, data.length, opts);
imageView.setImageBitmap(bm);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace(); } } }
Android圖形處理之Bitmap