Java Image Rotate rotate(Image img, double angle)

Here you can find the source of rotate(Image img, double angle)

Description

Rotates an image.

License

Open Source License

Parameter

Parameter Description
img The image to be rotated
angle The angle in degrees

Return

The rotated image

Declaration

public static Image rotate(Image img, double angle) 

Method Source Code

//package com.java2s;
/*//www. ja v  a 2 s.  c  om
 * #%L
 * Java Applet for biometric trait acquisition [http://www.biosignin.org]
 * ImageUtils.java is part of BioSignIn project
 * %%
 * Copyright (C) 2014 Innovery SpA
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 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 Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * #L%
 */

import java.awt.Graphics2D;
import java.awt.Image;

import java.awt.image.BufferedImage;

public class Main {
    /**
     * Rotates an image. Actually rotates a new copy of the image.
     * 
     * @param img The image to be rotated
     * @param angle The angle in degrees
     * @return The rotated image
     */
    public static Image rotate(Image img, double angle) {
        double sin = Math.abs(Math.sin(Math.toRadians(angle))), cos = Math.abs(Math.cos(Math.toRadians(angle)));
        int w = img.getWidth(null), h = img.getHeight(null);
        int neww = (int) Math.floor(w * cos + h * sin), newh = (int) Math.floor(h * cos + w * sin);
        BufferedImage bimg = toBufferedImage(getEmptyImage(neww, newh));
        Graphics2D g = bimg.createGraphics();
        g.translate((neww - w) / 2, (newh - h) / 2);
        g.rotate(Math.toRadians(angle), w / 2, h / 2);
        g.drawRenderedImage(toBufferedImage(img), null);
        g.dispose();
        return toImage(bimg);
    }

    public static BufferedImage toBufferedImage(Image img) {
        long start = System.nanoTime();

        if (img instanceof BufferedImage) {
            return (BufferedImage) img;
        }
        // Create a buffered image with transparency
        BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null),
                BufferedImage.TYPE_INT_ARGB);
        // Draw the image on to the buffered image
        Graphics2D bGr = bimage.createGraphics();
        bGr.drawImage(img, 0, 0, null);
        bGr.dispose();
        long last = System.nanoTime();
        System.out.println("tablet toBufferedImage in " + ((last - start) / 1000000) + "ms");
        // Return the buffered image
        return bimage;
    }

    /**
     * Creates an empty image with transparency
     * 
     * @param width The width of required image
     * @param height The height of required image
     * @return The created image
     */
    public static BufferedImage getEmptyImage(int width, int height) {
        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        return (img);
    }

    /**
     * Converts a given BufferedImage into an Image
     * 
     * @param bimage The BufferedImage to be converted
     * @return The converted Image
     */
    public static Image toImage(BufferedImage bimage) {
        // Casting is enough to convert from BufferedImage to Image
        Image img = (Image) bimage;
        return img;
    }
}

Related

  1. imageRotate(Image img, int angle)
  2. rotate(File sourceImage, String targetImageName, double degree)
  3. rotate(Image image, double angle)
  4. rotate(Image img, double angle)
  5. rotate(Image img, float radian)
  6. Rotate(Image src, int angel)
  7. rotateImage(final File imagePath, int numquadrants)
  8. rotateImage(final Image img, final Point location, final double degrees)