android 將圖片壓縮到指定的大小的示例
從網(wǎng)上收集后自己寫的一個(gè)方法;
1.首先是一個(gè)根據(jù)分辨率壓縮的類,首先對(duì)圖片進(jìn)行一次壓縮
/**
* 根據(jù)分辨率壓縮圖片比例
*
* @param imgPath
* @param w
* @param h
* @return
*/
private static Bitmap compressByResolution(String imgPath, int w, int h) {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imgPath, opts);
int width = opts.outWidth;
int height = opts.outHeight;
int widthScale = width / w;
int heightScale = height / h;
int scale;
if (widthScale < heightScale) { //保留壓縮比例小的
scale = widthScale;
} else {
scale = heightScale;
}
if (scale < 1) {
scale = 1;
}
Log.i(TAG,"圖片分辨率壓縮比例:" + scale);
opts.inSampleSize = scale;
opts.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(imgPath, opts);
return bitmap;
}
2.第二就是循環(huán)對(duì)圖片的壓縮,直到壓縮到指定的大小以下為止(重要?。?/p>
/**
* 根據(jù)分辨率壓縮
*
* @param srcPath 圖片路徑
* @param ImageSize 圖片大小 單位kb
* @return
*/
public static boolean compressBitmap(String srcPath, int ImageSize, String savePath) {
int subtract;
Log.i(TAG, "圖片處理開始..");
Bitmap bitmap = compressByResolution(srcPath, 1024, 720); //分辨率壓縮
if (bitmap == null) {
Log.i(TAG, "bitmap 為空");
return false;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int options = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//質(zhì)量壓縮方法,這里100表示不壓縮,把壓縮后的數(shù)據(jù)存放到baos中
Log.i(TAG, "圖片分辨率壓縮后:" + baos.toByteArray().length / 1024 + "KB");
while (baos.toByteArray().length > ImageSize * 1024) { //循環(huán)判斷如果壓縮后圖片是否大于ImageSize kb,大于繼續(xù)壓縮
subtract = setSubstractSize(baos.toByteArray().length / 1024);
baos.reset();//重置baos即清空baos
options -= subtract;//每次都減少10
bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//這里壓縮options%,把壓縮后的數(shù)據(jù)存放到baos中
Log.i(TAG, "圖片壓縮后:" + baos.toByteArray().length / 1024 + "KB");
}
Log.i(TAG, "圖片處理完成!" + baos.toByteArray().length / 1024 + "KB");
try {
FileOutputStream fos = new FileOutputStream(new File(savePath));//將壓縮后的圖片保存的本地上指定路徑中
fos.write(baos.toByteArray());
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
if (bitmap != null) {
bitmap.recycle();
}
return true; //壓縮成功返回ture
}
在這其中
/**
* 根據(jù)圖片的大小設(shè)置壓縮的比例,提高速度
*
* @param imageMB
* @return
*/
private static int setSubstractSize(int imageMB) {
if (imageMB > 1000) {
return 60;
} else if (imageMB > 750) {
return 40;
} else if (imageMB > 500) {
return 20;
} else {
return 10;
}
}
這個(gè)方法用來(lái)動(dòng)態(tài)設(shè)置每次壓縮的比例,主要用于提升壓縮的時(shí)間,這其中的數(shù)值是我大概測(cè)試出來(lái)的可以修改成你認(rèn)為比較合適的
3.最后
壓縮圖片費(fèi)時(shí)又費(fèi)內(nèi)存,很明顯執(zhí)行的時(shí)候需要在子線程中完成,如果需要的話可以加一個(gè)壓縮完成的監(jiān)聽
下載地址:CommonUtils_jb51.rar
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android studio3.0.1無(wú)法啟動(dòng)Gradle守護(hù)進(jìn)程的解決方法
這篇文章主要為大家詳細(xì)介紹了android studio3.0.1無(wú)法啟動(dòng)Gradle守護(hù)進(jìn)程的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Android實(shí)現(xiàn)在列表List中顯示半透明小窗體效果的控件用法詳解
這篇文章主要介紹了Android實(shí)現(xiàn)在列表List中顯示半透明小窗體效果的控件用法,結(jié)合實(shí)例形式分析了Android半透明提示框的實(shí)現(xiàn)與設(shè)置技巧,需要的朋友可以參考下2016-06-06
Android實(shí)現(xiàn)手勢(shì)密碼功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)手勢(shì)密碼功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android實(shí)現(xiàn)文字上下滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)文字上下滾動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
Android進(jìn)程間大數(shù)據(jù)通信LocalSocket詳解
這篇文章主要為大家介紹了Android進(jìn)程間大數(shù)據(jù)通信LocalSocket詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Android布局中g(shù)ravity與layout_gravity屬性說(shuō)明
這篇文章主要介紹了Android布局中g(shù)ravity與layout_gravity屬性說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
OpenGL Shader實(shí)例分析(3)等待標(biāo)識(shí)效果
這篇文章主要介紹了OpenGL Shader實(shí)例分析第3篇,等待標(biāo)識(shí)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02

