Java BufferedImage Rotate rotate90(BufferedImage img, boolean left)

Here you can find the source of rotate90(BufferedImage img, boolean left)

Description

Rotate the image 90 degrees

License

Open Source License

Parameter

Parameter Description
img image
left rotate counter clockwise

Return

rotated image

Declaration

public static BufferedImage rotate90(BufferedImage img, boolean left) 

Method Source Code

//package com.java2s;
/*//from www  .j ava 2 s.  c  o  m
 * Copyright 1997-2016 Unidata Program Center/University Corporation for
 * Atmospheric Research, P.O. Box 3000, Boulder, CO 80307,
 * support@unidata.ucar.edu.
 * 
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 * 
 * This library 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 Lesser
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import java.awt.Graphics2D;

import java.awt.geom.AffineTransform;

import java.awt.image.BufferedImage;

public class Main {
    /**
     * Rotate the image 90 degrees
     *
     * @param img image
     * @param left rotate counter clockwise
     *
     * @return rotated image
     */
    public static BufferedImage rotate90(BufferedImage img, boolean left) {
        int w = img.getWidth(null);
        int h = img.getHeight(null);
        double angle = (left ? -90 : 90);
        BufferedImage rotatedImage = new BufferedImage(h, w, BufferedImage.TYPE_INT_RGB);
        AffineTransform trans = new AffineTransform();

        trans.rotate(Math.toRadians(angle));

        if (left) {
            trans.translate(-w, 0);
        } else {
            trans.translate(0, -h);
        }

        Graphics2D g = rotatedImage.createGraphics();

        // g.rotate(Math.toRadians(angle),w/2,h/2);
        // g.drawImage(img, null, 0,0);
        g.drawImage(img, trans, null);

        return rotatedImage;
    }
}

Related

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