Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: How Convert a Byte array to a image format

  1. #1
    Junior Member
    Join Date
    Aug 2009
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post How Convert a Byte array to a image format

    Hi,
    How to convert a Byte array to and image format....I shall be more clear what i want...See i recieve a Byte array fom server which was converted from an image/text format into a Byte array....i recieve this Byte array I have to convert this Byte array into their respective image and text format....please tell me how to convert a Byte array to a image ......... Kindly explain clearly as i am new to Java......

    Thanking You.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: How Convert a Byte array to a image format

    How about this.

            try {
                final BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(myByteArray));
            } catch (IOException e) {
                e.printStackTrace();
            }

    // Json

  3. The Following User Says Thank You to Json For This Useful Post:

    perlWhite (August 22nd, 2009)

  4. #3
    Junior Member
    Join Date
    Aug 2009
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How Convert a Byte array to a image format

    Json then will this BufferedImage will contain jpeg format file..........

  5. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: How Convert a Byte array to a image format

    It will just contain the image data, what would you like to do with the image after this?

    // Json

  6. #5
    Junior Member
    Join Date
    Aug 2009
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How Convert a Byte array to a image format

    after converting what i need is a file with .jpeg extendsion and i store it in the local disk and will open when required................is the output from
    final BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(myByteArray));
    a jpeg file so that i can open it after storing it...................
    Thank u...

  7. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: How Convert a Byte array to a image format

    Well in that case we need to expand the code somewhat.

     
            try {
                final BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(myByteArray));
                ImageIO.write(bufferedImage, "jpg", new File("path/to/image.jpg"));
            } catch (IOException e) {
                e.printStackTrace();
            }

    The line ImageIO.write will write your bufferedImage data down to your disk as file of type jpeg and it will store it at path/to/image.jpg

    You can change that line slightly if you want to store it as png or something else. The bufferedImage data is just the image data, the ImageIO utility class will store that data to the format you specify when you call the write method.

    // Json

  8. #7
    Junior Member
    Join Date
    Jan 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Re: How Convert a Byte array to a image format

    Thank you Json this really helped me.. But I am kind of stuck in a problem related to this topic..

    Is there any way to know the image format / type when getting an image out of the byte array?
    Instead of storing it as 'jpg', as u did in your code, if we don't know the image type / format when it's pushed into the byte array how can we identify it later when getting it back? Is there any way?

    Because when my input was a png / gif file initially, i've got a blank image / invalid image when i store it to 'jpg' format..
    Last edited by ammu2288; January 6th, 2011 at 11:27 PM.

  9. #8
    Junior Member
    Join Date
    Feb 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How Convert a Byte array to a image format

    Hi I am having the same problem as ammu2288 except mine is a little different i have turned an image into a byte array. however i am trying to sho the image onto a jframe and when the image comes up it justs shows me the default colorof my jframe any help would be appreciated.
    Thank You
    here is my code:
    BufferedImage bImageFromConvert = null;

    try
    {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(screenImage, formatName, baos);
    byte[] bytesOut = baos.toByteArray();
    ByteArrayInputStream in = new ByteArrayInputStream(bytesOut);
    bImageFromConvert = ImageIO.read(in);
    }

    catch(IllegalArgumentException e)
    {
    }

    catch(IOException e)
    {
    }

    g.drawImage(bImageFromConvert, 0, 0, this);

Similar Threads

  1. Convert DOC,XLS to PDF with Java
    By comm in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: July 2nd, 2013, 04:10 AM
  2. Java program to format a double value to 2 decimal places
    By JavaPF in forum Java Programming Tutorials
    Replies: 3
    Last Post: December 4th, 2010, 04:08 PM
  3. Conversion of string into integer in Java
    By JavaPF in forum Java Programming Tutorials
    Replies: 17
    Last Post: January 23rd, 2010, 09:33 AM
  4. convert GSM to PCM (wav)
    By cilang in forum Java Theory & Questions
    Replies: 4
    Last Post: August 7th, 2009, 03:46 AM
  5. Replies: 2
    Last Post: June 26th, 2009, 11:08 AM