开发者

Thumbnail creation in Java

开发者 https://www.devze.com 2023-02-19 23:55 出处:网络
I want to create the thumbnails for a group of images. for that, I am using the following code. public void run() {

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);
 开发者_JAVA技巧   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.


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

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


I don't see where you are setting IMG_WIDTH, could it be that this is left at a huge value. You should prefer to pas teh target width amd height as parameters, teh way you set teh height prevents multi-threaded usage and makes it very hard to read. And I doubt the height of the target image is really part of an objects state.

Also, are you sure you are setting the image height correctly, the height is related to a ration between height and width, a narrow image will give you a very tall image and stretched image assuming the width of the target image is to remain constant.

One last thing, http://www.jhlabs.com/ip/filters/index.html has some useful code for image processing (including resizing), I've used these a number of times.

Assuming your images are large, you may want to look at JAI and use either embedded thumbnails or sub-sampling to reduce the memory needed.

public static BufferedImage getThumb(ImageReader reader, int size) throws IOException {
    BufferedImage img;

    try {
        if (reader.getNumThumbnails(0) > 0) {
            img = reader.readThumbnail(0, 0);
        } else {
            ImageReadParam param = reader.getDefaultReadParam();
            param.setSourceSubsampling(4, 4, 0, 0);
            img = reader.read(0); //read(0, param);
        }
        throw new Exception();
    } catch (Throwable t) {
        img = null;
    }

    return img != null ? resizeImage(img, size) : null;
}   

public static BufferedImage getThumb(File file, double scale) throws IOException {
    BufferedImage img = null;

    try {
        Class<?> c = ImageUtil.class.getClassLoader().loadClass("javax.media.jai.JAI");
        Class<?> ic = ImageUtil.class.getClassLoader().loadClass("javax.media.jai.Interpolation");
        Class<?> sc = ImageUtil.class.getClassLoader().loadClass("javax.media.jai.operator.ScaleDescriptor");
        Method jaiCreate = c.getMethod("create", String.class, Object.class);
        Method getInstance = findMethod(ic, "getInstance");
        Method sdCreate = findMethod(sc, "create");

        if (c != null) { 
            Object image = jaiCreate.invoke(null, "fileload", file.getAbsolutePath());              
            Object[] params = { image, (float) scale, (float) scale,
                    0.0f, 0.0f, getInstance.invoke(null, 2), null };
            Object sd = sdCreate.invoke(null, params);
            Method m = sd.getClass().getMethod("getAsBufferedImage");
            img = (BufferedImage) m.invoke(sd);
        }
    } catch (Throwable tt) {
        System.out.println("Could not read image using JAI, maybe JAI is not installed.");
        System.out.println(tt);
    }

    return img;
}


How do you launch this process? This code does seem to be cleaning up the images properly.

Only option is to increase heap size by adding -Xmx 512m (to increase heap size to 512 MB for example) as one of VM command line options.


Creating thumbnails in Java requires the appropriate tool for this: ImageMagick.

Call it from Java, and enjoy the results. It will always be better, more fail safe and faster than everything you can do in Java with Libraries available.


Check thumbnailator

0

精彩评论

暂无评论...
验证码 换一张
取 消