以前寫過服務端實現圖片壓縮
java
Android 項目中一般也有2種壓縮方式spa
Android中圖片有四種屬性,分別是:
ALPHA_8:每一個像素佔用1byte內存
ARGB_4444:每一個像素佔用2byte內存
ARGB_8888:每一個像素佔用4byte內存 (默認)
RGB_565:每一個像素佔用2byte內存
Android默認的顏色模式爲ARGB_8888,這個顏色模式色彩最細膩,顯示質量最高。但一樣的,佔用的內存也最大。 因此在對圖片效果不是特別高的狀況下使用RGB_565(565沒有透明度屬性).net
一般bitmap打水印方式壓縮以下code
public Bitmap createBitmapThumbnail(Bitmap bitMap) { int width = bitMap.getWidth(); int height = bitMap.getHeight(); // 設置想要的大小 int newWidth = 99; int newHeight = 99; // 計算縮放比例 float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // 取得想要縮放的matrix參數 Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); // 獲得新的圖片 Bitmap newBitMap = Bitmap.createBitmap(bitMap, 0, 0, width, height, matrix, true); return newBitMap; }
1.大小壓縮orm
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 compressImage(bitmap);//壓縮比如例大小後再進行質量壓縮 }
2.質量壓縮blog
private Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//質量壓縮方法,這裏100表示不壓縮,把壓縮後的數據存放到baos中 int options = 100; while ( baos.toByteArray().length / 1024>100) { //循環判斷若是壓縮後圖片是否大於100kb,大於繼續壓縮 baos.reset();//重置baos即清空baos image.compress(Bitmap.CompressFormat.JPEG, options, baos);//這裏壓縮options%,把壓縮後的數據存放到baos中 options -= 10;//每次都減小10 } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把壓縮後的數據baos存放到ByteArrayInputStream中 Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream數據生成圖片 return bitmap; }
3.混合方式壓縮圖片
private Bitmap comp(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100, baos); if( baos.toByteArray().length / 1024>1024) {//判斷若是圖片大於1M,進行壓縮避免在生成圖片(BitmapFactory.decodeStream)時溢出 baos.reset();//重置baos即清空baos image.compress(Bitmap.CompressFormat.JPEG, 50, baos);//這裏壓縮50%,把壓縮後的數據存放到baos中 } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray()); BitmapFactory.Options newOpts = new BitmapFactory.Options(); //開始讀入圖片,此時把options.inJustDecodeBounds 設回true了 newOpts.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts); 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了 isBm = new ByteArrayInputStream(baos.toByteArray()); bitmap = BitmapFactory.decodeStream(isBm, null, newOpts); return compressImage(bitmap);//壓縮比如例大小後再進行質量壓縮 }