2010年4月5日星期一

利用 Java 改變圖像畫布的闊及高 (Use Java to edit the canvas of an image)

import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
import java.awt.Color;

public static BufferedImage canvas(BufferedImage source, int width, int height, int x, int y, int cx, int cy, Color color){
    BufferedImage out = new BufferedImage(width, height, source.getType());
    Graphics2D g2d = out.createGraphics();
    g2d.setColor(color);
    g2d.fillRect(0, 0, width, height);
    g2d.drawImage(source, x - cx, y - cy, source.getWidth(), source.getHeight(), null);
    g2d.dispose();
    return out;
}

public static BufferedImage canvas(BufferedImage source, int width, int height){
    return canvas(source, width, height, width / 2, height / 2, source.getWidth() / 2, source.getHeight() / 2, Color.WHITE);
}

修改前
before
apple.jpg

以 canvas(source, 200, 200) 修改後
edit with canvas(source, 200, 200)
apple2.jpg

以 canvas(source, 400, 400) 修改後
edit with canvas(source, 400, 400)
apple3.jpg

以 canvas(source, 400, 400, 0, 0, 0, 0, Color.RED) 修改後
edit with canvas(source, 400, 400, 0, 0, 0, 0, Color.RED)
apple4.jpg

沒有留言 :

發佈留言