How to grab byte[] of an Image in java?

后端 未结 3 1471
面向向阳花
面向向阳花 2021-01-23 22:40

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

3条回答
  •  一整个雨季
    2021-01-23 23:28

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

提交回复
热议问题