0tom123546789 0tom123546789 - 4 months ago 16
Java Question

My code isn't printing out the ascii text

Thanks for the help everyone! I figured out the problem.

Answer
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageTest
{
    public static void main(String[] args) throws IOException
    {
        File input = new File("QoDeJ.png");
        BufferedImage image = ImageIO.read(input);
        StringBuilder stringBuilder = new StringBuilder();
        int redValue;
        int pixel;
        int byteValue;


        for (int x = 0; x < image.getWidth(); x+=8) {

            pixel = image.getRGB(x, 0);
            redValue = (pixel & 0x00ff0000) >> 16;

            if(redValue == 255)             
                break;

            byteValue = redValue;

            for(int p = 1; p < 8 ; p++){
                pixel = image.getRGB(x+p, 0);
                redValue = (pixel & 0x00ff0000) >> 16;
                byteValue += redValue << (7-p);
            }

            stringBuilder.append(Character.toString ((char) byteValue));

        }

        String message = stringBuilder.toString();

        System.out.println(message);
    }


}
Comments