Java : BufferedImage to Bitmap format
When you say "into Bitmap format" you then mean the data (as in a byte array)? If that's the case, then you can use ImageIO.write
(like suggested above).
If you don't want to save it to a file, but just want to get the data, can you use a ByteArrayOutputStream
like this:
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(img, "BMP", out);
byte[] result = out.toByteArray();
To see the image types available for write in the J2SE (ex. JAI), see ImageIO.getWriterFileSuffixes()
:
E.G.
class ShowJavaImageTypes {
public static void main(String[] args) {
String[] imageTypes =
javax.imageio.ImageIO.getWriterFileSuffixes();
for (String imageType : imageTypes) {
System.out.println(imageType);
}
}
}
Output
For this Sun Java 6 JRE on Windows 7.
bmp
jpg
wbmp
jpeg
png
gif
Press any key to continue . . .
See similar ImageIO
methods for MIME types, formats, and the corresponding readers.
You need to have a look at ImageIO.write
.
- The Java Tutorials: Writing/Saving an Image
If you want the result in the form of a byte[]
array, you should use a ByteArrayOutputStream
:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(yourImage, "bmp", baos);
baos.flush();
byte[] bytes = baos.toByteArray();
baos.close();