Geosquare February 2016

Cant use setRGB() to write to png

public static void main(String[] args) {
    String finalHex = "";
    String input = "Hello There Sir.";
    int pixelX = -1;
    int pixelY = 0;
    try{
        BufferedImage bi = new BufferedImage(64, 64, BufferedImage.TYPE_INT_ARGB);
        File out = new File("saved.png");
        if(out.exists()==false){
            ImageIO.write(bi, "png", out);
            System.out.println("PNG WAS CREATED");
        }else 
            System.out.println("ERROR: PNG WAS ALREADY THERE");
        for (int i = 0;i < input.length(); i++){
            char result = input.charAt(i);
            int ascii = (int) result;
            String num = Integer.toHexString(ascii).toUpperCase();
            if(finalHex.length()==6){
                System.out.println(finalHex);
                pixelX += 1;
                finalHex=("#"+finalHex);        
                Color c = Color.decode(finalHex);
                int rgb = c.getRGB();
                System.out.println(rgb);
                if(pixelX==63){
                    pixelX=0;
                    pixelY+=1;
                }
                bi.setRGB(pixelX, pixelY, rgb);                 
                finalHex="";                    
            }
            finalHex+=num;              
        }           
    }catch(IOException e){
        System.out.println("ERROR: WELP... SOMETHING SCREWED UP.");
    }
}

I am trying to use this to convert text into a png image but I cant get it to write to a png file. I am not that experienced in this area so if anyone could help me out i would be very much appreciated. :)

Answers


achabahe February 2016

you should add ImageIO.write(bi, "png", out); after the end of for(int i = 0;i < input.length(); i++){...} this program will write some colored pixels is that what you want?? example: result picture

Post Status

Asked in February 2016
Viewed 3,372 times
Voted 13
Answered 1 times

Search




Leave an answer