Through this tutorial, we will learn how to convert byte[] to BufferedImage in Java.
Source Code
package com.beginner.examples;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
public class ConvertByteToBufferedImage {
public static void main(String[] args) {
try {
BufferedImage image = ImageIO.read(new File("E:tmptest.jpg"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", baos);
baos.flush();
byte [] imageByte = baos.toByteArray();
baos.close();
InputStream in = new ByteArrayInputStream(imageByte);
BufferedImage bImageFromConvert = ImageIO.read(in);
ImageIO.write(bImageFromConvert, "jpg", new File("E:tmptest.jpg"));
}catch(IOException e) {
e.printStackTrace();
}
}
}
References
Imported packages in Java documentation: