I have a program for writing a text file to a png file, but it doesn't work - the image, when I decode it, returns the incorrect characters and sometimes the image does not show up correctly. Here is my code:
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;
}
}
}