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

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

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

沒有留言 :
張貼留言