Page 1 of 2 12 LastLast
Results 1 to 20 of 26
  1. #1
    playwin2 is offline Member
    Join Date
    Aug 2008
    Posts
    58
    Rep Power
    0

    Default [SOLVED] Image to byte[]

    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....

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    Look at the ImageIO class. Its write method uses an OutputStream. There is a ByteArrayOutputStream.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  4. #4
    playwin2 is offline Member
    Join Date
    Aug 2008
    Posts
    58
    Rep Power
    0

    Default

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

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    I don't see where you are drawing anything in the BufferedImage.
    You've commented out the drawImage code.

  6. #6
    playwin2 is offline Member
    Join Date
    Aug 2008
    Posts
    58
    Rep Power
    0

    Default

    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.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    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?

  8. #8
    playwin2 is offline Member
    Join Date
    Aug 2008
    Posts
    58
    Rep Power
    0

    Default

    Yes, I have tried with PNG format, same thing happens.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    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.

  10. #10
    dhnsekaran is offline Member
    Join Date
    Sep 2008
    Posts
    9
    Rep Power
    0

    Default

    the above issue is baSED ON os ?

  11. #11
    playwin2 is offline Member
    Join Date
    Aug 2008
    Posts
    58
    Rep Power
    0

    Default

    @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.

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    any decent image viewer should show those stats
    Can you give an example? I'm on XP.

    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.

  13. #13
    playwin2 is offline Member
    Join Date
    Aug 2008
    Posts
    58
    Rep Power
    0

    Default

    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. :(

    Can you give an example? I'm on XP.
    You mean how to view those in WinXp?
    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.

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    Have you tried using a photo editor to open and save a jpg file and compare the results?
    Did you test your program with a .png file? Did you read a png and write out a png?

  15. #15
    playwin2 is offline Member
    Join Date
    Aug 2008
    Posts
    58
    Rep Power
    0

    Default

    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:-

    BOM
    File Type
    ----------------------------
    FF FE
    Unicode (UTF-16 Little Endian)
    FE FF
    Unicode (UTF-16 Big Endian)
    EF BB BF
    Unicode (UTF-8)
    n/a
    Ansi/Ascii.

    Anything for images?

    Quote Originally Posted by Norm View Post
    Did you test your program with a .png file? Did you read a png and write out a png?
    Yes:
    Input Original Image:


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


    code:
    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);
    Output Image:
    Last edited by playwin2; 09-20-2008 at 06:16 PM. Reason: BOM - order not object

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    I've never seen reference to BOM? Where is the BOM kept for files on windows? How do you access it?

  17. #17
    playwin2 is offline Member
    Join Date
    Aug 2008
    Posts
    58
    Rep Power
    0

    Default

    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

  18. #18
    playwin2 is offline Member
    Join Date
    Aug 2008
    Posts
    58
    Rep Power
    0

    Default

    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

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    Sorry, I'm not familiar with the internals of image files.

  20. #20
    playwin2 is offline Member
    Join Date
    Aug 2008
    Posts
    58
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    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){...}

    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(...)
    Thanks.

Page 1 of 2 12 LastLast

Similar Threads

  1. work on byte image in j2me
    By sunitikumar in forum CLDC and MIDP
    Replies: 0
    Last Post: 07-24-2008, 04:12 PM
  2. using Byte arrays
    By mew in forum New To Java
    Replies: 2
    Last Post: 01-30-2008, 03:54 AM
  3. BufferedImage to Byte
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-22-2008, 08:17 PM
  4. int to byte
    By ravian in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 07:22 PM
  5. Replies: 0
    Last Post: 01-08-2008, 05:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •