Results 1 to 6 of 6

Thread: Convert image byte [] using java

  1. #1
    Join Date
    Dec 2009
    Posts
    292

    Convert image byte [] using java

    Hello,
    I want a simple way to convert an Image or BufferedImage in bit array, if possible without using a temporary file. It must be possible via a ByteArrayInputStream, but how about the size to initialize? Any help on this will be appreciated. Thank you.

  2. #2
    Join Date
    Nov 2009
    Posts
    359

    Re: Convert image byte [] using java

    Hello,
    Can be viewed from the side of BufferedImage and Raster with this method getDataBuffer (). I think you read some articles before coding on this topic. I would recommend you to go for the the sun's documentation and api which they provide. It will help you a lot. Just try the above mentioned class and the method, there are few handy methods, you can test them.

  3. #3
    Join Date
    Dec 2009
    Posts
    292

    Re: Convert image byte [] using java

    Hello,
    I searched in this way, and tried this
    ((DataBufferByte) newImage.getRaster().getDataBuffer()).getData()
    The following exception is generated:
    java.lang.ClassCastException: java.awt.image.DataBufferInt can not be cast to java.awt.image.DataBufferByte
    And of course a DataBufferInt.getData () returns an int [] and not a byte [].
    Do you have any idea how to fix this problem. Please help

  4. #4
    Join Date
    Nov 2009
    Posts
    356

    Re: Convert image byte [] using java

    Hello,
    The BufferedImage class has the method "getRGB (int startX, int startY, int w, int h, int [] rgbArray, int offset, int scan size). They are int, byte and not, for the most generic possible. Depending on your image, it's up to you to figure out where the data in your world: 8, 16, 24 or 32 bits, or a personal config.

  5. #5
    Join Date
    Nov 2009
    Posts
    446

    Re: Convert image byte [] using java

    Hello,
    A ByteArrayOutputStream instead. For the size it did not really matter because it will be adapted as needed. But if you have the exact size or a proxy, you can avoid unnecessary resizing.
    Code:
    	Public static byte[] getBytes(BufferedImage img) throws IOException {
    		ByteArrayOutputStream byar = new ByteArrayOutputStream();
    		try {
    			ImageIO.write(img, "png", byar);
    		} finally {
    			byar.close();
    		}
    		return byar.toByteArray();
    	}

  6. #6
    Join Date
    Dec 2009
    Posts
    292

    Re: Convert image byte [] using java

    Hello,
    Meanwhile I found a solution:
    Code:
    ByteArrayOutputStream byar = new ByteArrayOutputStream();
    JPEGCodec.createJPEGEncoder(byar).encodes(newImage);
    byte[] b = byar.toByteArray();
    So what is the best between these two statements:
    Code:
    ImageIO.write(newImage, "jpeg", byar);
    JPEGCodec.createJPEGEncoder(baos).encodes(newImage);
    In any case thank you for that clarification.

Similar Threads

  1. Convert 2D image into 3D image using Silverlight 4
    By Thenral in forum Windows Software
    Replies: 5
    Last Post: 18-04-2011, 10:45 AM
  2. Can I convert MBM image to JPG...Is this possible?
    By Bon-hwa in forum Operating Systems
    Replies: 4
    Last Post: 11-11-2010, 12:34 AM
  3. What are the Byte Streams in Java?
    By super soaker in forum Software Development
    Replies: 4
    Last Post: 18-02-2010, 06:26 AM
  4. Convert Vmware Image to Virtualbox Image
    By rashmi_ay in forum Windows Software
    Replies: 5
    Last Post: 23-12-2009, 02:56 PM
  5. Convert byte[] into image using ASP.Net
    By RasMus in forum Software Development
    Replies: 3
    Last Post: 12-06-2009, 02:22 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,121,737.98599 seconds with 16 queries