PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, May 6, 2022

[FIXED] How to convert binary image to binary array in java?

 May 06, 2022     arrays, image, java     No comments   

Issue

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]);
        }
    }
}

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing