我刚开始学习计算机科学,我们的老师给了我们这个小而棘手的编程作业。我需要解码一个 .bmp 图像http://postimg.org/image/vgtcka251/我们的老师已经交给了我们,经过 4 个小时的研究和尝试,我离解码还差得远。他给了我们他的编码方法:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class HideMsgInPicture {
final static long HEADSIZE=120;
public static void main(String[] args) throws IOException {
encode();
decode();
}
private static void encode() throws IOException {
FileInputStream in = null;
FileInputStream msg = null;
FileOutputStream out = null;
try {
in = new FileInputStream("car.bmp");
msg = new FileInputStream("msg.txt");
out = new FileOutputStream("carX.bmp");
int c,mb;
byte clearBit1 = (byte) 0xFE; //254; // 11111110
for (int i=1;i<=HEADSIZE;i++) out.write(in.read()); //copy header
while ((mb = msg.read()) != -1) { // for all byte in message
for (int bit=7; bit>=0; bit--) // 1 bit a time from messsage
{ c = in.read() & clearBit1; // get picturebyte,clear last bit
c = (c | ((mb >> bit) & 1));// put msg-bit in end of pic-byte
out.write(c); // add pic-byte in new file
}
}
for (int bit=7; bit>=0; bit--) // add 8 zeroes as stop-byte of msg
{ c = in.read() & clearBit1; // get picturebyte,clear last bit
out.write(c); // add pic-byte in new file
}
while ((c = in.read()) != -1) out.write(c);// copy rest of file
}
finally {
if (in != null) in.close();
if (msg != null) msg.close();
if (out != null) out.close();
}
}
}
有人能把我送到正确的方向吗?