Java BufferedImage Rotate rotateImage(BufferedImage image, int cwRotationNeeded)

Here you can find the source of rotateImage(BufferedImage image, int cwRotationNeeded)

Description

rotate Image

License

Apache License

Declaration

public static BufferedImage rotateImage(BufferedImage image, int cwRotationNeeded) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

public class Main {
    public static BufferedImage rotateImage(BufferedImage image, int cwRotationNeeded) {
        double angle = Math.toRadians(cwRotationNeeded);
        int type = thumbnailType(image);
        double sin = Math.abs(Math.sin(angle));
        double cos = Math.abs(Math.cos(angle));
        int width = image.getWidth();
        int height = image.getHeight();
        int newWidth = (int) Math.floor(width * cos + height * sin);
        int newHeight = (int) Math.floor(height * cos + width * sin);
        BufferedImage result = new BufferedImage(newWidth, newHeight, type);
        Graphics2D g = result.createGraphics();
        g.translate((newWidth - width) / 2, (newHeight - height) / 2);
        g.rotate(angle, width / 2, height / 2);
        g.drawRenderedImage(image, null);
        g.dispose();/*from  www .jav a2 s.  com*/
        return result;
    }

    public static int thumbnailType(BufferedImage image) {
        if (image.getColorModel().getNumComponents() > 3) {
            return BufferedImage.TYPE_4BYTE_ABGR;
        } else if (image.getColorModel().getNumColorComponents() == 3) {
            return BufferedImage.TYPE_3BYTE_BGR;
        }
        return BufferedImage.TYPE_INT_RGB;
    }
}

Related

  1. rotateFortyFiveClockwise(BufferedImage img)
  2. rotateImage(BufferedImage image, double angle)
  3. rotateImage(BufferedImage image, double angle)
  4. rotateImage(BufferedImage image, double radians)
  5. rotateImage(BufferedImage image, float angle)
  6. rotateImage(BufferedImage imageToRotate, float degrees)
  7. rotateImage(BufferedImage img, double angle)
  8. rotateImage(BufferedImage img, double angle, int type)
  9. rotateImage(BufferedImage img, double degree)