How to convert byte[] to BufferedImage in Java
The conversion from byte[] to BufferedImage is involved the use of InputStream and ImageIO.read as following :
InputStream in = new ByteArrayInputStream(imageInByte); BufferedImage bImageFromConvert = ImageIO.read(in);
The following example will read an image file named “darksouls.jpg“, convert it into byte array, and then reuse the converted byte array, and convert it back to a new BufferedImage, and save it back into a new name “new-darksouls.jpg“.
package com.mkyong.image; 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 ImageTest { public static void main(String[] args) { try { byte[] imageInByte; BufferedImage originalImage = ImageIO.read(new File( "c:/darksouls.jpg")); // convert BufferedImage to byte array ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(originalImage, "jpg", baos); baos.flush(); 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:/new-darksouls.jpg")); } catch (IOException e) { System.out.println(e.getMessage()); } } }
Tags : bufferedimage imageio java

Thank You Mr.Yong
God Bless u.
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.
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
Hola Amigos .. aca tengo una duda .. veo la imagen en mi aplicacion y todo bien .. el problema es como hago para guardar la imagen capturda de una webcam .. trate de acer esto . pero solo llege hasta aqui !! …
try{
Image img=null;
FrameGrabbingControl fgc = (FrameGrabbingControl)
player.getControl(“javax.media.control.FrameGrabbingControl”);
Buffer buf = fgc.grabFrame();
// creamos la imagen awt
BufferToImage btoi = new BufferToImage((VideoFormat)buf.getFormat());
img = btoi.createImage(buf);
if (img != null)
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write((RenderedImage) img, “JPEG”, out);
InputStream in = new ByteArrayInputStream(out.toByteArray());
// ——————————————————————————————————————————-
// AQUI COMO AGO PARA CONVERTIR LA IMAGEN A BYTE :.. PARA GUARDARLO EN BD SQL
// ——————————————————————————————————————————-
}catch(Exception ex){}ç
Thanks a lot for this and other nice and handy tutorials.
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.
How to read images from HDFS in map-reduce??
i got this error..
Exception in thread “main” java.lang.IllegalArgumentException: im == null!
at javax.imageio.ImageIO.write(ImageIO.java:1457)
at javax.imageio.ImageIO.write(ImageIO.java:1521)
at javaapplication3.JavaApplication3.main(JavaApplication3.java:48)
Java Result: 1
please help me
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
Hi Mkyong,
I having this error :
Exception in thread “main” java.lang.IllegalArgumentException: im == null!
Thank you!
Excelente ejemplo! Gracias.
Help me a lot, Thank you.
Great code.. much help.. thx
thanks a lot
it was useful for me.
god bless you
Tu java na..mei k aaj ko hu…
awesome code, helped me today…
thank you
Aashish Dalmia