使用Thumbnails實(shí)現(xiàn)圖片指定大小壓縮
項(xiàng)目中有個(gè)要求,對(duì)上傳服務(wù)器的圖片大小進(jìn)行判斷,大于500k的圖片要進(jìn)行壓縮處理,讓其小于500k后在上傳。
可以通過(guò)java api的ImageIO實(shí)現(xiàn)圖片壓縮,但是看了網(wǎng)上的博客普遍都說(shuō)bug比較多,會(huì)有OOM內(nèi)存溢出的現(xiàn)象。
Thumbnails插件是Google的插件,能指定不同的參數(shù)進(jìn)行壓縮操作。
比如:寬高(size),縮放(scale),制定質(zhì)量比(outputQuality)等。
插件使用的jar包為:
thumbnailator-0.4.8.jar
代碼如下:
/**
*
* @param srcPath 原圖片地址
* @param desPath 目標(biāo)圖片地址
* @param desFileSize 指定圖片大小,單位kb
* @param accuracy 精度,遞歸壓縮的比率,建議小于0.9
* @return
*/
public static String commpressPicForScale(String srcPath,String desPath,
long desFileSize , double accuracy){
try {
File srcFile = new File(srcPath);
long srcFilesize = srcFile.length();
System.out.println("原圖片:"+srcPath + ",大小:" + srcFilesize/1024 + "kb");
//遞歸壓縮,直到目標(biāo)文件大小小于desFileSize
commpressPicCycle(desPath, desFileSize, accuracy);
File desFile = new File(desPath);
System.out.println("目標(biāo)圖片:" + desPath + ",大小" + desFile.length()/1024 + "kb");
System.out.println("圖片壓縮完成!");
} catch (Exception e) {
e.printStackTrace();
}
return desPath;
}
public static void commpressPicCycle(String desPath , long desFileSize,
double accuracy) throws IOException{
File imgFile = new File(desPath);
long fileSize = imgFile.length();
//判斷大小,如果小于500k,不壓縮,如果大于等于500k,壓縮
if(fileSize <= desFileSize * 500){
return;
}
//計(jì)算寬高
BufferedImage bim = ImageIO.read(imgFile);
int imgWidth = bim.getWidth();
int imgHeight = bim.getHeight();
int desWidth = new BigDecimal(imgWidth).multiply(
new BigDecimal(accuracy)).intValue();
int desHeight = new BigDecimal(imgHeight).multiply(
new BigDecimal(accuracy)).intValue();
Thumbnails.of(desPath).size(desWidth, desHeight).outputQuality(accuracy).toFile(desPath);
//如果不滿(mǎn)足要求,遞歸直至滿(mǎn)足小于1M的要求
commpressPicCycle(desPath, desFileSize, accuracy);
}
然后壓縮圖片大?。?/p>
commpressPicForScale(filePath, filePath, 500, 0.8);
壓縮完成:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android activity堆棧及管理實(shí)例詳解
這篇文章主要介紹了Android activity堆棧及管理實(shí)例詳解的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,對(duì)android activity堆棧相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-09-09
Android實(shí)現(xiàn)簡(jiǎn)易記事本
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)易記事本,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android LayoutInflater深入分析及應(yīng)用
這篇文章主要介紹了Android LayoutInflater分析的相關(guān)資料,需要的朋友可以參考下2017-02-02
Flutter開(kāi)發(fā)之Widget自定義總結(jié)
這篇文章主要給大家介紹了關(guān)于Flutter開(kāi)發(fā)中Widget自定義的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
WheelView實(shí)現(xiàn)上下滑動(dòng)選擇器
這篇文章主要為大家詳細(xì)介紹了WheelView實(shí)現(xiàn)上下滑動(dòng)選擇器的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android用注解與反射實(shí)現(xiàn)Butterknife功能
Butterknife是一個(gè)在android上實(shí)現(xiàn)ioc(控制反轉(zhuǎn))的一個(gè)庫(kù)。ioc的核心是解耦。解耦的目的是修改耦合對(duì)象時(shí)不影響另外一個(gè)對(duì)象,降低模塊之間的關(guān)聯(lián)。在Spring中ioc更多的是依靠xml的配置。而android上的IOC框架可以不使用xml配置2022-11-11
Android實(shí)現(xiàn)簡(jiǎn)單QQ登錄頁(yè)面
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單QQ登錄頁(yè)面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04

