Results 1 to 20 of 26
Thread: [SOLVED] Image to byte[]
- 09-16-2008, 12:40 AM #1
Member
- Join Date
- Aug 2008
- Posts
- 58
- Rep Power
- 0
- 09-16-2008, 03:06 AM #2
Look at the ImageIO class. Its write method uses an OutputStream. There is a ByteArrayOutputStream.
- 09-16-2008, 05:41 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Use toByteArray() in ByteArrayOutputStream(), that's the easiest way I think.
- 09-16-2008, 05:05 PM #4
Member
- Join Date
- Aug 2008
- Posts
- 58
- Rep Power
- 0
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 ... :(
Java 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() }
- 09-16-2008, 06:11 PM #5
I don't see where you are drawing anything in the BufferedImage.
You've commented out the drawImage code.
- 09-16-2008, 11:18 PM #6
Member
- Join Date
- Aug 2008
- Posts
- 58
- Rep Power
- 0
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:
Java 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();Last edited by playwin2; 09-16-2008 at 11:20 PM.
- 09-17-2008, 12:08 AM #7
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?
- 09-17-2008, 06:00 PM #8
Member
- Join Date
- Aug 2008
- Posts
- 58
- Rep Power
- 0
Yes, I have tried with PNG format, same thing happens.
- 09-17-2008, 07:03 PM #9
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.
- 09-18-2008, 07:32 AM #10
Member
- Join Date
- Sep 2008
- Posts
- 9
- Rep Power
- 0
the above issue is baSED ON os ?
- 09-18-2008, 01:11 PM #11
Member
- Join Date
- Aug 2008
- Posts
- 58
- Rep Power
- 0
@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.Last edited by playwin2; 09-18-2008 at 01:25 PM.
- 09-18-2008, 02:04 PM #12
Can you give an example? I'm on XP.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.
Java 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();Last edited by Norm; 09-18-2008 at 03:46 PM.
- 09-19-2008, 09:21 AM #13
Member
- Join Date
- Aug 2008
- Posts
- 58
- Rep Power
- 0
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?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.
- 09-19-2008, 01:55 PM #14
Did you test your program with a .png file? Did you read a png and write out a png?Have you tried using a photo editor to open and save a jpg file and compare the results?
- 09-20-2008, 05:19 PM #15
Member
- Join Date
- Aug 2008
- Posts
- 58
- Rep Power
- 0
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:

code:Output Image:Java 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);

code:
Output Image:Java 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);
Last edited by playwin2; 09-20-2008 at 06:16 PM. Reason: BOM - order not object
- 09-20-2008, 06:00 PM #16
I've never seen reference to BOM? Where is the BOM kept for files on windows? How do you access it?
- 09-20-2008, 06:11 PM #17
Member
- Join Date
- Aug 2008
- Posts
- 58
- Rep Power
- 0
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
- 09-21-2008, 10:13 AM #18
Member
- Join Date
- Aug 2008
- Posts
- 58
- Rep Power
- 0
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:-


And the Other one:-


D
- 09-21-2008, 02:12 PM #19
Sorry, I'm not familiar with the internals of image files.
- 09-21-2008, 09:24 PM #20
Member
- Join Date
- Aug 2008
- Posts
- 58
- Rep Power
- 0
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.Java 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(...)
Similar Threads
-
work on byte image in j2me
By sunitikumar in forum CLDC and MIDPReplies: 0Last Post: 07-24-2008, 04:12 PM -
using Byte arrays
By mew in forum New To JavaReplies: 2Last Post: 01-30-2008, 03:54 AM -
BufferedImage to Byte
By Java Tip in forum Java TipReplies: 0Last Post: 01-22-2008, 08:17 PM -
int to byte
By ravian in forum New To JavaReplies: 1Last Post: 01-13-2008, 07:22 PM -
Converting multiple banded image into single banded image... Image enhancement
By archanajathan in forum Advanced JavaReplies: 0Last Post: 01-08-2008, 05:29 PM


LinkBack URL
About LinkBacks


Bookmarks