본문 바로가기
개발/자바

자바 이미지 회전(rotate) 시키기

by darksilber 2015. 12. 7.
반응형

public static BufferedImage rotate(BufferedImage bi, int degree) {
    int width = bi.getWidth();
    int height = bi.getHeight();

    BufferedImage biFlip;
    if (degree == 90 || degree == 270)
        biFlip = new BufferedImage(height, width, bi.getType());
    else if (degree == 180)
        biFlip = new BufferedImage(width, height, bi.getType());
    else 
        return bi;

    if (degree == 90) {
        for (int i = 0; i < width; i++)
            for (int j = 0; j < height; j++)
                biFlip.setRGB(height- j - 1, i, bi.getRGB(i, j));
   }

if (degree == 180) {
    for (int i = 0; i < width; i++)
        for (int j = 0; j < height; j++)
            biFlip.setRGB(width - i - 1, height - j - 1, bi.getRGB(i, j));
}

if (degree == 270) {
    for (int i = 0; i < width; i++)
        for (int j = 0; j < height; j++)
            biFlip.setRGB(j, width - i - 1, bi.getRGB(i, j));
}

bi.flush();
bi = null;

return biFlip;
}

 

반응형

댓글