Java Image Rotate rotate(Image img, float radian)

Here you can find the source of rotate(Image img, float radian)

Description

rotate

License

Open Source License

Declaration

public static Image rotate(Image img, float radian) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    public static Image rotate(Image img, float radian) {
        if (img == null) {
            return null;
        }//from w w w . j  a va  2  s  .c o m
        int width = img.getWidth(null);
        int height = img.getHeight(null);
        BufferedImage bufImage = null;
        if (img instanceof BufferedImage) {
            bufImage = (BufferedImage) img;
        } else {
            bufImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
        }

        Graphics2D g2d = bufImage.createGraphics();
        bufImage = g2d.getDeviceConfiguration().createCompatibleImage(height, width, Transparency.TRANSLUCENT);
        g2d.dispose();
        g2d = bufImage.createGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        AffineTransform origXform = g2d.getTransform();
        AffineTransform newXform = (AffineTransform) (origXform.clone());
        double xRot = height / 2.0f;
        newXform.rotate(Math.toRadians(90.0), xRot, xRot);
        g2d.setTransform(newXform);
        g2d.drawImage(img, 0, 0, null);
        g2d.setTransform(origXform);
        Image newImg = bufImage;

        float x = radian - 90.0f;
        if (x > 0) {
            newImg = rotate(bufImage, x);
        }

        return newImg;
    }
}

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, double angle)
  6. Rotate(Image src, int angel)
  7. rotateImage(final File imagePath, int numquadrants)
  8. rotateImage(final Image img, final Point location, final double degrees)
  9. rotateImage(Image inImage, int inMaxWidth, int inMaxHeight, int inRotationDegrees)