Java BufferedImage Rotate rotateImage(BufferedImage img, double degree)

Here you can find the source of rotateImage(BufferedImage img, double degree)

Description

rotate Image

License

Open Source License

Declaration

public static BufferedImage rotateImage(BufferedImage img, double degree) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) JavaPEG developers// w  w  w.ja v a  2  s .com
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

import java.awt.*;
import java.awt.image.BufferedImage;

public class Main {
    public static BufferedImage rotateImage(BufferedImage img, double degree) {
        double angle = Math.toRadians(degree);
        return tilt(img, angle);
    }

    public static BufferedImage tilt(BufferedImage image, double angle) {
        double sin = Math.abs(Math.sin(angle));
        double cos = Math.abs(Math.cos(angle));

        int w = image.getWidth();
        int h = image.getHeight();

        int neww = (int) Math.floor(w * cos + h * sin);
        int newh = (int) Math.floor(h * cos + w * sin);

        GraphicsConfiguration gc = getDefaultConfiguration();
        BufferedImage result = gc.createCompatibleImage(neww, newh, Transparency.TRANSLUCENT);
        Graphics2D g = result.createGraphics();
        g.translate((neww - w) / 2, (newh - h) / 2);
        g.rotate(angle, w / 2, h / 2);
        g.drawRenderedImage(image, null);
        g.dispose();
        return result;
    }

    public static GraphicsConfiguration getDefaultConfiguration() {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();
        return gd.getDefaultConfiguration();
    }
}

Related

  1. rotateImage(BufferedImage image, float angle)
  2. rotateImage(BufferedImage image, int cwRotationNeeded)
  3. rotateImage(BufferedImage imageToRotate, float degrees)
  4. rotateImage(BufferedImage img, double angle)
  5. rotateImage(BufferedImage img, double angle, int type)
  6. rotateImage(BufferedImage src, double degrees)
  7. rotateImage(final BufferedImage image, final double theta)
  8. rotateImage(final BufferedImage savedImage, final int angle)
  9. rotateImage(final BufferedImage src, final double degrees)