我一直在研究分析顯微鏡數據的軟件,該軟件存儲爲多層tiff文件。在通過StackOverflow和JAI文檔查看之後,我將一些代碼放在一起以存儲tiff堆棧並正確呈現它。使用JAI在Java中進行TIFF渲染可獲得更快的性能
但是,它遭受了一些相當不好的表現。我曾希望看到這樣的帖子後有一些更快的表現: Anyone have any luck writing a very fast tiff viewer/editor in Java?
不幸的是,它不像我希望的那麼好。我沒有太多與外部圖像庫或Java圖形功能有關的經驗,所以我不確定我會如何改進這一點。
對於某些方面,這裏是「口吃」的視頻,同時通過TIFF堆穿越我的經驗: http://www.youtube.com/watch?v=WiR4o6TsqyM&feature=channel_video_title
注意幀偶爾口吃如何隨着滑塊拖動。
當縮放圖像時,通過移除平滑來縮放圖像,但改善後的性能仍然不如我喜歡的那麼快。
我的代碼是下面:
/** Converts the RenderedImage into a BufferedImage, which is more flexible
* @param img The original RenderedImage
* @return The new BufferedImage
*/
public BufferedImage convertRenderedImage(RenderedImage img) {
if (img instanceof BufferedImage) {
return (BufferedImage)img;
}
ColorModel cm = img.getColorModel();
int width = img.getWidth();
int height = img.getHeight();
WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
Hashtable<String, Object> properties = new Hashtable<String, Object>();
String[] keys = img.getPropertyNames();
if (keys!=null) {
for (int i = 0; i < keys.length; i++) {
properties.put(keys[i], img.getProperty(keys[i]));
}
}
BufferedImage result = new BufferedImage(cm, raster, isAlphaPremultiplied, properties);
img.copyData(raster);
return result;
}
/** Draws everything on top of the scaled image
* @param scale The current scale value
*/
private void setScaledImage(double scale)
{
//Optimizes the image type for drawing onto the screen
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice device = env.getDefaultScreenDevice();
GraphicsConfiguration config = device.getDefaultConfiguration();
int w = (int)(scale*currImage.getWidth());
int h = (int)(scale*currImage.getHeight());
scaled = config.createCompatibleImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = scaled.createGraphics();
g2.drawImage(currImage, 0, 0, w, h, null);
g2.dispose();
}
我已經加載多層TIFF與JAI一個imageDecoder,並以顯示正確的層滑塊被拖動時,我使用以下代碼:
try {
currImage = convertRenderedImage(imageStack.decodeAsRenderedImage(currentLayerSlider.getValue()-1));
setScaledImage (scaleSlider.getValue()/100.0);
curves.changeFrame(currentLayerSlider.getValue());
drawImageOverlay();}
catch (IOException e) {
e.printStackTrace();
currImage = null;}
基本上,無論何時拖動滑塊,我都會從imageStack中提取渲染圖像,將其轉換爲BufferedImage(以便它可以與ImageIcon一起使用),縮放它並將其設置爲顯示的圖像。我認爲緩慢的部分是將其轉換爲緩衝圖像,並將其縮放。有什麼辦法可以更快地做到這一點?
有沒有人有任何建議如何改善事情,或從類似的經驗見識?
任何幫助將不勝感激。