I am trying to change the lsb value of image pixels such that it matches with the string "abc" but adding 1 or 0 to a pixel with odd value is returning 0. here is the code:
public static void main(String[] args) {
BufferedImage img = null;
try {
img = ImageIO.read(new File("a.jpg"));
} catch (IOException ex) {
}
int pixel[] = img.getRGB(0, 0, img.getWidth(), img.getHeight(), null, 0, img.getWidth());
String s = "abc";
byte[] b = s.getBytes();
String f = "";
for (int i = 0; i < b.length; i++) {
f += Integer.toBinaryString(b[i]);
}
f.trim();
int[] newpixel = new int[pixel.length];
for (int i = 0; i < pixel.length; i++) {
if (i < f.length()) {
if (pixel[i] % 2 == 0) {
if (f.charAt(i) == '0') {
newpixel[i] = pixel[i];
}
if (f.charAt(i) == '1') {
newpixel[i] = pixel[i] + 1;
}
}
if (pixel[i] % 2 == 1) {
if (f.charAt(i) == '0') {
newpixel[i] = pixel[i] - 1;
}
if (f.charAt(i) == '1') {
newpixel[i] = pixel[i];
}
}
} else {
newpixel[i] = pixel[i];
}
}
o:
for (int i = 0; i < img.getWidth() * img.getHeight(); i++) {
if (i < f.length()) {
System.out.print(" " + f.charAt(i) + ":(" + pixel[i] + "," + newpixel[i] + ")");
} else {
break o;
}
}
}
and the output is:
1:(-11235948,-11235947) 1:(-11893363,0) 0:(-11893617,0) 0:(-10577497,0) 0:(-11695976,-11695976) 0:(-12090996,-12090996) 1:(-11170168,-11170167) 1:(-10775924,-10775923) 1:(-9724765,0) 0:(-9658965,0) 0:(-9856341,0) 0:(-11236466,-11236466) 1:(-11564174,-11564173) 0:(-11431819,0) 1:(-10380136,-10380135) 1:(-10973290,-10973289) 0:(-12093056,-12093056) 0:(-10842985,0) 0:(-10118999,0) 1:(-11368034,-11368033) 1:(-11630686,-11630685)