import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.imageio.ImageIO;
public class EncodeBMP {
{
Color[][] image
= new Color[bi.
getWidth()][bi.
getHeight()]; for (int i = 0; i < image.length; i++)
for (int j = 0; j < image[i].length; j++) {
image
[i
][j
] = new Color(bi.
getRGB(i, j
)); }
encodeBMP(image, "data");
for (int i = 0; i < image.length; i++)
for (int j = 0; j < image[i].length; j++) {
bi.setRGB(i, j, image[i][j].getRGB());
}
ImageIO.
write(bi,
"bmp",
new File("out.bmp")); }
{
Color[][] image
= new Color[bi.
getWidth()][bi.
getHeight()]; for (int i = 0; i < image.length; i++)
for (int j = 0; j < image[i].length; j++) {
image
[i
][j
] = new Color(bi.
getRGB(i, j
)); }
System.
out.
println(extractMessage
(image
)); System.
out.
println("--------------------"); }
}
// returns byte with last bit modified if necessary
private static void modLSB(int[] bytes, int j, char foo) {
if (foo == '1')
bytes[j] = bytes[j] | 1; // make lsb one
else
bytes[j] = bytes[j] & ~1; // make lsb zero
}
// loads a text file and inserts it into the current BMP object
public static void encodeBMP
(Color[][] image,
String fName
) { try {
Scanner sc
= new Scanner
(new File(fName
)); while (sc.hasNextLine())
message += sc.nextLine() + "\n";
System.
out.
println("--------------------"); sc.close();
// read all rgb values into a bytes array
int[] bytes = new int[image.length * image[0].length * 3];
int i = 0;
for (Color[] cRow
: image
) bytes[i++] = c.getRed();
bytes[i++] = c.getGreen();
bytes[i++] = c.getBlue();
}
// modify each value based on the text
int j = 0;
for (i = 0; i < message.length(); i++) {
char c = message.charAt(i);
bin = eightBitsLong(bin);
modLSB(bytes, j++, bin.charAt(0));
modLSB(bytes, j++, bin.charAt(1));
modLSB(bytes, j++, bin.charAt(2));
modLSB(bytes, j++, bin.charAt(3));
modLSB(bytes, j++, bin.charAt(4));
modLSB(bytes, j++, bin.charAt(5));
modLSB(bytes, j++, bin.charAt(6));
modLSB(bytes, j++, bin.charAt(7));
}
// read values back into colors
int k = 0;
for (i = 0; i < image.length; i++)
for (j = 0; j < image[i].length; j++) {
image
[i
][j
] = new Color(bytes
[k
], bytes
[k
+ 1],
bytes[k + 2]);
k += 3;
}
e.printStackTrace();
}
}
private static String eightBitsLong
(String str
) // recursion! {
if (str.length() == 8)
return str;
else if (str.length() > 8)
return eightBitsLong(str.substring(2));
else
return eightBitsLong("0" + str);
}
// extracts a message hidden in a BMP file
int iter = 0;
// read the lsbs of the rgb values into one array
char[] pixels = new char[image.length * image[0].length * 3];
for (Color[] cRow
: image
) pixels[iter++] = (char) (c.getRed() & 1); // make zero or one
pixels[iter++] = (char) (c.getGreen() & 1);
pixels[iter++] = (char) (c.getBlue() & 1);
}
// iterate through the pixels and move the lsbs into the correct place
// value
int i = 0;
while (i < pixels.length - pixels.length % 8) {
char c = (char) 0;
c += pixels[i++] << 7; // 1
c += pixels[i++] << 6; // 2
c += pixels[i++] << 5; // 3
c += pixels[i++] << 4; // 4
c += pixels[i++] << 3; // 5
c += pixels[i++] << 2; // 6
c += pixels[i++] << 1; // 7
c += pixels[i++]; // 8
message += c;
}
return message;
}
}