最近會用上這也是因為在拍照時需要做些處理,拍完原始檔太大要傳好久光想就很麻煩,最後決定將檔案縮小再傳是比較適當點,剛拍完的照片儲存方式為Byte Array,但作些圖像處理時大多需要轉換為Bitmap比較方便。
Byte Array to Bitmap
buteArrayName就是這byte array的名稱
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArrayName, 0, byteArrayName.length);
Bitmap to Byte Array
其中"bitmap"就是Bitmap的變數名稱,90就是這照片的品質
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
byte[] array= out.toByteArray();
Rotate
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
//set rotate
int w = bitmap.getWidth();
int h = bitmap.getHeight();
// Setting post rotate to 90
Matrix mtx = new Matrix();
mtx.postRotate(90);
// Rotating Bitmap
Bitmap rotatedBMP = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
//BitmapDrawable bmd = new BitmapDrawable(rotatedBMP); //如果要在畫面中顯示才需要
resize
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
//set rotate
int w = bitmap.getWidth();
int h = bitmap.getHeight();
//set final size
int destWidth = 480;
int destHeigth = 800;
//set scale size
float scaleWidth = ((float) destWidth) / w;
float scaleHeight = ((float) destHeigth) / h;
//set resize
Matrix mtx = new Matrix();
mtx.postScale(scaleWidth, scaleHeight);
// Rotating Bitmap
Bitmap rotatedBMP = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
//BitmapDrawable bmd = new BitmapDrawable(rotatedBMP); //如果要在畫面中顯示才需要
相關連結
Android Developer
Android Developer - Bitmap
Android Developer - BitmapDrawable
Android Developer - ByteArrayOutputStream
Android Developer - BitmapFactory
Android Byte Array to Bitmap rotate & resize 圖片旋轉、縮放
訂閱:
張貼留言 (Atom)
很實用的文章,請問可以轉貼留紀錄嗎?
回覆刪除@zw
回覆刪除歡迎轉載