Tuesday, April 17, 2012

How to convert byte[] to BufferedImage in Java


package com.laxman.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());
  }
 }
}

10 comments:

  1. I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post I would like to read this
    Data Science training in Chennai
    Data science training in Bangalore
    Data science training in pune
    Data science online training
    Data Science Interview questions and answers
    Data science training in bangalore

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. I have to voice my passion for your kindness giving support to those people that should have guidance on this important matter.
    MCSE Training in chennai | mcse training class chennai


    ReplyDelete
  4. Thank you for this post. That's all I are able to say. You most absolutely have built this blog website into something special. You clearly know what you are working on, you have insured so many corners.thanks
    Data Science Training In Chennai

    Data Science Online Training In Chennai

    Data Science Training In Bangalore

    Data Science Training In Hyderabad

    Data Science Training In Coimbatore

    Data Science Training

    Data Science Online Training

    ReplyDelete
  5. Your work is very good and I appreciate you and hopping for some more informative posts
    data scientist course in hyderabad

    ReplyDelete



  6. Great to become visiting your weblog once more, it has been a very long time for me. Pleasantly this article i've been sat tight for such a long time. I will require this post to add up to my task in the school, and it has identical subject along with your review. Much appreciated, great offer. data science course in nagpur

    ReplyDelete
  7. Really an awesome blog, with informative and knowledgeable content. Thanks for sharing this blog with us. Keep sharing more stuff again.
    Data Science Online Course

    ReplyDelete
  8. Wonderful blog. I am delighted in perusing your articles. This is genuinely an incredible pursuit for me. I have bookmarked it and I am anticipating perusing new articles. Keep doing awesome!
    data analytics training in hyderabad

    ReplyDelete
  9. The technological knowledge you shared in this post is quite beneficial for coming up with fresh ideas.
    I also want to post this here , Java training institute in Noida

    ReplyDelete