在java中旋转图像

时间:2012-05-21 12:10:33

标签: java graphics

我有一张Pan Card图像,当我尝试将它旋转45度并保存时,我得到一张裁剪图像。 旋转图像的代码是:

    BufferedImage dimg = new BufferedImage(w, h, img.getType());
    Graphics2D g = dimg.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // Anti-alias!
            RenderingHints.VALUE_ANTIALIAS_ON);

    g.rotate(Math.toRadians(angle), w / 2, h / 2);

    g.drawImage(img, null, 0, 0);

1 个答案:

答案 0 :(得分:7)

使用AffineTransform:

查看此示例

http://www.billthelizard.com/2008/07/rotate-image-in-java.html

有一些代码加载图像,然后这就是核心:

private Image image;
AffineTransform identity = new AffineTransform();

Graphics2D g2d = (Graphics2D)g;
AffineTransform trans = new AffineTransform();
trans.setTransform(identity);
trans.rotate( Math.toRadians(45) );
g2d.drawImage(image, trans, this);