Link to home
Start Free TrialLog in
Avatar of delphi3
delphi3

asked on

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);
  }
}


Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Why don't you just do

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

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

Image image = Toolkit.getDefaultToolkit().createImage(bytes);
Avatar of delphi3
delphi3

ASKER

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




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

But why the 'but'? ;-)
Avatar of delphi3

ASKER

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

 
 
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()));
   
Avatar of delphi3

ASKER

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
  //JLabel label = new JLabel(new ImageIcon(img)); // objects, must be uncommented to test but    

You're better off using my first suggestion.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of delphi3

ASKER

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
no worries :)