2010-04-03

利用 Java 為圖像添加邊緣(Use Java to trim an image )

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

public static BufferedImage trim(BufferedImage source, int topMargin, int bottomMargin, int leftMargin, int rightMargin, Color color){
    int width = source.getWidth() + leftMargin + rightMargin;
    int height = source.getHeight() + topMargin + bottomMargin;
    BufferedImage out = new BufferedImage(width, height, source.getType());
    Graphics2D g2d = out.createGraphics();
    g2d.setColor(color);
    g2d.fillRect(0, 0, width, height);
    g2d.drawImage(source, leftMargin, topMargin, source.getWidth(), source.getHeight(), null);
    g2d.dispose();
    return out;
}

修改前
before
apple.jpg

以 trim(source, 10, 20, 30, 40, Color.RED) 修改後
edit with trim(source, 10, 20, 30, 40, Color.RED)
apple1.jpg

以 trim(source, -10, -20, -30, -40, Color.RED) 修改後
edit with trim(source, -10, -20, -30, -40, Color.RED)
apple2.jpg

沒有留言 :

張貼留言