編輯:Android資訊
Android圖片壓縮無非兩種,一種質量壓縮,一種像素壓縮,前者多用於圖片上傳時,後者多用於本地圖片展示縮略圖時。
對於質量壓縮,主要用到的一個方法就是:
public boolean compress(CompressFormat format, int quality, OutputStream stream) {}
這是Bitmap類裡的一個方法,第一個參數表示圖片壓縮的格式,android中提供了以下格式:
public enum CompressFormat {
JPEG (0),
PNG (1),
WEBP (2);
CompressFormat(int nativeInt) {
this.nativeInt = nativeInt;
}
final int nativeInt;
}
第二個參數表示壓縮的質量,注意這個是壓縮的關鍵,它的取值是0到100,越小表示壓縮的越厲害,第三個參數表示把壓縮的數據寫入了outputstream流中。
OK,來看一個例子:
public byte [] compressBitmap(Bitmap bitmap,int max){
int quality = 100;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,quality,byteArrayOutputStream);
while (byteArrayOutputStream.toByteArray().length / 1024 > max){
byteArrayOutputStream.reset();
quality = quality -10;
bitmap.compress(Bitmap.CompressFormat.JPEG,quality,byteArrayOutputStream);
}
return byteArrayOutputStream.toByteArray();
}
這個方法的第一個參數不必解釋,第二個參數表示你要求的壓縮後圖片最大可以是多少。最後可以拿到一個byte數組。我們有了這個byte數組就可以轉化為file或者bitmap。
注意這種質量壓縮後,像素本身沒有改變。
對於像素壓縮,顧名思義就是壓縮像素。這裡用到的一個主要的方法就是:
public static Bitmap decodeFile(String pathName, Options opts) {
Bitmap bm = null;
InputStream stream = null;
try {
stream = new FileInputStream(pathName);
bm = decodeStream(stream, null, opts);
} catch (Exception e) {
/* do nothing.
If the exception happened on open, bm will be null.
*/
Log.e("BitmapFactory", "Unable to decode stream: " + e);
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
// do nothing here
}
}
}
return bm;
}
這是BitmapFactory中的一個靜態方法,第一個參數表示file的全路徑,第二個參數是關鍵,Options是BitmapFactory類中的一個靜態內部類,它有兩個非常重要的屬性:
/** * If set to true, the decoder will return null (no bitmap), but * the out... fields will still be set, allowing the caller to query * the bitmap without having to allocate the memory for its pixels. */ public boolean inJustDecodeBounds; /** * If set to a value > 1, requests the decoder to subsample the original * image, returning a smaller image to save memory. The sample size is * the number of pixels in either dimension that correspond to a single * pixel in the decoded bitmap. For example, inSampleSize == 4 returns * an image that is 1/4 the width/height of the original, and 1/16 the * number of pixels. Any value <= 1 is treated the same as 1. Note: the * decoder uses a final value based on powers of 2, any other value will * be rounded down to the nearest power of 2. */ public int inSampleSize;
第一個屬性inJustDecodeBounds,如果設置為ture,則返回null。
第二個屬性inSampleSize表示縮放比例,大於1表示縮小了原來的多少,比如inSampleSize == 4,就表示縮小了原來的四分之一,如果小於1則和1相同。
好了來看看這個網上遍地都是的一個像素壓縮的方法:
private Bitmap getimage(String srcPath) {
BitmapFactory.Options newOpts = new BitmapFactory.Options();
//開始讀入圖片,此時把options.inJustDecodeBounds 設回true了
newOpts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(srcPath,newOpts);//此時返回bm為空
newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
//現在主流手機比較多是800*480分辨率,所以高和寬我們設置為
float hh = 800f;//這裡設置高度為800f
float ww = 480f;//這裡設置寬度為480f
//縮放比。由於是固定比例縮放,只用高或者寬其中一個數據進行計算即可
int be = 1;//be=1表示不縮放
if (w > h && w > ww) {//如果寬度大的話根據寬度固定大小縮放
be = (int) (newOpts.outWidth / ww);
} else if (w < h && h > hh) {//如果高度高的話根據寬度固定大小縮放
be = (int) (newOpts.outHeight / hh);
}
if (be <= 0)
be = 1;
newOpts.inSampleSize = be;//設置縮放比例
//重新讀入圖片,注意此時已經把options.inJustDecodeBounds 設回false了
bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
return bitmap;
}
是不是就很好理解了。
如何優化 Android Studio 啟動、編譯和運行速度?
作為一名 Android 程序員,選擇一個好的 IDE 工具可以使開發變得非常高效,很多程序員喜歡使用 Google 的 Android Studio來進行開發,
Android的官司打贏了 屬於Android社區的勝利
BI 中文站 5 月 27 日報道 美國陪審團裁決剛剛揭曉,根據此次最新的裁決,甲骨文在控訴谷歌侵權之爭中敗訴。陪審團認為,谷歌使用有爭議的代碼程序是“公平使用(
Android 進程通信機制之 AIDL
什麼是 AIDL AIDL 全稱 Android Interface Definition Language,即 安卓接口描述語言。聽起來很深奧,其實它的本質就是
Android中Handler Runnable與Thread的區別詳解
Android中Handler可以異步控制Runnable,那麼這樣做於Android中的Thread有什麼區別呢?本文將通過多個角度來講解這個問題,讀完此文,相