代码
public static BufferedImage rotateImage(BufferedImage originalImage, double angle) {
int width = originalImage.getWidth();
int height = originalImage.getHeight();
double radian = Math.toRadians(angle);
double sin = Math.abs(Math.sin(radian));
double cos = Math.abs(Math.cos(radian));
int newWidth = (int) Math.floor(width * cos + height * sin);
int newHeight = (int) Math.floor(width * sin + height * cos);
BufferedImage rotatedImage = new BufferedImage(newWidth, newHeight, originalImage.getType());
int centerX = newWidth / 2;
int centerY = newHeight / 2;
int originalCenterX = width / 2;
int originalCenterY = height / 2;
Graphics2D graphics2D = rotatedImage.createGraphics();
AffineTransform transform = new AffineTransform();
transform.rotate(radian, centerX, centerY);
transform.translate(centerX - originalCenterX, centerY - originalCenterY);
graphics2D.setTransform(transform);
graphics2D.drawImage(originalImage, 0, 0, null);
graphics2D.dispose();
return rotatedImage;
}