You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Java Swing中按指定角度旋转ImageIcon?

Java 实现 ImageIcon 旋转并转换为 BufferedImage

直接上可运行的完整实现,解决旋转后图像裁剪、格式兼容问题:

import javax.swing.ImageIcon;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

public class ImageRotationUtils {

    // 将 ImageIcon 转换为 BufferedImage
    public static BufferedImage iconToBufferedImage(ImageIcon icon) {
        Image image = icon.getImage();
        // 如果已经是 BufferedImage,直接强转
        if (image instanceof BufferedImage) {
            return (BufferedImage) image;
        }

        // 创建新的 BufferedImage,匹配原图像的透明度和尺寸
        BufferedImage bufferedImage = new BufferedImage(
                image.getWidth(null),
                image.getHeight(null),
                BufferedImage.TYPE_INT_ARGB
        );

        // 绘制原图像到 BufferedImage
        Graphics2D g2d = bufferedImage.createGraphics();
        g2d.drawImage(image, 0, 0, null);
        g2d.dispose();

        return bufferedImage;
    }

    // 旋转 BufferedImage 并返回新的图像(自动适配旋转后的尺寸)
    public static BufferedImage rotateBufferedImage(BufferedImage original, double degrees) {
        // 计算旋转弧度
        double radians = Math.toRadians(degrees);
        int originalWidth = original.getWidth();
        int originalHeight = original.getHeight();

        // 计算旋转后图像的新尺寸,避免裁剪
        int newWidth = (int) Math.round(
                originalWidth * Math.abs(Math.cos(radians)) + originalHeight * Math.abs(Math.sin(radians))
        );
        int newHeight = (int) Math.round(
                originalWidth * Math.abs(Math.sin(radians)) + originalHeight * Math.abs(Math.cos(radians))
        );

        // 创建适配新尺寸的 BufferedImage
        BufferedImage rotatedImage = new BufferedImage(newWidth, newHeight, original.getType());
        Graphics2D g2d = rotatedImage.createGraphics();

        // 设置高质量渲染参数
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // 平移坐标系到图像中心,旋转后再平移回去
        AffineTransform transform = new AffineTransform();
        transform.translate(newWidth / 2.0, newHeight / 2.0);
        transform.rotate(radians);
        transform.translate(-originalWidth / 2.0, -originalHeight / 2.0);

        g2d.transform(transform);
        g2d.drawImage(original, 0, 0, null);
        g2d.dispose();

        return rotatedImage;
    }

    // 组合方法:直接旋转 ImageIcon 并返回 BufferedImage
    public static BufferedImage rotateIconToBufferedImage(ImageIcon icon, double degrees) {
        BufferedImage original = iconToBufferedImage(icon);
        return rotateBufferedImage(original, degrees);
    }

    // 测试用例
    public static void main(String[] args) {
        ImageIcon icon = new ImageIcon("your-image-path.png"); // 替换为你的图片路径
        BufferedImage rotatedImage = rotateIconToBufferedImage(icon, 90); // 旋转90度

        // 这里可以添加保存图像或使用图像的逻辑
        // 比如用 ImageIO.write(rotatedImage, "PNG", new File("rotated.png"));
    }
}

关键细节说明

  • 类型兼容处理:先判断ImageIcon的Image是否已经是BufferedImage,避免重复转换
  • 避免图像裁剪:通过三角函数计算旋转后的精确尺寸,确保完整显示旋转后的图像
  • 高质量渲染:设置RenderingHints参数,提升旋转后的图像清晰度
  • 坐标系平移:旋转前将原点移到图像中心,防止旋转后图像偏移

内容的提问来源于stack exchange,提问作者thatRandomGuy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 23:20:38