不能使用setRGB()写入png

时间:2016-02-08 16:35:12

标签: java bufferedimage

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.");
    }
}

我正在尝试使用它将文本转换为png图像,但我无法将其写入png文件。我不是在这个领域有经验,所以如果有人能帮助我,我将非常感激。 :)

1 个答案:

答案 0 :(得分:1)

你应该在ImageIO.write(bi, "png", out);结束后添加for(int i = 0;i < input.length(); i++){...}这个程序会写一些彩色像素是你想要的吗? 例: result picture