2011-05-13 69 views
2

我知道字節,無符號短符號和整數在內存使用方面的差異,但是當談到BufferedImage時,它們之間是否存在「速度」差異?

我一直在我的代碼中使用圖像類型來存儲圖像,但我需要一個alpha層。使用BufferedImage爲我提供了ARGB,但是在從Image類型進行更改(並且僅針對少數對象進行更改)後,我的代碼變得/相當/較慢,因此我正在尋找可以獲得的所有性能改進。

我不知道這可能是多麼愚蠢的問題,所以我感謝你回覆任何答覆。BufferedImage INT/4BYTE/USHORT

+0

你的分析器說什麼? – trashgod 2011-05-13 01:31:17

+0

'探查者'是什麼意思?例如,可以使用['jvisualvm'](http://download.oracle.com/javase/6/docs/technotes/tools/share/jvisualvm.html)[ – Tanaki 2011-05-14 01:06:11

+0

]。 – trashgod 2011-05-14 01:52:03

回答

2

棚木,

我已經發現,當需要使用在一個BufferedImage alpha通道的,最好的是到預乘alpha通道。例如:

 
// Create an ARGB image 
BufferedImage bi = new BufferedImage(512, 512, BufferedImage.TYPE_INT_ARGB); 
Graphics2D g = bi.createGraphics(); 
// Fill the background (for illustration) 
g.setColor(Color.black); 
g.fill(new Rectangle(0, 0, 512, 512)); 

AlphaComposite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f)); 
// Keep the original composite 
Composite original = g.getComposite(); 
g.setComposite(alpha); 

// Paint with transparency 
Rectangle r = new Rectangle(100, 200, 50, 50); 
g.setColor(Color.magenta); 
g.fillRect(r); 
g.setComposite(original); 
// ... paint further shapes or images as necessary 
// ... 
g.dispose(); 

// Convert to a premultiplied alpha image for fast painting to a Canvas 
BufferedImage biPre = new BufferedImage(512, 512, BufferedImage.TYPE_INT_ARGB_PRE); 
Graphics2D gPre = biPre.createGraphics(); 
gPre.drawImage(bi, 0, 0, null); 
gPre.dispose(); 

// clean up: 
bi.flush(); 


// Now use biPre for painting to a Canvas, or a Component. 
// ... 

// Remember to flush it when done! 
biPre.flush(); 

其原因首先畫一個TYPE_INT_ARGB是確保你希望的所有字母被塗(不預乘每一次!)。然後,完成後,將整個圖像繪製到TYPE_INT_ARGB_PRE上,然後將數據以良好的速度帶到屏幕上。

+0

非常感謝,這清除了我的問題。 – Tanaki 2011-06-14 23:16:06