Hello,
In my code, im loading a bunch of images to create a sort of gallery,
I want to know if there is a faster way to load images than what i have done here.. it seems really slow and resource intensive
This is my code..
Code:Thread t = new Thread(){ public void run(){ GlobalVariables.setLoadImageThreads(GlobalVariables.getLoadImageThreads()+1); if(GlobalVariables.PET_VIEW_IS_ACTIVE){ JLabel imgLabel = new JLabel(); imgLabel.setName(pic.getPreviewName()); imgLabel.setToolTipText("Double click to open"); imgLabel.addMouseListener(new LabelImageMouseListener(tablePictures, pic.getPictureId(), pic.getFilePath())); //imgLabel.setBorder(BorderFactory.createLineBorder(Color.WHITE, 1, false)); imgLabel.setBorder(BorderFactory.createLineBorder(Color.WHITE, 1)); PictureUtil.setImageToLabelIcon(pic.getFilePath(), pic.getPreviewName(), imgLabel, 100, 100); panePictures.add(imgLabel); } GlobalVariables.setLoadImageThreads(GlobalVariables.getLoadImageThreads()-1); } }; loadImageThreads.add(t);
This is the code for PictureUtil.setImageToLabelIcon
This is the saveImage method im using, note that i only use this when a thumbnail does not exist. If it exists, it just loads the image.Code:public static void setImageToLabelIcon(String filePath, String previewName, JLabel label, int maxW, int maxH) { try { int maxWidth = maxW; int maxHeight = maxH; int newWidth = 0; int newHeight = 0; if (previewName == null) { previewName = "000"; } BufferedImage scaledImage = null; File previewImg = new File("imgs/" + previewName + ".jpg"); if (previewImg.exists()) { scaledImage = ImageIO.read(previewImg); } else { BufferedImage myPicture = ImageIO.read(new File(filePath)); double percentDrop = 1d; double percentDrop1 = 1d; double percentDrop2 = 1d; double width = myPicture.getWidth(); double height = myPicture.getHeight(); if (width > maxWidth) { percentDrop1 = maxWidth / width; } if (height > maxHeight) { percentDrop2 = maxHeight / height; } if (percentDrop1 > percentDrop2) { percentDrop = percentDrop2; } else { percentDrop = percentDrop1; } newWidth = (int) (Math.floor(myPicture.getWidth() * percentDrop)); newHeight = (int) (Math.floor(myPicture.getHeight() * percentDrop)); scaledImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = scaledImage.createGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.drawImage(myPicture, 0, 0, newWidth, newHeight, null); graphics2D.dispose(); PictureUtil.flushImages(myPicture); if(!previewName.equals("000")) PictureUtil.saveImage(scaledImage, previewName, "jpg"); } label.setIcon((new ImageIcon(scaledImage))); PictureUtil.flushImages(scaledImage); } catch (IOException ex) { Logger.getLogger(PictureUtil.class.getName()).log(Level.SEVERE, null, ex); } }
Code:public static void saveImage(BufferedImage image,String fileName, String ext) { File dir = new File("imgs/"); if(!dir.exists()){ dir.mkdir(); } File file = new File("imgs/"+fileName + "." + ext); try { if(!file.exists()) ImageIO.write(image, ext, file); } catch(IOException e) { System.out.println("Write error for " + file.getPath() +": " + e.getMessage()); } }
Individually, an image doesnt really take long, but if I want to load 700 images.. this takes minutes...
This is what i did to try to make this faster...
Each thread in this arraylist contains threads from the first piece of code.
Code:int i = 0; int limitIncrease = 10; int nextLimit = limitIncrease; long timeSpentWaiting = 0; for(Thread t:loadImageThreads){ if(GlobalVariables.LOAD_IMAGES_FAST && GlobalVariables.PICTURE_TAB_ACTIVE){ limitIncrease = 10; }else{ limitIncrease = 1; try { Thread.sleep(100); } catch (InterruptedException ex) { Logger.getLogger(LoadThumbnails.class.getName()).log(Level.SEVERE, null, ex); } } //VALIDATES THAT THE WINDOW IS OPEN, TO AVOID UNNECESSARY LOADING. if(!GlobalVariables.PET_VIEW_IS_ACTIVE){ break; } while(GlobalVariables.getLoadImageThreads() > 0 && i == nextLimit && nextLimit != loadImageThreads.size()){ try { if(timeSpentWaiting > 10000){ GlobalVariables.setLoadImageThreads(0); } timeSpentWaiting += 200; Thread.sleep(200); } catch (InterruptedException ex) { Logger.getLogger(LoadThumbnails.class.getName()).log(Level.SEVERE, null, ex); } } timeSpentWaiting = 0; if(i==nextLimit){ nextLimit += limitIncrease; //ONLY UPDATE THE UI IF THE PICTURE TAB IS ACTIVE if(GlobalVariables.PICTURE_TAB_ACTIVE){ panePictures.updateUI(); } } t.start(); i++; }
i hope someone can help me with this.. thanks in advance![]()


Reply With Quote

Bookmarks