Hello everyone I am making a code to take a binary code and then embed it within the first row of a picture. Then with that another function takes it out of the picture and converts it into decimal. The problem is that I am pretty sure I did it the way my teacher explained to convert and I did it by hand to see if the program was any different...it was not...by hand and the program got 22 166 54 54 246 ...but that doesn't see to make sense... I want to know if my math in the code and by hand is wrong or just my understanding of converting...Thanks
also aside from the problem...I am open for advice on improving my code.
/**
* This program codes a message and then decodes it
*
* @author Troyle Thomas
* @version 8/20/10
*/
import java.awt.*;
class Message
{
public void encodeMessage(Picture earth, int [] messageArray)
{
Pixel pixelTarget = new Pixel(earth,0,0);
Pixel [] pixelArray = earth.getPixels();
Color pixelColor = null;
int redValue = 0;
System.out.println(" ");
System.out.println("Binary Code");
for(int x = 0; x < messageArray.length; x++)
{
redValue = messageArray[x]; //takes message and assigns to redvalue one by one
pixelTarget = pixelArray[x];
pixelTarget.setRed(redValue);
System.out.print(messageArray[x] + " ");
}
System.out.println(" ");
pixelTarget = pixelArray[messageArray.length];
pixelTarget.setRed(255);
earth.write("SecretMessage.bmp");
earth.explore();
}
public void decodeMessage(Picture earth, int [] messageArray)
{
Pixel pointPixel = new Pixel(earth,0,0);
Pixel [] pixelList = earth.getPixels();
int [] messageBin = new int[messageArray.length];
int [] rawAscii = new int[messageArray.length];
int q = 0;
System.out.println(" ");
System.out.println("ASCII");
for(int a = 0; a < messageArray.length; a++)
{
pointPixel = pixelList[a];
messageBin[a] = pointPixel.getRed();
if(q == 8)
q = 0;
rawAscii[a] = messageBin[a] * (int)(Math.pow(2.0,(double)(q)));
q++;
}
int [] asciiSymbol = new int[rawAscii.length/8];
int add = 0;
for (int b = 0; b < rawAscii.length/8; b++)
{
asciiSymbol[b] = rawAscii[0 + add] + rawAscii[1 + add] + rawAscii[2 + add] + rawAscii[3 + add] + rawAscii[4 + add] + rawAscii[5 + add] + rawAscii[6 + add] + rawAscii[7 + add];
add = add + 8;
System.out.print(asciiSymbol[b] + " ");
}
}
}
public class HideMessageTester
{
public static void main(String[] args)
{
//int[] messageArray = {0,1,1,0,0,0,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,1,1,0,0,1,1,0,0,0,0,1,0,1,1,1,0,0,1,0,0,1,1,1,1,0,0,1};
int[] messageArray = {0,1,1,0,1,0,0,0,0,1,1,0,0,1,0,1,0,1,1,0,1,1,0,0,0,1,1,0,1,1,0,0,0,1,1,0,1,1,1,1};
Picture earth = new Picture("Earth.bmp");
Message hid = new Message();
hid.encodeMessage(earth, messageArray);
hid.decodeMessage(earth, messageArray);
}
}