我想为一组图像创建缩略图.为此,我正在使用以下代码.
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堆空间错误”.此代码是否有问题?我该如何解决这个问题.提前致谢.
如果您还有其他调整大小的代码,请给我.