Image to BufferedImage

M

Martin Thomas

Hi,

simple question and hopefully a simple solution:
How can i convert an java.awt.Image into a BufferedImage?

Thanks

Martin
 
V

VisionSet

Martin Thomas said:
Hi,

simple question and hopefully a simple solution:
How can i convert an java.awt.Image into a BufferedImage?

Chances are it already is!

BufferedImage is the single concrete subclass of Image (in current 1.4.2
API).

cast it to BufferedImage in a safe manner.

Other possibility is a VolatileImage from which you can get a BufferedImage
with getSnapshot().
 
T

Thomas Weidenfeller

Martin said:
Hi,

simple question and hopefully a simple solution:
How can i convert an java.awt.Image into a BufferedImage?

If you are lucky, a cast will do. If not, if you really have an unknown
Image subclass, well it's not fun:

* Get the Image's consumer with Image.getSource().

* Use a PixelGrabber to read the image data from the consumer into some
pixel array

* Create a DataBufferInt from the pixel data

* Create a WritableRaster using the DataBufferInt

* Create a BufferedImage using the WritableRaster

* curse, wait, curse, wait, curse, curse, curse :)


There might be better ways.

/Thomas
 
A

Adam

Hi,
simple question and hopefully a simple solution:
How can i convert an java.awt.Image into a BufferedImage?

If it is not already a BufferedImage (check with instanceof)
you can create a new BufferedImage with the same size,
and bufferedImage.getGraphics().drawImage(image, 0 , 0, null);

However I don't know what can possibly happen
if image si not yet fully loaded/decoded.

Adam
 
Joined
Mar 24, 2009
Messages
2
Reaction score
0
Thomas Weidenfeller said:
Martin Thomas wrote:

* Use a PixelGrabber to read the image data from the consumer into some
pixel array

* Create a DataBufferInt from the pixel data

* Create a WritableRaster using the DataBufferInt

* Create a BufferedImage using the WritableRaster

* curse, wait, curse, wait, curse, curse, curse :)


There might be better ways.

/Thomas


This is great, but how do I do this, I need to resize an image for my HighSchools project and I have tried many methods, but how do you do this?
 
Joined
Mar 19, 2011
Messages
1
Reaction score
0
Martin Thomas wrote:

* Get the Image's consumer with Image.getSource().

* Use a PixelGrabber to read the image data from the consumer into some
pixel array

* Create a DataBufferInt from the pixel data

* Create a WritableRaster using the DataBufferInt

* Create a BufferedImage using the WritableRaster

* curse, wait, curse, wait, curse, curse, curse :)


There might be better ways.

/Thomas

Thanks. That worked!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top