2010-04-05

利用 Java 將圖像合併 (Use Java to combine 2 images)

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

public static BufferedImage combine(BufferedImage source, BufferedImage other, int x, int y, int ox, int oy, int owidth, int oheight, int cx, int cy){
    BufferedImage out = new BufferedImage(source.getWidth(), source.getHeight(), source.getType());
    Graphics2D g2d = out.createGraphics();
    g2d.drawImage(source, 0, 0, null);
    x -= cx;
    y -= cy;
    g2d.drawImage(other, x, y, x + owidth, y + oheight, ox, oy, ox + owidth, oy + oheight, null);
    g2d.dispose();
    return out;
}

public static BufferedImage combine(BufferedImage source, BufferedImage other, int x, int y, int ox, int oy, int owidth, int oheight){
    return combine(source, other, x, y, ox, oy, owidth, oheight, 0, 0);
}

修改前
before
apple.jpg

以 combine(source, other, 150, 150, 0, 0, 150, 150) 修改後
edit with combine(source, other, 150, 150, 0, 0, 150, 150)
banana2.jpg

以 combine(source, other, 150, 150, 0, 0, 150, 150, 75, 75) 修改後
edit with combine(source, other, 150, 150, 0, 0, 150, 150, 75, 75)
banana3.jpg

沒有留言 :

張貼留言