거울 이미지를 만들려면
-
ImageIO.read() 메서드를 사용하여 필요한 이미지를 읽습니다.
-
이미지의 높이와 너비를 가져옵니다.
-
결과를 저장할 빈 버퍼링된 이미지 생성
-
중첩 for 루프를 사용하면 이미지의 각 픽셀을 통과합니다.
-
이미지의 너비를 오른쪽에서 왼쪽으로 반복합니다.
-
getRGB() 메서드를 사용하여 픽셀 값을 가져옵니다.
-
새로운 너비 값을 대체하여 setRGB() 메서드를 사용하여 픽셀 값을 결과 이미지 객체로 설정합니다.
예시
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class MirrorImage {
public static void main(String args[])throws IOException {
//Reading the image
File file= new File("D:\\Images\\tree.jpg");
BufferedImage img = ImageIO.read(file);
//Getting the height and with of the read image.
int height = img.getHeight();
int width = img.getWidth();
//Creating Buffered Image to store the output
BufferedImage res = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
for(int j = 0; j < height; j++){
for(int i = 0, w = width - 1; i < width; i++, w--){
int p = img.getRGB(i, j);
//set mirror image pixel value - both left and right
res.setRGB(w, j, p);
}
}
//Saving the modified image
file = new File("D:\\Images\\mirror_image.jpg");
ImageIO.write(res, "jpg", file);
System.out.println("Done...");
}
} 입력

출력
