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.
Re: How Convert a Byte array to a image format
How about this.
Code :
try {
final BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(myByteArray));
} catch (IOException e) {
e.printStackTrace();
}
// Json
Re: How Convert a Byte array to a image format
Json then will this BufferedImage will contain jpeg format file..........
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
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...
Re: How Convert a Byte array to a image format
Well in that case we need to expand the code somewhat.
Code :
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
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.. :confused:
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);