解決java壓縮圖片透明背景變黑色的問題
更新時間:2014年04月15日 09:01:36 作者:
這篇文章主要介紹了解決java壓縮圖片透明背景變黑色的問題,需要的朋友可以參考下
復(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) {
// 為等比縮放計算輸出的圖片寬度及高度
double rate1 = ((double) bi2.getWidth(null)) / (double) outputWidth + 0.1;
double rate2 = ((double) bi2.getHeight(null)) / (double) outputHeight + 0.1;
// 根據(jù)縮放比率大的進行縮放控制
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配置失敗的問題
這篇文章主要介紹了SpringBoot整合Mybatis,解決TypeAliases配置失敗的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
Java之SpringBoot集成ActiveMQ消息中間件案例講解
這篇文章主要介紹了Java之SpringBoot集成ActiveMQ消息中間件案例講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07
SpringBoot配置自定義攔截器實現(xiàn)過程詳解
在系統(tǒng)中經(jīng)常需要在處理用戶請求之前和之后執(zhí)行一些行為,例如檢測用戶的權(quán)限,或者將請求的信息記錄到日志中,即平時所說的"權(quán)限檢測"及"日志記錄",下面這篇文章主要給大家介紹了關(guān)于在SpringBoot項目中整合攔截器的相關(guān)資料,需要的朋友可以參考下2022-10-10
解決BeanUtils.copyProperties無法成功封裝的問題
這篇文章主要介紹了解決BeanUtils.copyProperties無法成功封裝的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
spring mvc使用@InitBinder標(biāo)簽對表單數(shù)據(jù)綁定的方法
這篇文章主要介紹了spring mvc使用@InitBinder標(biāo)簽對表單數(shù)據(jù)綁定的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03
如何通過自定義spring?invalidator注解校驗數(shù)據(jù)合法性
這篇文章主要介紹了如何通過自定義spring?invalidator注解校驗數(shù)據(jù)合法性,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07

