user3725938
user3725938

Reputation: 257

Change the format of an Image in java without saving

I would like to read an image and then change its format but without saving it.

For example I can read the image like this
BufferedImage img=ImageIO.read(new File(fileName));

Then I want to change the format of img, for example from jpeg to png. The only way I found is to use ImageIO.read to write and then read the new image, but it does seem to be an efficient way to do it.

Upvotes: 0

Views: 1226

Answers (1)

leonbloy
leonbloy

Reputation: 76016

When you "read" the image via

BufferedImage img=ImageIO.read(new File('myimage.png'));

you are not only reading but also decoding it, i.e., transforming the raw bytes in the (say) PNG format to some "RAW" format that your aplication or API can manipulate (or display) - in this case, a BufferedImage. Once this is done, the fact that this image came from a PNG file is forgotten. To read it as PNG and save it as JPEG you need to decode it (as PNG) and then code it (as JPG).

I would like to read an image and then change its format but without saving it.

The "format" of the image (in the PNG/JPEG sense) gives you a way of packing an image in a sequence of bits. So, your desire makes little sense. At most, you could store those bits in memory (what for?), but that would be the same as "saving it" (to memory instead of disk).

Upvotes: 3

Related Questions