Android圖片壓縮的實(shí)例詳解
Android圖片壓縮的實(shí)例詳解
在做微信分享的時(shí)候,由于分享的縮略圖要求不得大于32K,否則不能調(diào)起微信,所以總結(jié)了一下Android圖片的壓縮問(wèn)題,大部分資料都是來(lái)自網(wǎng)上各位的分享,自己只是完善或修改了一下,本著繼續(xù)分享的精神,也方便自己記憶,于是總結(jié)如下。
android圖片壓縮主要有兩種方式:1.壓縮圖片分辨率 2.壓縮圖片質(zhì)量
一、先看壓縮圖片分辨率,很好理解,如本來(lái)1280*768的圖片壓縮為640*384大小。廢話不說(shuō),直接上代碼:
/**
* 按比例壓縮圖片分辨率
* @param inBitmap
* @param outHeight 輸出圖片高度,可據(jù)此保持比例計(jì)算輸出寬度
* @param needRecycled 是否回收inBitmap
* @return
*/
public static Bitmap createScaledBitmapByOutHeight(Bitmap inBitmap, int outHeight, boolean needRecycled) {
int bitmapHeight = inBitmap.getHeight();
int bitmapWidth = inBitmap.getWidth();
int outWidth = bitmapWidth * outHeight / bitmapHeight;
return createScaledBitmap(inBitmap, outWidth, outHeight, needRecycled);
}
/**
* 按比例壓縮圖片分辨率
* @param inBitmap
* @param outHeight 輸出圖片寬度,可據(jù)此保持比例計(jì)算輸出高度
* @param needRecycled 是否回收inBitmap
* @return
*/
public static Bitmap createScaledBitmapByOutWidth(Bitmap inBitmap, int outWidth, boolean needRecycled) {
int bitmapHeight = inBitmap.getHeight();
int bitmapWidth = inBitmap.getWidth();
int outHeight = bitmapHeight * outWidth / bitmapWidth;
return createScaledBitmap(inBitmap, outWidth, outHeight, needRecycled);
}
/**
* 指定輸出的寬高縮放圖片
* @param inBitmap
* @param outWidth
* @param outHeight
* @param needRecycled
* @return
*/
public static Bitmap createScaledBitmap(Bitmap inBitmap, int outWidth, int outHeight, boolean needRecycled) {
Bitmap thumbBmp = Bitmap.createScaledBitmap(inBitmap, outWidth, outHeight, true);
if (needRecycled) {
inBitmap.recycle();
}
return thumbBmp;
}
前兩個(gè)方法可以指定期望的寬度或高度,并按比例縮放圖片的分辨率,第3個(gè)方法可以隨意指定期望的寬高,縮放圖片。
上面代碼是對(duì)輸入的bitmap進(jìn)行縮放,還可以從資源或文件中加載圖片并縮放,具體如下:
/**
* 從資源加載并壓縮圖片
* @param res
* @param resId
* @param outWidth 目標(biāo)寬度
* @param outHeight 目標(biāo)高度
* @return
*/
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int outWidth, int outHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; // 假解,來(lái)獲取圖片大小
BitmapFactory.decodeResource(res, resId, options);
options.inSampleSize = calculateInSampleSize(options, outWidth, outHeight);
// 使用獲取到的inSampleSize值再次解析圖片
options.inJustDecodeBounds = false;
//options.inPreferredConfig = Config.RGB_565;
return BitmapFactory.decodeResource(res, resId, options);
}
/**
* 從文件中加載并壓縮圖片
* @param res
* @param resId
* @param outWidth 目標(biāo)寬度
* @param outHeight 目標(biāo)高度
* @return
*/
public static Bitmap decodeSampledBitmapFromFile(String pathName, int outWidth, int outHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; // 假解,來(lái)獲取圖片大小
BitmapFactory.decodeFile(pathName, options);
options.inSampleSize = calculateInSampleSize(options, outWidth, outHeight);
// 使用獲取到的inSampleSize值再次解析圖片
options.inJustDecodeBounds = false;
//options.inPreferredConfig = Config.RGB_565;
return BitmapFactory.decodeFile(pathName, options);
}
/**
* 計(jì)算options.inSampleSize
* @param options
* @param reqWidth
* @param reqHeight
* @return
*/
public 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) {
// 計(jì)算出實(shí)際寬高和目標(biāo)寬高的比率
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// 選擇寬和高中最小的比率作為inSampleSize的值,這樣可以保證最終圖片的寬和高
// 一定都會(huì)大于等于目標(biāo)的寬和高。
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
二、壓縮圖片的質(zhì)量
/**
* 壓縮圖片質(zhì)量,把圖片壓縮到outSize以內(nèi)
* @param inBitmap 原始bitmap
* @param outSize 壓縮到的大小
* @param needRecycled 是否回收bitmap
* @return
*/
public static Bitmap compressImage(Bitmap inBitmap, int outSize, boolean needRecycled) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
inBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
int quality = 100;
while (baos.toByteArray().length / 1024 > outSize) {
if (quality <= 0) {
ByteArrayInputStream outBais = new ByteArrayInputStream(baos.toByteArray());
return BitmapFactory.decodeStream(outBais, null, null);// 如果quaLity為0時(shí)還未達(dá)到32k以內(nèi),返回得到的最小值;如需要可結(jié)合分辨率壓縮
}
baos.reset();
//PNG格式下,這種壓縮不起作用(quality:0-100,如果目標(biāo)大小太小,有時(shí)候質(zhì)量壓縮不一定能達(dá)到效果,需結(jié)合分辨率壓縮)
inBitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
Log.e("AN", "bitmap size:"+ baos.toByteArray().length / 1024 + "k");
quality -= 10;
}
if (needRecycled) {
inBitmap.recycle();
}
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
Bitmap outBitmap= BitmapFactory.decodeStream(bais, null, null);//ByteArrayInputStream轉(zhuǎn)成bitmap
return outBitmap;
}
需要注意的是compress方法,該壓縮方法只對(duì)JPEG格式有效,對(duì)于PNG格式,則會(huì)忽略第二個(gè)參數(shù)quality,即壓縮不起作用。這種壓縮只是對(duì)圖片質(zhì)量有影響,并不會(huì)改變圖片大小。
當(dāng)然,如有需要,以上兩種壓縮方法可以結(jié)合使用。
以上就是Android 圖片壓縮的實(shí)現(xiàn)方法的詳解,如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
學(xué)習(xí)Android Material Design(RecyclerView代替ListView)
Android Material Design越來(lái)越流行,以前很常用的 ListView 現(xiàn)在也用RecyclerView代替了,實(shí)現(xiàn)原理還是相似的,感興趣的小伙伴們可以參考一下2016-01-01
Android利用反射機(jī)制調(diào)用截屏方法和獲取屏幕寬高的方法
這篇文章主要介紹了Android利用反射機(jī)制調(diào)用截屏方法和獲取屏幕寬高的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Android自定義控件RatingBar調(diào)整字體大小
這篇文章主要為大家詳細(xì)介紹了Android自定義控件RatingBar調(diào)整字體大小的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Android酷炫動(dòng)畫(huà)效果之3D星體旋轉(zhuǎn)效果
本文要實(shí)現(xiàn)的3D星體旋轉(zhuǎn)效果是從CoverFlow演繹而來(lái),不過(guò)CoverFlow只是對(duì)圖像進(jìn)行轉(zhuǎn)動(dòng),我這里要實(shí)現(xiàn)的效果是要對(duì)所有的View進(jìn)行類似旋轉(zhuǎn)木馬的轉(zhuǎn)動(dòng)2018-05-05
詳解Android ContentProvider的基本原理和使用
ContentProvider(內(nèi)容提供者)是 Android 的四大組件之一,管理 Android 以結(jié)構(gòu)化方式存放的數(shù)據(jù),以相對(duì)安全的方式封裝數(shù)據(jù)(表)并且提供簡(jiǎn)易的處理機(jī)制和統(tǒng)一的訪問(wèn)接口供其他程序調(diào)用2021-06-06
Android開(kāi)發(fā)四大組件之實(shí)現(xiàn)電話攔截和電話錄音
這篇文章給大家介紹Android開(kāi)發(fā)四大組件之實(shí)現(xiàn)電話攔截和電話錄音,涉及到android四大基本組件在程序中的應(yīng)用,對(duì)android四大基本組件感興趣的朋友可以參考下本篇文章2015-10-10
Android實(shí)現(xiàn)簡(jiǎn)易的柱狀圖和曲線圖表實(shí)例代碼
柱狀圖是統(tǒng)計(jì)圖表中經(jīng)常用到的一種圖表,比如降雨量之類的統(tǒng)計(jì)展示。這篇文章主要給大家介紹了關(guān)于利用Android如何實(shí)現(xiàn)簡(jiǎn)易的柱狀圖和曲線圖表的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-12-12

