How to convert byte[] to BufferedImage in Java
Written on September 29, 2009 at 9:59 am by
mkyong
The conversion from byte[] to BufferedImage is involved InputStream and ImageIO.read as follow :
InputStream in = new ByteArrayInputStream(imageInByte); BufferedImage bImageFromConvert = ImageIO.read(in);
Example
This class will read an image file, convert it to byte array , and then convert it back to BufferedImage, finally save it to a new location.
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; /* * @author mkyong * */ public class ImageTest { public static void main(String [] args){ try{ BufferedImage originalImage = ImageIO.read(new File("c:\\image\\mypic.jpg")); //convert BufferedImage to byte array ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write( originalImage, "jpg", baos ); baos.flush(); byte[] imageInByte = baos.toByteArray(); baos.close(); //convert byte array back to BufferedImage InputStream in = new ByteArrayInputStream(imageInByte); BufferedImage bImageFromConvert = ImageIO.read(in); ImageIO.write(bImageFromConvert, "jpg", new File("c:\\image\\mypic_new.jpg")); }catch(IOException e){ System.out.println(e.getMessage()); } } }
Oracle Magazine (Free)
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world\'s largest enterprise software company.
Publisher : Oracle Corporation



awesome code, helped me today…
thank you
Aashish Dalmia