Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thumbnail creation in Java

I want to create the thumbnails for a group of images. for that, I am using the following code.

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;
}

This code is working fine and creating thumbnails. But the problem is that, in case of large number of images, I am getting "java heap space error".Is it the problem with this code? How can i solve this issue. Thanks in advance. If you have any other code for resizing, please give to me.

like image 514
laradev Avatar asked Dec 19 '25 13:12

laradev


1 Answers

Try this Image Scaling Library, it works fine for me.

http://www.thebuzzmedia.com/software/imgscalr-java-image-scaling-library/

like image 125
edze Avatar answered Dec 24 '25 12:12

edze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!