解決java壓縮圖片透明背景變黑色的問(wèn)題
更新時(shí)間:2014年04月15日 09:01:36 作者:
這篇文章主要介紹了解決java壓縮圖片透明背景變黑色的問(wèn)題,需要的朋友可以參考下
復(fù)制代碼 代碼如下:
public class Picture {
// TODO Auto-generated constructor stub
public static void resizePNG(String fromFile, String toFile, int outputWidth, int outputHeight,boolean proportion) {
try {
File f2 = new File(fromFile);
BufferedImage bi2 = ImageIO.read(f2);
int newWidth;
int newHeight;
// 判斷是否是等比縮放
if (proportion == true) {
// 為等比縮放計(jì)算輸出的圖片寬度及高度
double rate1 = ((double) bi2.getWidth(null)) / (double) outputWidth + 0.1;
double rate2 = ((double) bi2.getHeight(null)) / (double) outputHeight + 0.1;
// 根據(jù)縮放比率大的進(jìn)行縮放控制
double rate = rate1 < rate2 ? rate1 : rate2;
newWidth = (int) (((double) bi2.getWidth(null)) / rate);
newHeight = (int) (((double) bi2.getHeight(null)) / rate);
} else {
newWidth = outputWidth; // 輸出的圖片寬度
newHeight = outputHeight; // 輸出的圖片高度
}
BufferedImage to = new BufferedImage(newWidth, newHeight,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = to.createGraphics();
to = g2d.getDeviceConfiguration().createCompatibleImage(newWidth,newHeight,
Transparency.TRANSLUCENT);
g2d.dispose();
g2d = to.createGraphics();
Image from = bi2.getScaledInstance(newWidth, newHeight, bi2.SCALE_AREA_AVERAGING);
g2d.drawImage(from, 0, 0, null);
g2d.dispose();
ImageIO.write(to, "png", new File(toFile));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
System.out.println("Start");
resizePNG("C:\\Documents and Settings\\Administrator\\桌面\\8d9e9c82d158ccbf8b31059319d8bc3eb035414e.jpg", "C:\\Documents and Settings\\Administrator\\桌面\\ell.png",200, 100,true);
System.out.println("OK");
}
}
您可能感興趣的文章:
相關(guān)文章
SpringBoot整合Mybatis,解決TypeAliases配置失敗的問(wèn)題
這篇文章主要介紹了SpringBoot整合Mybatis,解決TypeAliases配置失敗的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Java之SpringBoot集成ActiveMQ消息中間件案例講解
這篇文章主要介紹了Java之SpringBoot集成ActiveMQ消息中間件案例講解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
SpringBoot配置自定義攔截器實(shí)現(xiàn)過(guò)程詳解
在系統(tǒng)中經(jīng)常需要在處理用戶請(qǐng)求之前和之后執(zhí)行一些行為,例如檢測(cè)用戶的權(quán)限,或者將請(qǐng)求的信息記錄到日志中,即平時(shí)所說(shuō)的"權(quán)限檢測(cè)"及"日志記錄",下面這篇文章主要給大家介紹了關(guān)于在SpringBoot項(xiàng)目中整合攔截器的相關(guān)資料,需要的朋友可以參考下2022-10-10
解決BeanUtils.copyProperties無(wú)法成功封裝的問(wèn)題
這篇文章主要介紹了解決BeanUtils.copyProperties無(wú)法成功封裝的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
spring mvc使用@InitBinder標(biāo)簽對(duì)表單數(shù)據(jù)綁定的方法
這篇文章主要介紹了spring mvc使用@InitBinder標(biāo)簽對(duì)表單數(shù)據(jù)綁定的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
如何通過(guò)自定義spring?invalidator注解校驗(yàn)數(shù)據(jù)合法性
這篇文章主要介紹了如何通過(guò)自定義spring?invalidator注解校驗(yàn)數(shù)據(jù)合法性,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
輕量級(jí)聲明式的Http庫(kù)——Feign的獨(dú)立使用
這篇文章主要介紹了輕量級(jí)聲明式的Http庫(kù)——Feign的使用教程,幫助大家更好的理解和學(xué)習(xí)使用feign,感興趣的朋友可以了解下2021-04-04

