我正在編寫一個程序,涉及到許多圖像的自定義搜索。當用戶輸入他們的搜索條件時,相應的圖像將從緩存或服務器中獲取。我有一個顯示指向相應圖像的鏈接的JTable。當用戶點擊鏈接時,圖像顯示在JPanel上。點擊鏈接時,我可以從硬盤讀取圖像,但速度並不像我想的那麼快。加載它需要幾秒鐘的時間。我已經嘗試創建一個BufferedImage數組,但如果搜索返回許多結果,我去奧姆。我想知道什麼是點擊鏈接時更快速地顯示圖像的最佳方式。在java中加載很多緩衝圖像100+到內存
繼承人我如何獲取圖片...
Public void getFile(String fileName){
File file = new File("./cache/"+fileName);
boolean exists = file.exists();
BufferedImage returnImage =null;
if(exists){
try {
returnImage = ImageIO.read(file);
System.out.println("Found In Cache!");
} catch (IOException|IndexOutOfBoundsException e) {
try {
if(fileName != null){
returnImage = downloadImage(fileName);
System.out.println("Found ON Server :(");
}
} catch (IOException |IndexOutOfBoundsException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
}
}else{
try {
if(fileName != null){
returnImage = downloadImage(fileName);
System.out.println("Found ON Server :(");
}
} catch (IOException |IndexOutOfBoundsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return returnImage;
}
}
而這正是我把它們放在一個數組...
BufferedImage[] images = new BufferedImage[numOfSearchResults];
for(SearchResult r: results){
images[i] = imageCache.getFile(r.imageName);
}
基本上我只是想知道會是什麼最好的方式來預先加載圖像,而不會發生。提前致謝
從磁盤加載圖像並顯示它不應該花費幾秒鐘,除非它是一個非常大的圖像(磁盤空間很大尺寸)。你有沒有分析你的應用程序,看看圖像加載是否確實是瓶頸? – Robin 2012-07-25 16:14:40
什麼是奧姆? – 2012-07-25 16:15:52
@SteveKuo我編輯了包含正確的標記。 – 2012-07-25 16:16:14