Convert image to binary data (0s and 1s) in java

I want to read an image from a url and convert it to binary data. Please help me..

        byte[] data = null;
        ByteArrayOutputStream bas = null;
        try {
            URL u = new URL(
                    "http://www.eso.org/public/archives/images/screen/eso0844a.jpg");
            HttpURLConnection con1 = (HttpURLConnection) u.openConnection();
            con1.setAllowUserInteraction(true);
            con1.setRequestMethod("GET");
            con1.connect();
            InputStream is = con1.getInputStream();
            BufferedImage imgToServe = null;
            if (is != null) {
                imgToServe = ImageIO.read(is);
            }
            bas = new ByteArrayOutputStream();
            ImageIO.write(imgToServe, "jpg", bas);

            File f = new File("C:\\img.jpg");
            ImageIO.write(imgToServe, "jpg", f);

            data = bas.toByteArray();
            String str = "";
            for (int i = 0; i < data.length; i++) {
                str = str + toBinary(data[i]);
            }
            System.out.println(str);

        } catch (HTTPException he) {

        } catch (IOException ioe) {
        }
    }

    private static String toBinary(byte b) {
        StringBuilder sb = new StringBuilder("00000000");

        for (int bit = 0; bit < 8; bit++) {
            if (((b >> bit) & 1) > 0) {
                sb.setCharAt(7 - bit, '1');
            }
        }
        return (sb.toString());
    }
+3
source share
3 answers

If you are reading an image from a URL, it will already be in binary format. Just upload the data and ignore the fact that this is an image. In the end, the code that is involved in the download is all the same. Assuming you want to write it to a file or something similar, just open URLConnectionand open FileOutputStreamand re-read from the input stream from the Internet, writing the data that you read to the output stream.

If this is not what you need, clarify the question.

EDIT: ( ), :

  • (. , , ByteArrayOutputStream)
  • ( ) 0s 1s

, , . ?

+7

ImageIO. read URL Image. write File a ByteArrayOutputStream, .

public static void main(String[] args) throws Exception {

    // read "any" type of image (in this case a png file)
    BufferedImage image = ImageIO.read(new URL("http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png"));

    // write it to byte array in-memory (jpg format)
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    ImageIO.write(image, "jpg", b);

    // do whatever with the array...
    byte[] jpgByteArray = b.toByteArray();

    // convert it to a String with 0s and 1s        
    StringBuilder sb = new StringBuilder();
    for (byte by : jpgByteArray)
        sb.append(Integer.toBinaryString(by & 0xFF));

    System.out.println(sb.toString());
}
+4

/url BufferedImage

public static Raster loadImageRaster(String file_path) throws IOException 
{
    File input = new File(file_path);
    BufferedImage buf_image = ImageIO.read(input);

    buf_image = binarizeImage(buf_image);

    return buf_image.getData(); //return raster
}

BufferedImage BufferedImage

public static BufferedImage binarizeImage(BufferedImage img_param) 
{
    BufferedImage image = new BufferedImage(
        img_param.getWidth(), 
        img_param.getHeight(),                                    
        BufferedImage.TYPE_BYTE_BINARY);

    Graphics g = image.getGraphics();
    g.drawImage(img_param, 0, 0, null);
    g.dispose();

    return image;
}

Convert BufferedImageto Rasterso you can manipulate it pixel by pixel

imageRaster.getSample(x, y, 0)

Raster.getSample(x,y, channel)will return 0sor 1s.

channel = 0 for TYPE_BYTE_BINARYimages

+3
source

Source: https://habr.com/ru/post/1791928/


All Articles