0

I have a binary image like this:

enter image description here

I want to represent or convert this image or (any binary image) in binary array of 0's and 1's then print its values (of course it should be 0's and 1's).

My code prints non-binary values:

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class PP {

    public static void main(String argv[]) throws IOException
    {
        File file = new File("binary.jpg");
        BufferedImage originalImage = ImageIO.read(file);
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        ImageIO.write(originalImage, "jpg", baos);
        byte[] imageInByte = baos.toByteArray();

        for(int i = 0; i < imageInByte.length; i++)
        {
            System.out.println(imageInByte[i]);
        }
    }
}
hongsy
  • 1,498
  • 1
  • 27
  • 39
Maher Mahmoud
  • 115
  • 3
  • 11
  • 1
    You may want to look at : http://stackoverflow.com/questions/4989603/convert-an-image-to-binary-data-0s-and-1s-in-java – SomeDude Dec 25 '15 at 00:07
  • 1
    You're printing the `byte` value (which is an integer value from -128 to 127 in Java), you'll need to [convert the `byte` value into it's binary representation](http://stackoverflow.com/questions/12310017/how-to-convert-a-byte-to-its-binary-string-representation) – txtechhelp Dec 25 '15 at 00:08
  • well, check this please `System.out.println(Integer.toBinaryString(imageInByte[i]))` ... i tried this line of code to print values but it prints values like this `1111(line)0001(line)` .. this is not correct it should represent only one bit in a line ... is there something wrong i did? – Maher Mahmoud Dec 25 '15 at 00:19
  • Please guys correct me if i was wrong, it should print 1 bit a line in case of using 2D array ... right ?? – Maher Mahmoud Dec 25 '15 at 00:30
  • do you know what jpeg is? – njzk2 Dec 25 '15 at 04:39

2 Answers2

0

Maybe i didn't found the optimal answer for my question but these links help me so i accept this as an answer for my question.

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

How to convert a byte to its binary string representation

Edit

I worked more and finally found a way to represent each bit of the binary image in a single line:

here is the code:

StringBuilder check = new StringBuilder();
for(int i = 0; i < imageInByte.length; i++)
{
    check.append(Integer.toBinaryString(imageInByte[i]));
}

String array[] = check.toString().split("");

for(int i = 0; i < array.length; i++){
    System.out.println(array[i)];
}
Community
  • 1
  • 1
Maher Mahmoud
  • 115
  • 3
  • 11
  • but `imageInByte` is a jpg. it is a compressed format. It does not represent the image, only the file that once decoded represent the image – njzk2 Dec 25 '15 at 04:43
  • Compression is used to reduce the size of an image in the hard disk ... you get the same image with compression but with lower quality. – Maher Mahmoud Dec 25 '15 at 04:51
  • from your question, it seems that you are trying to obtain a list of 0,1 values corresponding to the black/white color of each pixel. is it not the case? – njzk2 Dec 25 '15 at 06:00
0

This code converts an image to a String that contains 0 for white and 1 for black pixels. The result is not an 2 dimensional array but just one long String with 0/1 values.

    public static String getBinaryImage(BufferedImage image) {
    StringBuffer result = new StringBuffer();
    int width = image.getWidth();
    int height = image.getHeight();
    for (int i = 0; i < height; i++)
        for (int j = 0; j < width; j++)
            result.append(image.getRGB(j, i) == -1 ? 0 : 1);
    return result.toString();
}
dulvui
  • 122
  • 9