Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BufferedImage to BMP in Java

I have a BufferedImage object and I want to encode it to the BMP format and save it to disk.

How do I do this?

In JPEG it's ok:

BufferedImage img; //here is an image ready to be recorded into the hard disk
FileOutputStream fout = new FileOutputStream("image.jpg");

JPEGImageEncoder jencoder = JPEGCodec.createJPEGEncoder(fout);
JPEGEncodeParam enParam = jencoder.getDefaultJPEGEncodeParam(img);

enParam.setQuality(1.0F, true);
jencoder.setJPEGEncodeParam(enParam);
jencoder.encode(img);

fout.close();
like image 933
Eduardo Abreu Avatar asked Oct 18 '10 17:10

Eduardo Abreu


People also ask

What is BufferedImage in Java?

A BufferedImage is comprised of a ColorModel and a Raster of image data. The number and types of bands in the SampleModel of the Raster must match the number and types required by the ColorModel to represent its color and alpha components. All BufferedImage objects have an upper left corner coordinate of (0, 0).

How do you create a bitmap image in Java?

To create a bitmap from a resource, you use the BitmapFactory method decodeResource(): Bitmap bitmap = BitmapFactory. decodeResource(getResources(), R. drawable.

What is the use of BufferedImage?

It is used to handle and manipulate the image data. A BufferedImage is made of ColorModel of image data. All BufferedImage objects have an upper left corner coordinate of (0, 0).


2 Answers

Use ImageIO -

ImageIO.write(img, "BMP", new File("filename.bmp"))
like image 155
Marc Avatar answered Oct 05 '22 18:10

Marc


Something like this should do:

ImageIO.write(image, "BMP", new File("filename.bmp"));

where image is the BufferedImage you want to encode.

like image 44
Faisal Feroz Avatar answered Oct 05 '22 20:10

Faisal Feroz