Results 1 to 2 of 2
Thread: BuffededImage cache
- 01-23-2008, 09:53 PM #1
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 :DEyes dwelling into the past are blind to what lies in the future. Step carefully.
- 01-24-2008, 12:00 AM #2
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
-
clear cache
By Jadellll in forum New To JavaReplies: 0Last Post: 03-20-2008, 09:27 AM -
JSP - using connection cache
By Java Tip in forum Java TipReplies: 0Last Post: 01-30-2008, 09:54 AM -
cache problem in jsp
By lpwing in forum JavaServer Pages (JSP) and JSTLReplies: 4Last Post: 01-15-2008, 07:43 AM -
how to restict cache
By fred in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 07-24-2007, 01:56 AM -
Caché Monitor 0.35
By levent in forum Java SoftwareReplies: 0Last Post: 06-10-2007, 01:40 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks