Main Tutorials

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

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
19 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Mircea
10 years ago

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

Anand
10 years ago

Thanks a ton mkyong .

Saravanan
10 years ago

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

sankar ece
10 years ago

how can i retrive the image from the byte array…

VJ
10 years ago

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();

Maulik
3 years ago
Reply to  VJ

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.

Pramod Pandey
7 years ago
Reply to  VJ

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.

?????? ?????? ????????????
5 years ago
Reply to  Pramod Pandey

working

jjv
12 years ago

Hi,

How to view the output of this program.

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

Bayu
2 years ago

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

Christian Ullenboom
3 years ago

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

Merwais
4 years ago

How can I directly convert BufferedImage to File

Dhinesh waran
8 years ago

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?

Sreelal
11 years ago

Thank you sir, Was useful to me.

friend
11 years ago

thanks for the post, very good

Leonarda Chaloux
13 years ago

Great info, thanks for the post!