Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

BufferedImage to Image  RSS feed

 
paul wheaton
Trailboss
Posts: 22258
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a need to keep a few hundred images in memory. Most of these images started off as BufferedImage. Once I do the image mangling, I don't need the whiz-bang mangling ability that comes with BufferedImage. I also assume that BufferedImage takes up a lot more memory than Image does.
How can I extract a "regular" Image from BufferedImage?
 
paul wheaton
Trailboss
Posts: 22258
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmmm, I found Image.getScaledInstance() - seems like the quality could be degraded though ....
 
Terence Doyle
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I was curious about that method so I've read up on it in a few places and as you just want to get an Image object but not any smaller why not try

but passing in the actual image size and SCALE_REPLICATE as the hint.
This scaling technique removes or duplicates rows and columns of pixels to achieve the desired size.
If the new size is to be the same as the old BufferedImage then the pixels should be left unchanged.
Hope that works out.
Bye,
Terry
 
paul wheaton
Trailboss
Posts: 22258
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Terry - that seems to be doing the trick!
 
paul wheaton
Trailboss
Posts: 22258
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bad news - it doesn't work. I think it is just casting the object. Cuz when I would tinker with the BufferedImage after using the getScaledInstance() my copied image would change - less than optimal.
 
Terence Doyle
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again,
I looked at the API again and this is the quote for the getScaledInstance() method:
Creates a scaled version of this image. A new Image object is returned .....
If you have finished handling the bufferedImage and you want to reduce system resources why not get the scaled version, perform flush()on the old bufferedImage and set it to null?
I'd love to have the time to try this out but it's 1am over here in Spain so I'm signing off for now.

Bye,
Terry
 
Daniel Searson
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I may be wrong about this, but it is entirely possible that getScaledInstance() simply produces another BufferedImage, upcasts it to an Image and then returns it (you might want to check the source code).
In effect, you could get an Image reference by doing:
//where someBufferedImage is a BufferedImage
Image img = (Image)someBufferedImage;
This wouldnt save any memory because the object would still be a BufferedImage in memory - but an Image reference in code.
 
Terence Doyle
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I looked at the source for that method in the Image class and here it is:

The filtering seems to grouped a little too casually.
Anyway the createImage(prod) takes us over to an abstract method in Toolkit
Which in turn takes us over to Component.createImage()

And that's where I get lost ....
I'm not sure what would happen if the method returned back to the Toolkit and tried to invoke an abstract unimplemented method ?? Any ideas people?
Bye,
Terry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!