How one supposed to convert java.awt.Image to bytes...
For ex: one can do,
ImageIcon theImage = new ImageIcon(imageData);
// where imageData is a byte[] array....
How to do the opposite...
imageData = ?????
Thanks for any clue....
Printable View
How one supposed to convert java.awt.Image to bytes...
For ex: one can do,
ImageIcon theImage = new ImageIcon(imageData);
// where imageData is a byte[] array....
How to do the opposite...
imageData = ?????
Thanks for any clue....
Look at the ImageIO class. Its write method uses an OutputStream. There is a ByteArrayOutputStream.
Use toByteArray() in ByteArrayOutputStream(), that's the easiest way I think.
Hello thanks for helping me out...
I tried following ... but this seems not working, I only see a "black image(!!?)" as a result, obviously I messed up ... :(
Code:private byte [] getImgBytes(Image image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ImageIO.write(getBufferedImage(image), "JPEG", baos);
} catch (IOException ex) {
//handle it here.... not implemented yet...
}
return baos.toByteArray();
}
private BufferedImage getBufferedImage(Image image) {
int width = image.getWidth(null);
int height = image.getHeight(null);
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//Graphics2D g2d = bi.createGraphics();
//g2d.drawImage(image, 0, 0, null);
return bi;
}
private void saveImage(String fname)
{
java.io.FileOutputStream fos = new java.io.FileOutputStream(fname);
//here lblImg is the JLabel which hold the picture...
byte buffer[] = getImgBytes(((ImageIcon)lblImg.getIcon()).getImage());
fos.write(buffer);
fos.flush();
fos.close()
}
I don't see where you are drawing anything in the BufferedImage.
You've commented out the drawImage code.
Hello thanks for pointing out the error,
but this gives wrong image data.... :confused:
for ex original image was:
original:
------------
284 kb;
1024, 768(W, h)
150 dpi, 24 bit
creation soft: ACD Systems Digital Imaging
color representation: Uncalibrated
I get:
--------
136 kb;
1024, 768(w, h)
96 dpi, 24 bit
creation soft: <n/a>
color representation: <n/a>
and the code is now:
Code:fname += ".jpg";
java.io.FileOutputStream fos = new java.io.FileOutputStream(fname);
//fos.write(thePicBuffer);
//here lblImg is a JLabel which holds the picture...
Image img = ((ImageIcon)lblImg.getIcon()).getImage();
BufferedImage bi = new BufferedImage(
img.getWidth(null),
img.getHeight(null),
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
g2.drawImage(img, 0, 0, null);
ImageIO.write(bi,"jpg",fos);
fos.close();
I guess your are getting an image but the output is different from the input?
It might have something to do with jpeg compression. Have you tried it with another image type, say PNG?
Yes, I have tried with PNG format, same thing happens.
Does the image look ok?
What program do you use to get the stats you posted? On what OS?
Could you post a small program that shows the problem so others could try it?
No GUI, just the copy from an Image file to another Image file.
the above issue is baSED ON os ?
@Norm
I'm using Windows XP, any decent image viewer should show those stats, even file properties in windows display those.
>>NO GUI
I think GUI is necessary here, the question is AWT Image to byte[],
If one has access to original image bytes, then just copying them would yield exact copy. (I already know how to do that. :) )
I'm attaching a demo, it also includes 2 sample pictures.
DL Link: http://rapidshare.com/files/146280694/TestPic.zip
(I uploaded at Rapdshare because the file is too big for here.)
@dhnsekaran
I'm not sure is this OS specific, but I don't think so.
Can you give an example? I'm on XP.Quote:
any decent image viewer should show those stats
Have you tried using a photo editor to open and save a jpg file and compare the results?
Try writing the jpg file with this.
Code:OutputStream os = new FileOutputStream("XX"+targetPath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
JPEGEncodeParam encParm = encoder.getDefaultJPEGEncodeParam(bi);
float quality = 1.0F; // high qual
encParm.setQuality(quality, true);
encoder.setJPEGEncodeParam(encParm);
encoder.encode(bi);
os.close();
Hello,
I beginning to think that it's just not possible to get the original image data from the AWT image object, either one has to store the original image bytes when the image was loaded and use it later or create a new image(the kind/type of the image depends on the enc/dec of the current machine) from the AWT image object.
Well that's not very good news, but it can't be helped. :(
You mean how to view those in WinXp?Quote:
Can you give an example? I'm on XP.
The easier way is right-click on the pic file in windows explorer then select properties and click on summary tab.
Or your image-viewer-application might show those (provided it support that functionality) ; I'm using picasa for past few weeks.
Did you test your program with a .png file? Did you read a png and write out a png?Quote:
Have you tried using a photo editor to open and save a jpg file and compare the results?
Hello,
I have a new question regarding this...
Is it possible to tell what kind of image (jpeg,png etc.) it is by looking at the bytes? Is there any BOM or similar things present for image file?
For ex: in case of text files we can examine BOM (Byte Order Mark) to differentiate them:-
BOMFile Type----------------------------
FF FEUnicode (UTF-16 Little Endian)FE FFUnicode (UTF-16 Big Endian)EF BB BFUnicode (UTF-8)n/aAnsi/Ascii.
Anything for images?
Yes:
Input Original Image:
http://img229.imageshack.us/img229/8920/bulbborgyz2.png
code:Output Image:Code:BufferedImage bi = new BufferedImage(
img.getWidth(null),
img.getHeight(null),
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
g2.drawImage(img, 0, 0, null);
ImageIO.write(bi,"png",fos);
http://img153.imageshack.us/img153/6...yimage2ez2.png
code:
Output Image:Code:BufferedImage bi = new BufferedImage(
img.getWidth(null),
img.getHeight(null),
BufferedImage.TYPE_BYTE_INDEXED);
Graphics2D g2 = bi.createGraphics();
g2.drawImage(img, 0, 0, null);
ImageIO.write(bi,"png",fos);
http://img153.imageshack.us/img153/8...yimage1ms8.png
I've never seen reference to BOM? Where is the BOM kept for files on windows? How do you access it?
hello,
BOM - Byte Order (not object sorry) Mark, has nothing to do with Windows.
Maybe this will help:
FAQ - UTF-8, UTF-16, UTF-32 & BOM
Hello,
One of my friend send me the documentation of "JPEG File Interchange Format" ver 1.02 by "Eric Hamilton". :D
Obviously not all JPG images are JFIF but in our case, I think following is enough,
what do you think?
Norm, here is the dump of the two pic you found on the bundle I uploaded at rapidshare:
Bridge.jpg:-
http://img81.imageshack.us/img81/9176/b1so4.jpg
http://img81.imageshack.us/img81/b1so4.jpg/1/w861.png
And the Other one:-
http://img81.imageshack.us/img81/6611/m1nj4.jpg
http://img81.imageshack.us/img81/m1nj4.jpg/1/w863.png
:D:D
Sorry, I'm not familiar with the internals of image files.
Ohh! err.... okay Norm, no problem. :)
Could you just tell me how to call the read() function only once in the following code? and also why I can't use: if(data == jpgSOI){...}
Thanks.Code:final int jpgSOI[] = {0xFF, 0xD8};
java.io.FileInputStream fis = new java.io.FileInputStream(name);
int data[] = new int[4];
data[0] = fis.read();
data[1] = fis.read();
data[2] = fis.read();
data[3] = fis.read();
fis.close();
System.out.println(data[0] + " " + data[1] + " " +
data[2] + " " + data[3]);
if((data[0] == 0xFF)||(data[1] == 0xD8))
{
System.out.println("JPEG SOI marker matched.");
//nextStuff...
}
else if(...)