我有一个将文本文件写入 png 文件的程序,但它不起作用 - 图像在解码时返回不正确的字符,有时图像无法正确显示。这是我的代码:
public static void readText(String text, int[] pixArr, BufferedImage im, File outFile)
throws FileNotFoundException, IOException{
char[] txt = text.toCharArray(); //Changes text file to array of characters
int[] eightBit=new int[8]; //stores binary representation of characters
for (int i=0;i<txt.length;i++){
int hey=txt[i];
for (int a=0;a<8;a++){ //converting text to binary
eightBit[a]=hey%2;
hey=hey/2;
}
eightBit=reverseArray(eightBit);
insertion(pixArr, eightBit);
}
BufferedImage norm = new BufferedImage(im.getWidth(), im.getHeight(),
BufferedImage.TYPE_INT_ARGB);
norm.getGraphics().drawImage(im, 0, 0, null);
ImageIO.write(im, "png", outFile);
}
public static void insertion(int[] pixArr, int[]eightBit){
for (int i=0;i<pixArr.length;i++){
for (int a=0;a<eightBit.length;a++){
int temp=pixArr[i];
temp=temp/2;
temp*=2;
pixArr[i++]=eightBit[a]+temp;
}
}
}