Salinan kode adalah sebagai berikut:
gambar kelas publik {
// TODO Stub Konstruktor yang dihasilkan otomatis
public static void resizePng (string fromfile, string tofile, int outputWidth, int outputheight, proporsi boolean) {
mencoba {
File f2 = file baru (fromFile);
BufferedImage Bi2 = ImageIo.Read (F2);
int newwidth;
int newheight;
// Tentukan apakah itu rasio penskalaan
if (proporsi == true) {
// Hitung lebar dan tinggi gambar output untuk penskalaan rasio yang sama
Double rate1 = ((ganda) bi2.getWidth (null)) / (ganda) outputWidth + 0,1;
double rate2 = ((double) bi2.getHeight (null)) / (ganda) outputheight + 0.1;
// Kontrol penskalaan sesuai dengan rasio penskalaan yang besar
tarif ganda = rate1 <rate2?
newwidth = (int) ((((double) bi2.getWidth (null)) / rate);
newheight = (int) ((((double) bi2.getHeight (null)) / rate);
} kalau tidak {
newwidth = outputwidth;
newheight = outputheight; // tinggi gambar output
}
BufferedImage ke = BufferedImage baru (Newwidth, Newheight,
BufferedImage.type_int_rgb);
Grafik2d g2d = to.creategraphics ();
to = g2d.getDeviceConfiguration (). CreateCompatibleImage (Newwidth, Newheight,
Transparansi.translucent);
g2d.dispose ();
g2d = to.creategraphics ();
Gambar dari = bi2.getscaledInstance (newwidth, newheight, bi2.scale_area_averaging);
g2d.drawimage (dari, 0, 0, null);
g2d.dispose ();
Imageo.write (ke, "png", file baru (tofile));
} catch (ioException e) {
e.printstacktrace ();
}
}
public static void main (string [] args) melempar ioException {
System.out.println ("Start");
resizePng ("c: // dokumen dan pengaturan // administrator // desktop // 8d9e9c82d158ccbf8b31059319d8bc3eb035414e.jpg", "c: // dokumen dan pengaturan // administrator // desktop." ;
System.out.println ("OK");
}
}