import java.awt.image.BufferedImage;
public static BufferedImage verticalReverse(BufferedImage source){ BufferedImage out = new BufferedImage(source.getWidth(), source.getHeight(), source.getType()); for (int i = 0; i < source.getWidth(); i++){ for (int j = 0; j < source.getHeight(); j++){ out.setRGB(i, j, source.getRGB(i, source.getWidth() - j - 1)); } } return out; } public static BufferedImage horizontalReverse(BufferedImage source){ BufferedImage out = new BufferedImage(source.getWidth(), source.getHeight(), source.getType()); for (int i = 0; i < source.getWidth(); i++){ for (int j = 0; j < source.getHeight(); j++){ out.setRGB(i, j, source.getRGB(source.getHeight() - i - 1, j)); } } return out; }
修改前
before

以 verticalReverse(source) 修改後
edit with verticalReverse(source)

以 horizontalReverse(source) 修改後
edit with horizontalReverse(source)

對於 verticalReverse(BufferedImage) 及 horizontalReverse(BufferedImage)
其實可以修改為 reverse(BufferedImage, int) 來處理
利用 int 指定為垂直或水平
例如 final int VERTICAL = 0 及 final int HORIZONTAL = 1
透過 switch 或 if 判斷使用哪一種 reverse 比較有規劃性
沒有留言 :
張貼留言