6

我有一张 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);
4

1 回答 1

8

看看这个例子,使用 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);
于 2012-05-21T12:19:11.193 回答