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.BufferedIma ge;
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).getIconWi dth(), new
ImageIcon(image).getIconHe ight(), BufferedImage.TYPE_INT_RGB );
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
ImageIO.createImageOutputS tream(byte Array); // 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);
}
}
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.BufferedIma
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
image = ImageIO.read(is);
// code is questionable from here on down to
BufferedImage bi = new BufferedImage(new ImageIcon(image).getIconWi
ImageIcon(image).getIconHe
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
ImageIO.createImageOutputS
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
frame.pack();
frame.setVisible(true);
}
}
JLabel label = new JLabel(new ImageIcon(bytes));
Or to create an Image you could also use:
Image image = Toolkit.getDefaultToolkit( ).createIm age(bytes) ;
Image image = Toolkit.getDefaultToolkit(
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).getIconWi dth(), new
ImageIcon(image).getIconHe ight(), BufferedImage.TYPE_INT_RGB );
System.out.println(bi);
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
System.out.println(byteArr ay);
ImageIO.createImageOutputS tream(byte Array); // image is your BufferedImage
byte[] bytes = byteArray.toByteArray();
Image img = Toolkit.getDefaultToolkit( ).createIm age(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
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).getIconWi
ImageIcon(image).getIconHe
System.out.println(bi);
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
System.out.println(byteArr
ImageIO.createImageOutputS
byte[] bytes = byteArray.toByteArray();
Image img = Toolkit.getDefaultToolkit(
} 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")
frame.getContentPane().add
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'? ;-)
But why the 'but'? ;-)
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
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( )));
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(
ASKER
CEHJ,
Trying your suggestion above. I get this error
1 error found:
File: C:\DrJavaProjects\ioshoima ge.java [line: 32]
Error: cannot resolve symbol
symbol : method write (java.awt.Image,java.lang. String,jav a.io.ByteA rrayOutput Stream)
location: class javax.imageio.ImageIO
I am using j2sdk1.4.2_08 and win2K with NT
D3
Trying your suggestion above. I get this error
1 error found:
File: C:\DrJavaProjects\ioshoima
Error: cannot resolve symbol
symbol : method write (java.awt.Image,java.lang.
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.
You're better off using my first suggestion.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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 :)
JLabel label = new JLabel(new ImageIcon("NadiaFace.jpg")
?