Results 1 to 2 of 2
  1. #1
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default BuffededImage cache

    Hello everyone

    I've been using BufferedImage objects for some time and I noticed something strange. When I use the ImageIO.read() method to read an image from the hard drive and I draw the image somewhere in a thread, I found that it is very slow. But, if I load the image in some BufferedImage object, say A, create a new Instance of BufferedImage B and draw A onto B using the BufferedImage.getGraphics() method, I found that rendering B is much faster. Can someone please explain this and perhaps show me a better way of using the BufferedImage for performance.

    Thank you :D
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Try an image type based on your local configuration.
    On ms vista I get:
    Java Code:
    C:\jexp>java ImageTypes
    loaded image type   = 5
    compatible dst type = 1
    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class ImageTypes {
        private JScrollPane getContent(BufferedImage image) {
            System.out.println("loaded image type   = " + image.getType());
            JPanel panel = new JPanel(new GridLayout(1,0));
            panel.add(wrap(image));
            panel.add(wrap(convert(image)));
            panel.add(wrap(convert(image, BufferedImage.TYPE_INT_RGB)));
            return new JScrollPane(panel);
        }
    
        private BufferedImage convert(BufferedImage src) {
            int w = src.getWidth();
            int h = src.getHeight();
            BufferedImage dst =
                GraphicsEnvironment.getLocalGraphicsEnvironment()
                                   .getDefaultScreenDevice()
                                   .getDefaultConfiguration()
                                   .createCompatibleImage(w, h);
            // Component has a [i]getGraphicsConfiguration[/i] method.
            System.out.println("compatible dst type = " + dst.getType());
            Graphics2D g2 = dst.createGraphics();
            g2.drawImage(src,0,0,null);
            g2.dispose();
            return dst;
        }
    
        private BufferedImage convert(BufferedImage src, int type) {
            int w = src.getWidth();
            int h = src.getHeight();
            BufferedImage dst = new BufferedImage(w, h, type);
            Graphics2D g2 = dst.createGraphics();
            g2.drawImage(src,0,0,null);
            g2.dispose();
            return dst;
        }
    
        private JLabel wrap(BufferedImage image) {
            JLabel label = new JLabel(new ImageIcon(image));
            label.setHorizontalAlignment(JLabel.CENTER);
            return label;
        }
    
        public static void main(String[] args) throws IOException {
            String path = "images/blackBear.jpg";
            BufferedImage image = ImageIO.read(new File(path));
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new ImageTypes().getContent(image));
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

Similar Threads

  1. clear cache
    By Jadellll in forum New To Java
    Replies: 0
    Last Post: 03-20-2008, 09:27 AM
  2. JSP - using connection cache
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-30-2008, 09:54 AM
  3. cache problem in jsp
    By lpwing in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 01-15-2008, 07:43 AM
  4. how to restict cache
    By fred in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-24-2007, 01:56 AM
  5. Caché Monitor 0.35
    By levent in forum Java Software
    Replies: 0
    Last Post: 06-10-2007, 01:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •