Here you can find the source of rotateImage(BufferedImage src, double degrees)
Parameter | Description |
---|---|
src | Source image |
degrees | Angle in degrees |
public static BufferedImage rotateImage(BufferedImage src, double degrees)
//package com.java2s; //License from project: Open Source License import java.awt.*; import java.awt.image.BufferedImage; public class Main { /**/*from w w w .j a v a 2 s . c om*/ * Rotates a BufferedImage * * @param src Source image * @param degrees Angle in degrees * @return Rotated image */ public static BufferedImage rotateImage(BufferedImage src, double degrees) { double radians = Math.toRadians(degrees); int srcWidth = src.getWidth(); int srcHeight = src.getHeight(); double sin = Math.abs(Math.sin(radians)); double cos = Math.abs(Math.cos(radians)); int newWidth = (int) Math.floor(srcWidth * cos + srcHeight * sin); int newHeight = (int) Math.floor(srcHeight * cos + srcWidth * sin); BufferedImage result = new BufferedImage(newWidth, newHeight, src.getType()); Graphics2D g = result.createGraphics(); g.translate((newWidth - srcWidth) / 2, (newHeight - srcHeight) / 2); g.rotate(radians, srcWidth / 2, srcHeight / 2); g.drawRenderedImage(src, null); return result; } }