How to convert BufferedImage to byte[] in Java
This article shows how to convert a BufferedImage
to a byte array or byte[]
.
BufferedImage bi = ImageIO.read(new File("c:\\image\\mypic.jpg"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, "jpg", baos);
byte[] bytes = baos.toByteArray();
The idea is uses the ImageIO.write
to write the BufferedImage
object into a ByteArrayOutputStream
object, and we can get the byte[]
from the ByteArrayOutputStream
.
1. Convert BufferedImage to byte[]
Below is a Java example of converting a BufferedImage
into a byte[]
, and we use the Base64 encoder to encode the image byte[]
for display purpose. In the end, we also convert the byte[]
back to a new BufferedImage
and save it into a new image file.
ImageUtils.java
package com.mkyong.io.image;
import org.apache.commons.codec.binary.Base64;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
public class ImageUtils {
// convert BufferedImage to byte[]
public static byte[] toByteArray(BufferedImage bi, String format)
throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, format, baos);
byte[] bytes = baos.toByteArray();
return bytes;
}
// convert byte[] to BufferedImage
public static BufferedImage toBufferedImage(byte[] bytes)
throws IOException {
InputStream is = new ByteArrayInputStream(bytes);
BufferedImage bi = ImageIO.read(is);
return bi;
}
public static void main(String[] args) throws IOException {
BufferedImage bi = ImageIO.read(new File("c:\\test\\google.png"));
// convert BufferedImage to byte[]
byte[] bytes = toByteArray(bi, "png");
//encode the byte array for display purpose only, optional
String bytesBase64 = Base64.encodeBase64String(bytes);
System.out.println(bytesBase64);
// decode byte[] from the encoded string
byte[] bytesFromDecode = Base64.decodeBase64(bytesBase64);
// convert the byte[] back to BufferedImage
BufferedImage newBi = toBufferedImage(bytesFromDecode);
// save it somewhere
ImageIO.write(newBi, "png", new File("c:\\test\\google-decode.png"));
}
}
Download Source Code
$ git clone https://github.com/mkyong/core-java
$ cd java-io
Thanks, man! You’re helping us so much. Thank you!
Thanks a ton mkyong .
Sir, Its working fine. But still I am getting lossy JPEG Image that means, my 17.6 MB file become 5.4MB size. How do I Get lossless JPEG and my input also is a Image not the File so How can I get the byte[] for Image(BufferedImage) ..?
how can i retrive the image from the byte array…
InputStream is = new ByteArrayInputStream(bytes);
BufferedImage bi = ImageIO.read(is);
this writes the file to desk and then converts to byte[]. This is a costly approach
the following does the conversion without writing to disk
byte[] byteArray = ((DataBufferByte) bufferedImage.getData().getDataBuffer()).getData();
Not a right solution. I am also getting java.lang.ClassCastException: java.awt.image.DataBufferInt cannot be cast to java.awt.image.DataBufferByte. Please validate the response before suggesting.
its not working. I am getting an exception – java.lang.ClassCastException: java.awt.image.DataBufferInt cannot be cast to java.awt.image.DataBufferByte. I’d appreciate if you could help.
working
Hi,
How to view the output of this program.
How to view the Converted byte array as the output??????
String bytesBase64 = Base64.encodeBase64String(bytes);
Thanks, You’re helping so much. But when I decoded the base64, the image was rotated 90 degrees. Do you have any solution??
You don’t need to flush(), its a no-op for the BAOS.
We updated the code again, thanks for your feedback.
How can I directly convert BufferedImage to File
I need to convert a blob type image to black and white.
if (rs.next()) {
blob = rs.getBlob(“value”);
}
int blobLength = (int) blob.length();
byte[] blobAsBytes = blob.getBytes(1, blobLength);
InputStream in = new ByteArrayInputStream(bytes);
final BufferedImage bufferedImage = ImageIO.read(in);
But i am getting java.lang.NullPointerException
where i am doing mistake?
Thank you sir, Was useful to me.
thanks for the post, very good
Great info, thanks for the post!