編輯:關於 Android 程式設計
手機相機的解析度愈來愈高,一張照片動輒數 MB。若要把拍好的照片上傳到伺服器,在行動網路上不僅慢,还很耗流量。的解法有兩條路:一是拍照時直接取縮圖,二是把大圖壓縮。前者實作容易但清晰度明顯不足;後者能在維持可接受畫質的前提下大幅縮小檔案,是比較實用的做法。
壓縮的關鍵觀念是:不要先把整張大圖載入記憶體再處理,否則很容易 OOM。正確順序是「只讀尺寸 → 計算縮放倍率 → 縮圖解碼 → 依 Exif 校正角度 → 重新編碼」。
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; // 只讀邊界,不配置像素記憶體
BitmapFactory.decodeFile(filePath, options);
options.inSampleSize = calculateInSampleSize(options, 480, 800);
options.inJustDecodeBounds = false; // 這次真的解碼
Bitmap bm = BitmapFactory.decodeFile(filePath, options);
手機拍照時,方向資訊存在 Exif 裡而非像素中,若不處理,直式拍的照片上傳後會變成橫躺。
int degree = readPictureDegree(filePath);
bm = rotateBitmap(bm, degree);
private static int readPictureDegree(String path) {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(
ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90: degree = 90; break;
case ExifInterface.ORIENTATION_ROTATE_180: degree = 180; break;
case ExifInterface.ORIENTATION_ROTATE_270: degree = 270; break;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}
private static Bitmap rotateBitmap(Bitmap bitmap, int rotate) {
if (bitmap == null) return null;
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix mtx = new Matrix();
mtx.postRotate(rotate);
return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}
bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);
// 第二個參數是品質,100 為近乎不壓縮,30 代表壓掉約七成資料量
public static Bitmap getSmallBitmap(String filePath) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
options.inSampleSize = calculateInSampleSize(options, 480, 800);
options.inJustDecodeBounds = false;
Bitmap bm = BitmapFactory.decodeFile(filePath, options);
if (bm == null) return null;
bm = rotateBitmap(bm, readPictureDegree(filePath));
ByteArrayOutputStream baos = null;
try {
baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);
} finally {
try {
if (baos != null) baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return bm;
}
private 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 heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// 取較大者,確保縮完後的兩邊尺寸都不小於需求
inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;
}
return inSampleSize;
}
inSampleSize 只接受整數,且解碼時會以 2 的次方倍數縮減,因此它決定的是「解析度」,而 compress 的品質參數決定的是「編碼後的位元率」。兩者搭配使用,才能在清晰度與檔案大小之間取得平衡。
recycle()(4.4 以前效果明顯)。compress 寫入 ByteArrayOutputStream 會在記憶體中再存一份完整資料,檔案很大時建議直接寫進檔案串流。
Android 線程與 UI 更新:主線程與工作線程實戰
Android 的線程模型可以簡單理解為:Main Thread 負責使用者介面,Worker Thread 負責耗時運算。絕大多數 UI 操作都必須在主線程完成,這是撰
Android 刮刮樂效果實作:用 Canvas 與 PorterDuff 模式做塗開式遮罩
刮刮樂的核心原理其實非常單純:在獎項上方蓋一層不透明的「銀漆」,使用者手指劃過的地方就把這層漆擦掉。換成 Android 的語言,就是「一張可繪圖的 Bitmap 當作遮
Android-PullToRefresh 下拉刷新庫的基本用法
PullToRefresh 是一套設計相當成熟的下拉刷新庫,支援的 View 類型很廣: ListView ExpandableListView GridView Web
Android 圖像處理:黑白濾鏡實作
黑白濾鏡是最經典的圖像效果之一。它會去除色彩資訊,只保留明暗層次,使照片呈現出紀錄片、街拍或復古影像般的氛圍。在 Android 中,這類效果同樣可以透過 Bitmap