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

References

19 comments on “How to convert BufferedImage to byte[] in Java

  1. Thanks, You’re helping so much. But when I decoded the base64, the image was rotated 90 degrees. Do you have any solution??

    Reply
  2. You don’t need to flush(), its a no-op for the BAOS.

    Reply
  3. 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?

    Reply
  4. 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) ..?

    Reply
    1. InputStream is = new ByteArrayInputStream(bytes);
      BufferedImage bi = ImageIO.read(is);

      Reply
  5. 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();

    Reply
    1. 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.

      Reply
    2. 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.

      Reply
  6. Thanks, man! You’re helping us so much. Thank you!

    Reply
  7. Hi,

    How to view the output of this program.

    How to view the Converted byte array as the output??????

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *