Java Program to convert byte array to image


In this program, You will learn how to convert byte array to image in Java.


byte[] bdata = bos.toByteArray();

ByteArrayInputStream bis = new ByteArrayInputStream(bdata);

BufferedImage bImage2 = ImageIO.read(bis);

ImageIO.write(bImage2, "jpg", new File("smith.jpg"));


Example: How to convert byte array to image in Java

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

class StringToChar {

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

        BufferedImage bImage = ImageIO.read(new File("abc.jpg"));
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ImageIO.write(bImage, "jpg", bos);
        byte[] bdata = bos.toByteArray();

        ByteArrayInputStream bis = new ByteArrayInputStream(bdata);
        BufferedImage bImage2 = ImageIO.read(bis);
        ImageIO.write(bImage2, "jpg", new File("smith.jpg"));
        System.out.println("image created success");

    }
}

Output:

image created success

Note: Now you can check your project or file location.