为什么我的程序创建的bmp图像无法打开?

时间:2016-02-09 18:02:49

标签: java fileinputstream bmp fileoutputstream steganography

我的隐写术程序从用户处获取一个字符串和一个bmp图像,并创建一张同一张照片的新bmp图像,但略微改变以隐藏图像中每个字节的LSB中的字符串。

然而,当我写bmp图像时,它不会打开。当我只是读取字节并将它们写回来时,它打开没问题,但是当我改变它的少数字节的LSB时它不会。我可以通过测试代码看到字节值没有被改变或改变一个(对于ex 72而不是73)但是由于某种原因导致图像不能打开。这是有问题的代码。

   public String hideString(String payload, String cover_filename) throws IOException
{

/**reads source image*/
FileInputStream fis = new FileInputStream(cover_filename);

/**reads how many bytes are in the image*/
int size = fis.available();

/**loops through all bytes in the bmp image and saves them in List list*/
for(int i = 0; i < size; i++)
{
    list.add(fis.read());
}

fis.close();



/**turns payload into an array of integers - each integer is either 0 or 1*/
int[] binValues = toBinary(payload);


/**loops though payload and hides it in each byte*/
for(int i = 0; i < binValues.length; i++)
    {
        /**swapLsb takes 1 or 0 from payload and puts it in Lsb in each image byte and returns a binary integer of various sizes(no leading zeros)*/
        String s = "" + swapLsb(binValues[i], list.get(i));


        /**converts binary integer into an integer and adds it to List listAltered*/
        listAltered.add(Integer.parseInt(s, 2));
    }

/**writes stego image*/
FileOutputStream fos = new FileOutputStream("stego.bmp");

/**writes altered bytes into the bmp image*/
for(int i = 0; i < binValues.length; i++)
    {fos.write(listAltered.get(i));count++;}

/**writes the rest of bytes into bmp image*/
for(int i = binValues.length; i < size; i++)
fos.write(list.get(i));

fos.close();//close writing object

return "Success! new image is called 'stego.bmp'";

} 

0 个答案:

没有答案