954,168 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

turning an image to binary code

Hey everyvody... I have this crazy idea for a program... I want to be able to load an image (most likely a .jpg) into a java program and then I want the program to print out the binary code that makes up the image and save it to a .txt file. I know how to do the i/o stuff... but I can't think of how to extract the binary code from the image.... Anybody know how to do this?

stupidenator
Junior Poster
192 posts since Mar 2005
Reputation Points: 18
Solved Threads: 4
 

I think you would have to create a PixelGrabber which returns an array of ints made from the image. Once you have that, I think you could use this method to convert the ints to binary:

Integer.toBinaryString(int);

Here is an example of a Pixel Grabber that creates a screen shot and throws it into an array of ints.

private int[] capture() throws InterruptedException {
        dim = Toolkit.getDefaultToolkit().getScreenSize();
        Image tmp = robot.createScreenCapture(new Rectangle(dim.width,dim.height)).getScaledInstance(800,600,BufferedImage.SCALE_SMOOTH);
        int pix[] = new int[IMG_WIDTH*IMG_HEIGHT];
        PixelGrabber pg = new PixelGrabber(tmp,0,0,IMG_WIDTH,IMG_HEIGHT,pix,0,IMG_WIDTH);
        pg.grabPixels();
        return pix;
    }
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

Hi everyone,

Save the image as an java object using streams.

Richard West

freesoft_2000
Practically a Master Poster
623 posts since Jun 2004
Reputation Points: 25
Solved Threads: 10
 

Alright, the pixelgrabber worked perfectly! Thank you... But now is there a way i can compile the binary back to an image after i modify it a little bit? I'm trying to make something of a stegonagraphy program but I can't think of how to save it back to an image again...

stupidenator
Junior Poster
192 posts since Mar 2005
Reputation Points: 18
Solved Threads: 4
 

Generate an XML..use jDom...bingo

Urchin1111
Newbie Poster
1 post since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You