public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a message");
String str=sc.nextLine();
int count=0;
char ch=' ';
int p=0,a=0,r=0,g=0,b=0,k=0;
try
{
BufferedImage img=null;
img=ImageIO.read(new File("in.jpg"));
for (int i = 0; i < 5184; i++) {
for (int j = 0; j < 3456; j++) {
p=img.getRGB(i,j);// getting the RGB values at specified pixel
// get alpha
a = (p>>24) & 0xff;
// get red
r = (p>>16) & 0xff;
// get green
g = (p>>8) & 0xff;
// get blue
b = p & 0xff;
if (count<str.length()) { // if a character exists to encode
ch=str.charAt(count);// extracting a character
count++;
//encoding last 2 bits of character in last two bits of a
k=ch & (0x3);
a=a&(0xfc);
a=a|k;
//encoding next 2 bits of char in last two bits of r and so on
k= ch & (0xc);
r=r&(0xfc);
k=k>>2;
r=r|k;
k= ch &(0x30);
g=g&(0xfc);
k=k>>4;
g=g|k;
k=ch &(0xc0);
b=b&(0xfc);
k=k>>6;
b=b|k;
}
// if no char is there to encode then simply reform the previous p
p = (a<<24) | (r<<16) | (g<<8) | b;
img.setRGB(i, j, p);
}
}
File outputfile = new File("out.jpg");
ImageIO.write(img, "jpg", outputfile);
}
catch (Exception e)
{
//System.out.println(e);
}
System.out.println("END of programme");
}
I wrote the above mentioned code to encode a given text in an Image by altering the LSB's of the RGB values of each pixel, But when I read the pixels from the created image I dont get the expected values.
Is there some mistake in my encoding technique?