コードコピーは次のとおりです。
パブリッククラスの画像{
// TODO自動生成コンストラクタースタブ
public static void resizepng(string fromfile、string tofile、int outputwidth、int oututheight、boolean propution){
試す {
ファイルF2 =新しいファイル(FromFile);
bufferedimage bi2 = imageio.read(f2);
int newWidth;
int newheight;
//スケーリング比かどうかを判断します
if(propertion == true){
//等しい比率スケーリングの出力画像幅と高さを計算します
double rate1 =((double)bi2.getwidth(null)) /(double)outputWidth + 0.1;
double rate2 =((double)bi2.getheight(null)) /(double)oututheight + 0.1;
//大きなスケーリング比に応じたスケーリングコントロール
double rate = rate1 <rate2?
newWidth =(int)(((((double)bi2.getwidth(null)) / reat);
newHeight =(int)(((((double)bi2.getheight(null)) / rate);
} それ以外 {
newWidth = outputWidth; //出力幅
newHeight = Outputheight; //出力画像の高さ
}
bufferedimage to = new BufferedImage(NewWidth、NewHeight、
bufferedimage.type_int_rgb);
graphics2d g2d = to.creategraphics();
to = g2d.getDeviceConfiguration()。CreateCompatibleImage(newWidth、NewHeight、
透明性。Translucent);
g2d.dispose();
g2d = to.creategraphics();
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 // desktop // 8d9e9c82d158ccbf8b31059319d8bc3eb035414e.jpg"、 " ;
system.out.println( "ok");
}
}