views:

100

answers:

1

Is it correct that if we have a BufferedImage object in java, we could potentially write it out in ANY format using ImageIO.write (if we have a Writer object for the same)? I tried writing a BufferedImage object into a jpg file, it outputted an empty image file however when i tried writing it in to a png file, it worked fine.

A: 

A BufferedImage can be written into any format that ImageIO.write supports (that is, has an ImageWriter available), yes. Please show the code if you can't get it to work.

Joonas Pulakka
Hey, I think i diagnosed the problem. For my images the Image Type is TYPE_USHORT_GRAY, and they use 16 bits per sample to represent the data. I dont think jpeg supports 16 bits per sample, it supports just 8 (as all 8 bits per sample images write to disk as jpg correctly). I read that PNG and TIFF support 16 bits per sample so seems to work fine when i write them as PNG (or TIFF). Could this be a plausible reason? Again, not too sure why empty files are being written and an exception is not thrown?
I think the ImageWriter ought to do any needed conversions. But then again, I could be wrong! But please post the code if you need further help.
Joonas Pulakka