Java BufferedImage Rotate rotate90(BufferedImage bi)

Here you can find the source of rotate90(BufferedImage bi)

Description

Rotate a BufferedImage 90 degrees clockwise.

License

Open Source License

Parameter

Parameter Description
bi A given image to rotate.

Return

A new image that is rotated 90 degrees clockwise.

Declaration

public static BufferedImage rotate90(BufferedImage bi) 

Method Source Code

//package com.java2s;
/*//from  w  w  w  . j  a  va 2  s.c  o  m
This file is part of JFLICKS.
    
JFLICKS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
JFLICKS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with JFLICKS.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.awt.Graphics2D;

import java.awt.image.BufferedImage;

public class Main {
    /**
     * Rotate a BufferedImage 90 degrees clockwise.
     *
     * @param bi A given image to rotate.
     * @return A new image that is rotated 90 degrees clockwise.
     */
    public static BufferedImage rotate90(BufferedImage bi) {

        BufferedImage result = null;

        if (bi != null) {

            int w = bi.getWidth();
            int h = bi.getHeight();
            int endw = h;
            int endh = w;
            int max = w;
            if (h > w) {

                max = h;
                endw = w;
                endh = h;
            }
            result = new BufferedImage(max, max, BufferedImage.TYPE_INT_ARGB);
            Graphics2D bg = result.createGraphics();
            double dmax = ((double) max) / 2.0;
            bg.rotate(Math.toRadians(90), dmax, dmax);
            bg.drawImage(bi, 0, 0, w, h, 0, 0, w, h, null);

            bg.dispose();

            if (endw == h) {

                result = result.getSubimage(max - endw, 0, endw, endh);

            } else {

                result = result.getSubimage(0, 0, endh, endw);
            }
        }

        return (result);
    }
}

Related

  1. rotate(BufferedImage img, int b, int a, int angle)
  2. rotate(BufferedImage source, int angle)
  3. rotate(BufferedImage src)
  4. rotate(final BufferedImage image, final int angle)
  5. rotate(Graphics2D g, BufferedImage newImg, int x, int y, int width, int height, int angle, int originX, int originY)
  6. rotate90(BufferedImage img, boolean left)
  7. rotate90ToRight(BufferedImage inputImage)
  8. rotateAndFlipSwappingRowsAndColumns(BufferedImage srcImage)
  9. rotateBufferedImage(BufferedImage bufferedImage, double angle)