Here you can find the source of rotateImage(BufferedImage image, int cwRotationNeeded)
public static BufferedImage rotateImage(BufferedImage image, int cwRotationNeeded)
//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; } }