2011-06-03 39 views
0

我想旋轉畫布上的PNG圖像,並且旋轉後圖像質量變得非常差。最初,PNG是透明背景上的箭頭。旋轉後,不可能說它是一個箭頭。使用swt無法旋轉透明圖像Transform

我用下面的代碼:

Transform oldTransform = new Transform(
Display.getCurrent()); 
gc.getTransform(oldTransform); 

Transform transform = new Transform(Display.getCurrent()); 
transform.translate(xm + imageBounds.width/2, ym + imageBounds.height/2); 
transform.rotate(179); 
transform.translate(-xm - imageBounds.width/2, -ym - imageBounds.height/2); 

gc.setTransform(transform); 
gc.drawImage(image, xm, ym); 
gc.setTransform(oldTransform); 

transform.dispose(); 

預先感謝您。

回答

3

相反旋轉圖像180度的,可以在水平和垂直翻轉(沒有任何像素變換):

private BufferedImage flipH(BufferedImage src) { 
     int w = src.getWidth(); 
     int h = src.getHeight(); 
     BufferedImage dst = new BufferedImage(w, h, src.getType()); 
     Graphics2D g = dst.createGraphics(); 
     g.drawImage(src, 
        0, // x of first corner (destination) 
        0, // y of first corner (destination) 
        w, // x of second corner (destination) 
        h, // y of second corner (destination) 
        w, // x of first corner (source) 
        0, // y of first corner (source) 
        0, // x of second corner (source) 
        h, // y of second corner (source) 
        null); 
     g.dispose(); 
     return dst; 
} 

private BufferedImage flipV(BufferedImage src) { 
     int w = src.getWidth(); 
     int h = src.getHeight(); 
     BufferedImage dst = new BufferedImage(w, h, src.getType()); 
     Graphics2D g = dst.createGraphics(); 
     g.drawImage(src, 0, 0, w, h, 0, h, w, 0, null); 
     g.dispose(); 
     return dst; 
} 

... 
BufferedImage flipped = flipH(flipV(ImageIO.read(new File("test.png")))); 
ImageIcon icon = new ImageIcon(flipped); 
... 

編輯:或者甚至更好,翻轉無論是在單個運算的水平和垂直(相同旋轉180度):

g.drawImage(src, 0, 0, w, h, w, h, 0, 0, null); 

EDIT2:另外也SWT-特定圖像旋轉的例子/無翻轉陳sformtoo