How to convert byte[] to BufferedImage in Java

This article shows how to convert a byte[] to a BufferedImage in Java.


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

The idea is puts the byte[] into an ByteArrayInputStream object, and we can use ImageIO.read to convert it to a BufferedImage.

1. Convert byte[] to BufferedImage.

The below example shows how to convert BufferedImage to byte[] and vice versa.

ImageUtils.java

package com.mkyong.io.image;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;

public class ImageUtils {

    public static void main(String[] args) throws IOException {

        Path source = Paths.get("c:\\test\\mkyong.png");
        Path target = Paths.get("c:\\test\\mkyong-new.png");

        BufferedImage bi = ImageIO.read(source.toFile());

        // convert BufferedImage to byte[]
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(bi, "png", baos);
        byte[] bytes = baos.toByteArray();

        // convert byte[] back to a BufferedImage
        InputStream is = new ByteArrayInputStream(bytes);
        BufferedImage newBi = ImageIO.read(is);

        // add a text on top on the image, optional, just for fun
        Graphics2D g = newBi.createGraphics();
        g.setFont(new Font("TimesRoman", Font.BOLD, 30));
        g.setColor(Color.BLACK);
        g.drawString("Hello World", 100, 100);

        // save it
        ImageIO.write(newBi, "png", target.toFile());

    }
}

Download Source Code

$ git clone https://github.com/mkyong/core-java

$ cd java-io

References

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

  1. This is not working in my case if you use webp image we get null data BufferedImage newBi

    Reply
  2. hi i have image that stored in mongodb as byte format looking for separate decoder for that.

    File image = new File(“mongodb://localhost:27017//test-db//user”);
    Tesseract tessInst = new Tesseract();
    tessInst.setDatapath(“C:\\Users\\Administrator\\Desktop\\tessdata”);
    try {
    String result= tessInst.doOCR(image);
    System.out.println(result);
    } catch (TesseractException e) {
    System.err.println(e.getMessage());
    }

    }
    }

    Reply
  3. Hi,
    Is there any way to convert multipartfile file of type webp rfrmat to png ?

    Reply
  4. Do we have any other option otherthan ImageIO.read for converting Bytes to BufferedImage
    Because when reading via ImageIO.read,image size is increasing…

    Reply
  5. ImageIO.read() is very, VERY slow (even without disk cache) – please offer a better solution. Thanks in advance

    Reply
  6. I have image in binary format file how to get back image .there is concpt of ImageIO and ByteBuffer .but How i use this

    Reply
    1. Import the following classes :
      import java.awt.Graphics;
      import java.awt.image.BufferedImage;
      import javax.imageio.ImageIO;

      Then use the following piece of code:
      Graphics g;
      Supposing you have image’s byte code in
      byte[] img;

      Now, this will get you the image.
      InputStream in = new ByteArrayInputStream(imgData);
      BufferedImage bImageFromConvert = ImageIO.read(in);
      g = bImageFromConvert.getGraphics();

      // optional statements
      g.setColor(Color.red); // to set color
      g.setFont(g.getFont().deriveFont(100f)); // to set font style
      g.drawString(“Hello”, 100, 200); // to draw any string on it
      g.dispose();

      Reply
  7. Hi,
    this solution is only valid when you read image from a file. But when reading a byte array from a database the following error shows up:

    Exception in thread “main” java.lang.IllegalArgumentException: im == null!
    at javax.imageio.ImageIO.write(Unknown Source)
    at javax.imageio.ImageIO.write(Unknown Source)

    Reply
  8. Hi, I use richfaces treenode because there are some problems on primefaces treenode. I need to load an image inside from a byte array (blob type in my database).

    Is it necessary to declare the icon url into iconLeaf param?
    I mean, is it necessary to create an image file to get it inside the treenode or can i get a buffered image directly from the bean?
    BR.

    Reply
  9. i got a null pointer exception.
    please what could be wrong?
    thanks in advance.

    Reply
  10. Hi.
    I read form file to byte array. In next step i change image size and write as byte array to screen. It’s work fine.
    Thanks for tutorial.

    Reply
  11. i need help on showing images from database to my jsp page. I am using Spring+Hibernate Framework. i am getting String when i get the blob from the pojo. can i get image directly from the pojo? thanks

    Reply
  12. Thanks a lot for this and other nice and handy tutorials.

    Reply
  13. Hello All,
    I want to convert raw file to jpg.
    I have file name ss.raw. I want to convert it into ss.jpg. Please help me.
    Thanks in Advance.

    Reply
  14. How to read images from HDFS in map-reduce??

    Reply
  15. hi,
    how can i read image in byte array from socket and write a jpeg file, please help me here is my email umer_rock_12[at]hotmail.com

    Thanks and Regards

    umer

    Reply
  16. awesome code, helped me today…
    thank you
    Aashish Dalmia

    Reply

Leave a Comment

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