2012-05-21 183 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方法似乎沒有這個構造函數 –