我想爲一組圖像創建縮略圖。爲此,我使用下面的代碼。在Java中創建縮略圖
public void run() {
try{
BufferedImage originalImage = ImageIO.read(new File(url));
int type = originalImage.getType() == 0? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
IMG_HEIGHT = (originalImage.getHeight()*600)/originalImage.getWidth();
BufferedImage resizeImageJpg = resizeImage(originalImage, type);
ImageIO.write(resizeImageJpg, "jpg", new File(thumb));
originalImage.flush();
resizeImageJpg.flush();
System.gc();
}catch(IOException e){
System.out.println(e.getMessage());
System.out.println("Not Created:"+url);
}
}
private static BufferedImage resizeImage(BufferedImage originalImage, int type){
BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
g.dispose();
System.gc();
return resizedImage;
}
此代碼工作正常,並創建縮略圖。但問題是,在大量圖像的情況下,我得到「Java堆空間錯誤」。它是這個代碼的問題?我該如何解決這個問題。提前致謝。 如果您有其他任何調整大小的代碼,請給我。
看來你已經完成堆空間。用你用來閱讀所有圖像的cicle完成代碼 – Heisenbug 2011-03-28 06:46:17