2010-04-07

利用 Java 以不同形狀合併圖像 (Use Java to combine 2 images with a shape of area)

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

public static BufferedImage combineEllipse(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);
    g2d.dispose();
    Ellipse2D.Float ellipse = new Ellipse2D.Float(ox -= cx, oy -= cy, owidth, oheight);
    for (int i1 = x - cx, i2 = ox; i2 < ox + owidth; i1++, i2++){
        for (int j1 = y - cy, j2 = oy; j2 < oy + oheight; j1++, j2++){
            if (ellipse.contains(i2, j2) && ((0 <= i1 && i1 < source.getWidth()) && (0 <= j1 && j1 < source.getHeight()))){
                out.setRGB(i1, j1, other.getRGB(i2, j2));
            }
        }
    }
    return out;
}

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

修改前
before
apple.jpg

以 combineEllipse(source, other, 100, 100, 0, 0, 200, 200) 修改後
edit with combineEllipse(source, other, 100, 100, 0, 0, 200, 200)
apple3.jpg

以 combineEllipse(source, other, 150, 150, 150, 150, 200, 200, 100, 100) 修改後
edit with combineEllipse(source, other, 150, 150, 150, 150, 200, 200, 100, 100)
apple2.jpg

沒有留言 :

張貼留言