我想讀取.CBZ存檔內的圖像並將它們存儲在ArrayList中。我嘗試了以下解決方案,但它至少有2個問題。從.cbz存檔讀取圖像
- 我加入10-15圖像到ArrayList
- 必須有獲取圖像的ArrayList內,而不是一遍上的臨時文件,並閱讀他們寫一個更好的方法後得到一個內存不足錯誤。
public class CBZHandler {
final int BUFFER = 2048;
ArrayList<BufferedImage> images = new ArrayList<BufferedImage>();
public void extractCBZ(ZipInputStream tis) throws IOException{
ZipEntry entry;
BufferedOutputStream dest = null;
if(!images.isEmpty())
images.clear();
while((entry = tis.getNextEntry()) != null){
System.out.println("Extracting " + entry.getName());
int count;
FileOutputStream fos = new FileOutputStream("temp");
dest = new BufferedOutputStream(fos,BUFFER);
byte data[] = new byte[BUFFER];
while ((count = tis.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
BufferedImage img = ImageIO.read(new FileInputStream("temp"));
images.add(img);
}
tis.close();
}
}
ImageIO從InputStream中讀取數據。爲什麼不將ZipInputStream('tis')傳遞給'ImageIO.read()'?圖像大小是多少(以字節爲單位),JVM的堆大小是多少? – 2012-03-10 18:38:58
瞭解這些圖像的分辨率(寬*高,可能* colordepth)也有助於估計內存使用量。 – 2012-03-10 18:52:10