Improve performance when working with images in Java
I'm writing a program that includes an image folder (usually about 2000 JPEG images) to resize them and add them to the image timeline The results are as follows:
It's good, but the way I do it seems very inefficient The code for processing these images is as follows:
public void setTimeline(Vector<String> imagePaths){ int numberOfImages = imagePaths.size(); JLabel [] TotalImages = new JLabel[numberOfImages]; setGridPanel.setLayout(new GridLayout(1,numberOfImages,10,0)); Dimension image = new Dimension(96,72); if (imagePaths != null){ for(int i = 0; i <numberOfImages; i++){ TotalImages[i] = new JLabel(""); TotalImages[i].setPreferredSize(image); ImageIcon tempicon = new ImageIcon(imagePaths.elementAt(i)); Image tempimage = tempicon.getImage(); Image newimg = tempimage.getScaledInstance(96,72,java.awt.Image.SCALE_SMOOTH); ImageIcon newIcon = new ImageIcon(newimg); TotalImages[i].setIcon(newIcon); setGridPanel.add(TotalImages[i]); } } }
As you can see, this code loops through each image path, adds it to the label and adds it to the panel - exactly according to the correct output
However, it took a long time to do so For 2000 images, it usually takes about 5 minutes (depending on the machine) I wonder if there is any way to improve this performance by using different technologies?
Any help is greatly appreciated
Solution
Save scaled instances and load them directly Hard disk space is cheap This does not affect the initial cost of generating a thumb, but any subsequent occurrence will be lightning fast