Results 1 to 6 of 6

Thread: Image, BufferedImage

  1. #1

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Image, BufferedImage

    I use Toolkit.createImage to load images from my harddisk. This returns a java.awt.Image (the runtime class is sun.something.windows.WImage). But I need a java.awt.image.BufferedImage.
    My current converter takes the dimensions of the Image and creates a new BufferedImage with those dimensions. Then I retrieve a Graphics2D from the BufferedImage and draw the Image inside. Finally I return the BufferedImage.

    This works, but it is very slow. The total startup time for my app ranges from 30 seconds to 2 minutes! And most of it is the time I require to load the images.

    Does anyone know a better way to convert Image to BufferedImage?
    Current code:
    Code:
    private static class ImgListener implements java.awt.image.ImageObserver
    {
    	public boolean imageUpdate(java.awt.Image img, int f, int x, int y, int w, int h) {
    		return ((f & java.awt.image.ImageObserver.ALLBITS) == 0);
    	}
    }
    
    private static BufferedImage createBufferedImage(java.awt.Image img)
    {
    	// this is damn slow!
    	ImgListener listener = new ImgListener();
    	int width;
    	while((width=img.getWidth(listener)) < 0)
    		;
    	int height;
    	while((height=img.getHeight(listener)) < 0)
    		;
    	BufferedImage out = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    	java.awt.Graphics2D g = out.createGraphics();
    	while(!g.drawImage(img, new java.awt.geom.AffineTransform(), listener))
    		;
    	return out;
    }
    Thanks in advance!
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  2. #2
    New Member
    Join Date
    Dec 2002
    Location
    Saudi Arabia , Riyadh
    Posts
    8
    I think u should use this


    Toolkit.getDefaultToolKit().getImage()
    Allah only forbids you, with regard to those who fight you for (your) Faith, and drive you out, of your homes, and support (others) in driving you out, from turning to them (for friendship and protection). It is such as turn to them (in these circumstances), that do wrong. [The Holy Quran 60:9]
    http://www.al-islam.com

  3. #3

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I don't think so. From the Sun Docs:
    ... developers are encouraged to implement their own caching of images by using the createImage variant wherever available.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    And it doesn't solve my original problem of not having a BufferedImage and needing to long to convert.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    New Member
    Join Date
    Dec 2002
    Location
    Saudi Arabia , Riyadh
    Posts
    8
    woudn't this help ?
    PHP Code:

    BufferedImage getBufferedImage
    (Image image)
    {
            
    int imageWidth  image.getWidth(this);
            
    int imageHeight image.getHeight(this);
                    
    BufferedImage buffImage = new BufferedImage(imageWidthimageHeightBufferedImage.TYPE_INT_RGB);
                        
            return 
    buffImage;
        } 
    Allah only forbids you, with regard to those who fight you for (your) Faith, and drive you out, of your homes, and support (others) in driving you out, from turning to them (for friendship and protection). It is such as turn to them (in these circumstances), that do wrong. [The Holy Quran 60:9]
    http://www.al-islam.com

  6. #6

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    That's what I already do. Except in my code there is a part that copies the content of the Image to the BufferedImage.

    Thanks, but this won't help me.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

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



Click Here to Expand Forum to Full Width