處理java緊縮圖片通明配景變黑色的成績。本站提示廣大學習愛好者:(處理java緊縮圖片通明配景變黑色的成績)文章只能為提供參考,不一定能成為您想要的結果。以下是處理java緊縮圖片通明配景變黑色的成績正文
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;
// 依據縮放比率年夜的停止縮放掌握
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");
}
}