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?

Recommended Answers

All 4 Replies

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

Hi everyone,

Save the image as an java object using streams.

Richard West

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...

Generate an XML..use jDom...bingo

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.