1

I have a url of an Image. Now I want to get the byte[] of that image. How can I get that image in byte form.

Actually the image is a captcha image. I am using decaptcher.com to solve that captcha. To send that captcha image to the decaptcher.com through its API, the image should in in bytes array.

That's why I want to get the image at the url to be in bytes form.

Amit
  • 33,847
  • 91
  • 226
  • 299
  • 2
    If you just want the raw data from the URL, why do you need to create an Image? – Michael Myers Sep 25 '09 at 17:01
  • The actual bytes of the image (as it would be stored on disk, in a specific filetype)? Or the pixel data of the image? – Nate Sep 25 '09 at 17:04
  • And if you want a representation of the pixels, note that you will need to decide which representation is required. There are hundreds of ways to represent pixels. 32-bit ARGB is common, using a whole 32-bit int per pixel, for instance, but is by no means the only representation. – Sean Owen Sep 25 '09 at 23:41

3 Answers3

8

EDIT

From this SO question I've got how to read an input stream into a byte array.

Here's the revised program.

import java.io.*;
import java.net.*;

public class ReadBytes {
    public static void main( String [] args ) throws IOException {

        URL url = new URL("http://sstatic.net/so/img/logo.png");

            // Read the image ...
        InputStream inputStream      = url.openStream();
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte [] buffer               = new byte[ 1024 ];

        int n = 0;
        while (-1 != (n = inputStream.read(buffer))) {
           output.write(buffer, 0, n);
        }
        inputStream.close();

        // Here's the content of the image...
        byte [] data = output.toByteArray();

    // Write it to a file just to compare...
    OutputStream out = new FileOutputStream("data.png");
    out.write( data );
    out.close();

    // Print it to stdout 
        for( byte b : data ) {
            System.out.printf("0x%x ", b);
        }
    }
}

This may work for very small images. For larger ones, ask/search about "read input stream into byte array"

Now the code I posted works for larger images too.

Community
  • 1
  • 1
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
0

You may want to read this for reading an image in. ImageIO.Read(url) will give you a Buffered Image Which you can then ask for information. I use the getRGB method on BufferedReader to read individual pixels.

captncraig
  • 22,118
  • 17
  • 108
  • 151
0

If you want the byte string as it is stored on disk, just create a socket and open the image file. Then read the bytes as they come down the wire.

I don't have any sample code handy and it's been a while since I've done this, so excuse me if I've got the details wrong to be sure I give this to you straight, but the basic idea would be:

URL imageUrl=new URL("http://someserver.com/somedir/image.jpg");
URLConnection imageConnect=imageUrl.openConnection();
imageConnect.connect();
InputStream is=imageConnect.getInputStream();
... read from the input stream ...
Jay
  • 26,876
  • 10
  • 61
  • 112
  • This will read the content of the JPG file, so it'll give you the image data in JPG format. Maybe the poster needs the raw pixel data - but that's not clear from the question. – Jesper Sep 25 '09 at 19:19
  • Quite true, which is why I began "If you want the byte string as it is stored on disk ...". Off the top of my head the only way I know to create an Image object or something comparable would be to read the JPG (or whatever format) over the wire and then run it through the class to read an Image from disk. If this is the question, I'd have to look back at the API to get the details, but I suspect one could suck the input stream directly into the image reader. – Jay Sep 25 '09 at 20:43