android 比較靠譜的圖片壓縮

第一:咱們先看下質量壓縮方法:php

 

Java代碼  收藏代碼android

  1. private Bitmap compressImage(Bitmap image) {  web

  2.   

  3.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  算法

  4.         image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//質量壓縮方法,這裏100表示不壓縮,把壓縮後的數據存放到baos中  服務器

  5.         int options = 100;  spa

  6.         while ( baos.toByteArray().length / 1024>100) {  //循環判斷若是壓縮後圖片是否大於100kb,大於繼續壓縮         debug

  7.             options -= 10;//每次都減小10 code

  8.              baos.reset();//重置baos即清空baos  orm

  9.             image.compress(Bitmap.CompressFormat.JPEG, options, baos);//這裏壓縮options%,把壓縮後的數據存放到baos中  圖片

  10.             

  11.         }  

  12.         ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把壓縮後的數據baos存放到ByteArrayInputStream中  

  13.         Bitmap bitmap = BitmapFactory.decodeStream(isBm, nullnull);//把ByteArrayInputStream數據生成圖片  

  14.         return bitmap;  

  15.     }  

 

第二:圖片按比例大小壓縮方法(根據路徑獲取圖片並壓縮):

 

 

Java代碼 收藏代碼

  1. private Bitmap getimage(String srcPath) {  

  2.         BitmapFactory.Options newOpts = new BitmapFactory.Options();  

  3.         //開始讀入圖片,此時把options.inJustDecodeBounds 設回true了  

  4.         newOpts.inJustDecodeBounds = true;  

  5.         Bitmap bitmap = BitmapFactory.decodeFile(srcPath,newOpts);//此時返回bm爲空  

  6.           

  7.         newOpts.inJustDecodeBounds = false;  

  8.         int w = newOpts.outWidth;  

  9.         int h = newOpts.outHeight;  

  10.         //如今主流手機比較可能是800*480分辨率,因此高和寬咱們設置爲  

  11.         float hh = 800f;//這裏設置高度爲800f  

  12.         float ww = 480f;//這裏設置寬度爲480f  

  13.         //縮放比。因爲是固定比例縮放,只用高或者寬其中一個數據進行計算便可  

  14.         int be = 1;//be=1表示不縮放  

  15.         if (w > h && w > ww) {//若是寬度大的話根據寬度固定大小縮放  

  16.             be = (int) (newOpts.outWidth / ww);  

  17.         } else if (w < h && h > hh) {//若是高度高的話根據寬度固定大小縮放  

  18.             be = (int) (newOpts.outHeight / hh);  

  19.         }  

  20.         if (be <= 0)  

  21.             be = 1;  

  22.         newOpts.inSampleSize = be;//設置縮放比例  

  23.         //從新讀入圖片,注意此時已經把options.inJustDecodeBounds 設回false了  

  24.         bitmap = BitmapFactory.decodeFile(srcPath, newOpts);  

  25.         return compressImage(bitmap);//壓縮比如例大小後再進行質量壓縮  

  26.     }  

 

第三:圖片按比例大小壓縮方法(根據Bitmap圖片壓縮):

 

Java代碼 收藏代碼

  1. private Bitmap comp(Bitmap image) {  

  2.       

  3.     ByteArrayOutputStream baos = new ByteArrayOutputStream();         

  4.     image.compress(Bitmap.CompressFormat.JPEG, 100, baos);  

  5.     if( baos.toByteArray().length / 1024>1024) {//判斷若是圖片大於1M,進行壓縮避免在生成圖片(BitmapFactory.decodeStream)時溢出    

  6.         baos.reset();//重置baos即清空baos  

  7.         image.compress(Bitmap.CompressFormat.JPEG, 50, baos);//這裏壓縮50%,把壓縮後的數據存放到baos中  

  8.     }  

  9.     ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());  

  10.     BitmapFactory.Options newOpts = new BitmapFactory.Options();  

  11.     //開始讀入圖片,此時把options.inJustDecodeBounds 設回true了  

  12.     newOpts.inJustDecodeBounds = true;  

  13.     Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);  

  14.     newOpts.inJustDecodeBounds = false;  

  15.     int w = newOpts.outWidth;  

  16.     int h = newOpts.outHeight;  

  17.     //如今主流手機比較可能是800*480分辨率,因此高和寬咱們設置爲  

  18.     float hh = 800f;//這裏設置高度爲800f  

  19.     float ww = 480f;//這裏設置寬度爲480f  

  20.     //縮放比。因爲是固定比例縮放,只用高或者寬其中一個數據進行計算便可  

  21.     int be = 1;//be=1表示不縮放  

  22.     if (w > h && w > ww) {//若是寬度大的話根據寬度固定大小縮放  

  23.         be = (int) (newOpts.outWidth / ww);  

  24.     } else if (w < h && h > hh) {//若是高度高的話根據寬度固定大小縮放  

  25.         be = (int) (newOpts.outHeight / hh);  

  26.     }  

  27.     if (be <= 0)  

  28.         be = 1;  

  29.     newOpts.inSampleSize = be;//設置縮放比例  

  30.     //從新讀入圖片,注意此時已經把options.inJustDecodeBounds 設回false了  

  31.     isBm = new ByteArrayInputStream(baos.toByteArray());  

  32.     bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);  

  33.     return compressImage(bitmap);//壓縮比如例大小後再進行質量壓縮  

  34. }  

 

 

評論

8 樓 jthou 2014-12-10  

剛 image.compress(Bitmap.CompressFormat.JPEG, options, baos);//這裏壓縮options%,這一句用的Bitmap.CompressFormat.PNG,改成Bitmap.CompressFormat.JPEG這個就變了,不明白爲何

7 樓 jthou 2014-12-10  

用第一種方法,每次循環以後baos.toByteArray().length的大小不變
用第二種方法,上傳到服務器以後佔用內存大小不變
不知道是否是本身用的不對,心塞

6 樓 gys12321 2014-10-13  

這是源碼:
public boolean compressImage(File file) {
String path = file.getPath();
Bitmap bitmap = BitmapFactory.decodeFile(path);
BitmapFactory.Options newOpts = new BitmapFactory.Options();
// 開始讀入圖片,此時把options.inJustDecodeBounds設爲true
newOpts.inJustDecodeBounds = true;
bitmap = BitmapFactory.decodeFile(path, newOpts);
newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
// 設置分辨率
float hh = 800f;
float ww = 480f;
// 縮放比。因爲是固定的比例縮放,只用高或者寬其中一個數據進行計算便可
int 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;// 設置縮放比例
// 從新讀入圖片,注意此時已經把newOpts.inJustDecodeBounds = false
bitmap = BitmapFactory.decodeFile(path, newOpts);
try {
return compressImage2(bitmap);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}

public boolean compressImage2(Bitmap bitmap) throws Exception {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
int options = 100;
int size = baos.toByteArray().length / 1024;
while (size > 40 && options > 0) {
baos.reset();// 重置baos即清空baos
options -= 10;// 每次都減小10
// 這裏壓縮options%,把壓縮後的數據存放到baos中
bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
size = baos.toByteArray().length / 1024;
}
// 把壓縮後的數據baos存放到ByteArrayInputStream中
ByteArrayInputStream isBm = new ByteArrayInputStream(
baos.toByteArray());
// 把ByteArrayInputStream數據生成圖片
bitmap = BitmapFactory.decodeStream(isBm, null, null);
if (size > 40) {
return true;
} else {
return false;
}

} catch (Exception e) {
throw e;
}

}

5 樓 gys12321 2014-10-13  

您好,樓主,我用了第二種壓縮圖片的方法後,發現程序在debug的時候圖片大小小於40kb(客戶的要求),可是實際上傳到服務器的圖片卻沒有壓縮,求解

4 樓 影憂藍 2013-07-16  

有沒有android把多張圖片打包壓縮一次性上傳
php接收解壓縮的方法

3 樓 Jack_l1 2013-03-01  

2 樓 tony21st 2013-01-16  

我認爲經過compress的方法只是減少了文件的大小,可是並不能保證減低bitmap文件解碼後在內存的佔用量,而android的圖片處理的最關鍵的問題仍是解碼後大小不能太大,不然很容易出現OOM,而縮小圖片的尺寸則能夠下降圖片解碼後在內存的大小。

1 樓 edwar12345 2012-12-17  

兄弟,看了你的博客,對壓縮又有收穫。。同時,指出你的循環裏一個問題。。以下:  ByteArrayOutputStream baos = new ByteArrayOutputStream();          image.compress(Bitmap.CompressFormat.JPEG, 100, baos);        int options = 100;while ( baos.toByteArray().length / 1024>100) {             baos.reset();//重置baos即清空baos              image.compress(Bitmap.CompressFormat.JPEG, options, baos);            options -= 10;//每次都減小10          }  問題就是:   當options=100,while判斷爲真時,循環體內,compress方法仍是按options=100壓縮,而後options才自減。。 等於多執行了一次循環體。。   建議:   壓縮代碼 和 自減代碼 調換位置,從而節省一次循環。。另: 一,根據個人壓縮實踐,個人手機。。 壓縮先後圖片大小以下:100%-11620190%-3775480%-25571故, 第一次90%壓縮,圖片大小大幅度下降。第二次80%壓縮(至關於90%*80% = 72%壓縮),大小已經降低幅度不大了。 結論一:因此,能夠第一次直接options = 90壓縮,跳過=100 的壓縮。二, 圖片比例壓縮時, 我看到一個算法,說比較快。。be = (int) ((w / STANDARD_WIDTH + h/ STANDARD_HEIGHT) / 2);結論二:圖片比例壓縮倍數 就是 (寬度壓縮倍數+高度壓縮倍數)/2..三,再剛生成圖片時,options裏設置以下:newOpts.inPreferredConfig = Config.RGB_565;//下降圖片從ARGB888到RGB565結論三:圖片配置係數用RGB_565, 默認是24位的ARGB_888

相關文章
相關標籤/搜索