1

I am working on an application which takes images in a folder and shows them as thumbnails for further selection and operation individually. below is the code that adds them to Jtogglebuttons.

toglBtn=new JToggleButton(""+i,new ImageIcon(ImageIO.read(new File(listOfFiles[i].getAbsolutePath())).getScaledInstance(139, 163, BufferedImage.SCALE_SMOOTH)));

My original images in the folder are <100kb but the Jtogglebutton size is 6mb. Is there any way to reduce the size of these? currently its taking up all my heap space and giving me an out of memory error when ever there are more than 40 files. I already increased the heap size to 512MB but after analyzing heap dumps with MAT, i figured i need to reduce the thumbnailsizes to solve this error.

Is there any way i can force the thumbnail to be a jpeg? or any other way of adding thumbnails to jtogglebuttons?

Final Update: I used thumbnailator to get the size of the button down to a few kb

try{
                            BufferedImage originalImage = ImageIO.read(new File(listOfFiles[i].getAbsolutePath()));                             
                            Image image =Thumbnails.of(originalImage).size(138, 163).asBufferedImage();
                            toglBtn=new JToggleButton(""+i,new ImageIcon(image));
                            }
DJR
  • 454
  • 2
  • 8
  • 29
  • Presumably, the images are compressed (ei jpg), so when they are loaded, they need to uncompressed so they can be rendered. You can try reducing the physical size and color deepness of the images. You could also limit the number of images you have loaded at any one time. – MadProgrammer Feb 06 '13 at 20:33
  • How can i reduce the color deepness of the thumbnail? the original ones are black and white images of documents if it helps. – DJR Feb 06 '13 at 20:50
  • have to use XxxImage.getScaledInstance, there you can to set desired pixels ratio, notice this method is asynchronous – mKorbel Feb 06 '13 at 21:04
  • @DJR I would be reducing the color deepness and pixel size of the original images, that way you can reduce the memory usage required when reading them in. You can use something like PhotoShop to reduce the color deepness (down to 256, 16, 4 or 2 colors) – MadProgrammer Feb 06 '13 at 21:08
  • that cant be done in my case. My original files could be in thousands. Also, even if i reduce the colordeepness of a 100kb image, i dont see how the thumbnailsize is going drastically down from 6MB. – DJR Feb 06 '13 at 21:30

1 Answers1

1

Perhaps your one Java line is keeping the original image, the scaled image, and the image icon in memory.

Breaking up the code by doing it this way ensures that the original image and scaled image are dropped for garbage collection.

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JToggleButton;

    String text = "" + i;
    File imageFile = new File(listOfFiles[i].getAbsolutePath());
    BufferedImage image = ImageIO.read(imageFile);
    Image scaledImage = image.getScaledInstance(139, 163, 
            BufferedImage.SCALE_SMOOTH);
    ImageIcon imageIcon = new ImageIcon(scaledImage);
    toglBtn = new JToggleButton(text, imageIcon);
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • Gilbert, I get the following exception when i try your method. Exception in thread "main" java.lang.ClassCastException: sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage – DJR Feb 11 '13 at 14:39
  • You have the wrong import. Change "sun.awt.image.ToolkitImage" to "java.awt.Image". – Gilbert Le Blanc Feb 11 '13 at 16:33
  • sun.awt.image.ToolkitImage is not in my list of imports :( – DJR Feb 11 '13 at 17:00
  • I put the code into Eclipse and got the list of imports that I put in my answer. Make sure your imports match. – Gilbert Le Blanc Feb 11 '13 at 17:12
  • Thanks for the help Gilbert. Your method worked but it did not reduce the size of the JtoggleButton for me. I used Thumbnailator library @ [link](https://code.google.com/p/thumbnailator/) finally to create thumbnails on the fly and add them to jTogglebuttons. This helped reduce my button size to a few thousand KB from 6 MB. – DJR Feb 15 '13 at 15:47