2012-05-21 185 views
4

我有一个潘卡的图像,当我尝试将它旋转45度并保存时,我得到一个裁剪后的图像。 代码旋转图像是:在java中旋转图像

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

选中此项:http://stackoverflow.com/questions/2687926/how-can-i-rotate-an-image-using-java-swing-and-then-set-its-origin-to-0 -0 – Ponmalar

+0

还有一件事我想包括一些功能,如缩放,裁剪,在我的项目中可以得到我的项目的任何实用程序jar ... – Azuu

回答

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); 
+1

你如何定位转换后的图像? drawImage方法似乎没有这个构造函数 –