• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1963
  • Last Modified:

ByteArray to Image.

Hi All,

With this code, I want  to reconstruct the image  from the    byte[] bytes = byteArray.toByteArray();

Your help is appreciated.

Delphi3

Given:

import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.imageio.*;
import java.awt.image.BufferedImage;

public class ioshoimage extends JFrame
{  
   public static void main(String a[])
  {
    new ioshoimage();
  }
 
  public ioshoimage()    
  {    
    Image image = null;
   
    try {

      InputStream is = new BufferedInputStream(new FileInputStream("NadiaFace.jpg"));
      image = ImageIO.read(is);

    // code is questionable from here on down to

 
      BufferedImage bi = new BufferedImage(new ImageIcon(image).getIconWidth(), new

ImageIcon(image).getIconHeight(), BufferedImage.TYPE_INT_RGB);

     
      ByteArrayOutputStream byteArray = new ByteArrayOutputStream();

     
     
      ImageIO.createImageOutputStream(byteArray); // image is your BufferedImage
      byte[] bytes = byteArray.toByteArray();
     
      // here???

     
    } catch (IOException e) {
    }
   
    //Use a label to display the image
    JFrame frame = new JFrame();
    JLabel label = new JLabel(new ImageIcon(image));
    frame.getContentPane().add(label, BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
  }
}


0
delphi3
Asked:
delphi3
  • 5
  • 4
  • 3
1 Solution
 
CEHJCommented:
Why don't you just do

JLabel label = new JLabel(new ImageIcon("NadiaFace.jpg"));

?
0
 
objectsCommented:
 JLabel label = new JLabel(new ImageIcon(bytes));
0
 
objectsCommented:
Or to create an Image you could also use:

Image image = Toolkit.getDefaultToolkit().createImage(bytes);
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
delphi3Author Commented:
CEHJ,
   
I have no problem with your solution. as you can see in my snip below

I really want to do this with the bytes....

Objects,  I tried your suggestion. but nothing but the caption bar show at the top.



       BufferedImage bi = new BufferedImage(new ImageIcon(image).getIconWidth(), new

ImageIcon(image).getIconHeight(), BufferedImage.TYPE_INT_RGB);
      System.out.println(bi);
     
      ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
      System.out.println(byteArray);
     
     
      ImageIO.createImageOutputStream(byteArray); // image is your BufferedImage
      byte[] bytes = byteArray.toByteArray();
      Image img = Toolkit.getDefaultToolkit().createImage(bytes);
     
     
    } catch (IOException e) {
    }
   
    // Use a label to display the image
     JFrame frame = new JFrame();
     //JLabel label = new JLabel(new ImageIcon(img)); // objects, must be uncommented to test but      

                                                                            this does not work
     JLabel label = new JLabel(new ImageIcon("NadiaFace.jpg"));//  CEHJ, this works but??
     frame.getContentPane().add(label, BorderLayout.CENTER);
     frame.pack();
     frame.setVisible(true);
  }
}



Again, please help me out with your bright ideas.

D3




0
 
CEHJCommented:
>>Label label = new JLabel(new ImageIcon("NadiaFace.jpg"));//  CEHJ, this works but??

But why the 'but'? ;-)
0
 
delphi3Author Commented:
CEHJ ,
this is an explanation of the  'but'? :

I am trying to put this image into a byte array first and then put it back into an image.

I know that I do not need to have it in this instance  but I need to  see/learn  how to put the byte array  back into an image.

Thanks

D3

 
 
0
 
CEHJCommented:
Try the following then:

    ImageIcon icon = new ImageIcon("NadiaFace.jpg");
    Image image = icon.getImage();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ImageIO.write(image, "JPG", out);
   
    JLabel label = new JLabel(new ImageIcon(out.toByteArray()));
   
0
 
delphi3Author Commented:
CEHJ,

Trying your suggestion above.  I get this error


1 error found:
File: C:\DrJavaProjects\ioshoimage.java  [line: 32]
Error: cannot resolve symbol
symbol  : method write (java.awt.Image,java.lang.String,java.io.ByteArrayOutputStream)
location: class javax.imageio.ImageIO

I am using j2sdk1.4.2_08 and win2K with NT

D3
0
 
objectsCommented:
  //JLabel label = new JLabel(new ImageIcon(img)); // objects, must be uncommented to test but    

You're better off using my first suggestion.
0
 
objectsCommented:
Though you aren't reading the file correctly, you don't need to worry about ImageIO to read the files bytes.
Just use something like:

    InputStream is = new BufferedInputStream(new FileInputStream("NadiaFace.jpg"));
    ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
    int n = 0;
    while (-1!=(n=is.read()))
    {
       byteArray.write(n);
    }
    byteArray.close();
    byte[] bytes = byteArray.toByteArray();
    JLabel label = new JLabel(new ImageIcon(bytes));

0
 
delphi3Author Commented:
Thanks to both of you for stopping by.
CEHJ ,
 I think that the write might be OK  with code that references a "rendered image"  but that was not happening here as the image was already there.


Objects,
I appreciate you going above and beyond, writing  the code that works.

D3
0
 
objectsCommented:
no worries :)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now