การคัดลอกรหัสมีดังนี้:
รูปภาพชั้นเรียนสาธารณะ {
// todo todo stub constructor ที่สร้างขึ้นอัตโนมัติ
public void void resizepng (String fromfile, String tofile, int outputwidth, int outputheight, สัดส่วนบูลีน) {
พยายาม {
ไฟล์ F2 = ไฟล์ใหม่ (FromFile);
bufferedImage bi2 = imageio.read (f2);
int newwidth;
int Newheight;
// พิจารณาว่าเป็นอัตราส่วนการปรับขนาด
ถ้า (สัดส่วน == จริง) {
// คำนวณความกว้างและความสูงของภาพเอาท์พุทสำหรับการปรับอัตราส่วนที่เท่ากัน
อัตราสองเท่า 1 = ((สองเท่า) bi2.getWidth (null)) / (สอง) outputWidth + 0.1;
double rate2 = ((double) bi2.getheight (null)) / (double) outputheight + 0.1;
// การควบคุมการปรับสเกลตามอัตราส่วนการปรับขนาดใหญ่
อัตราสองครั้ง = อัตรา 1 <อัตรา 2? อัตรา 1: อัตรา 2;
newWidth = (int) ((((สองเท่า) bi2.getWidth (null)) / อัตรา);
newHeight = (int) ((((สองเท่า) bi2.getheight (null)) / อัตรา);
} อื่น {
newWidth = outputWidth;
newHeight = outputheight;
-
bufferedImage to = new BufferedImage (Newwidth, Newheight,
bufferedImage.type_int_rgb);
graphics2d g2d = to.createGraphics ();
ถึง = g2d.getDeviceConfiguration (). createCompatibleImage (newWidth, Newheight,
โปร่งใส translucent);
g2d.dispose ();
g2d = to.createGraphics ();
รูปภาพจาก = bi2.getScaledInstance (Newwidth, Newheight, bi2.scale_area_averaging);
G2D.DrawImage (จาก, 0, 0, null);
g2d.dispose ();
imageio.write (ถึง, "png", ไฟล์ใหม่ (tofile));
} catch (ioexception e) {
E.PrintStackTrace ();
-
-
โมฆะคงที่สาธารณะหลัก (String [] args) พ่น IOException {
System.out.println ("เริ่ม");
resizepng ("c: // เอกสารและการตั้งค่า // ผู้ดูแลระบบ // เดสก์ท็อป // 8D9E9C82D158CCBF8B31059319D8BC3EB035414E.JPG", "// การตั้งค่า // ;
System.out.println ("ตกลง");
-
-