Issue
I have a binary image like this:
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]);
}
}
}
Solution
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)];
}
Answered By - Maher Mahmoud Answer Checked By - Senaida (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.