2014-01-20 61 views
0

我试图从网站上读这段文字,但我不明白为什么“第32像素存储所需的位来重建创建原始字符串所需的字节值。”将消息存储到图像中

这是试图把消息到alpha(透明度)(ARGB)

在这下面的代码,为什么需要嵌入整数和字节

int imageWidth = img.getWidth(), imageHeight = img.getHeight(), 
imageSize = imageWidth * imageHeight; 
if(messageLength * 8 + 32 > imageSize) { 
    JOptionPane.showMessageDialog(this, "Message is too long for the chosen image", 
     "Message too long!", JOptionPane.ERROR_MESSAGE); 
    return; 
    } 
    embedInteger(img, messageLength, 0, 0); 

    byte b[] = mess.getBytes(); 
    for(int i=0; i<b.length; i++) 
     embedByte(img, b[i], i*8+32, 0); 
    } 

private void embedInteger(BufferedImage img, int n, int start, int storageBit) { 
    int maxX = img.getWidth(), maxY = img.getHeight(), 
     startX = start/maxY, startY = start - startX*maxY, count=0; 
    for(int i=startX; i<maxX && count<32; i++) { 
     for(int j=startY; j<maxY && count<32; j++) { 
     int rgb = img.getRGB(i, j), bit = getBitValue(n, count); 
     rgb = setBitValue(rgb, storageBit, bit); 
     img.setRGB(i, j, rgb); 
     count++; 
     } 
     } 
    } 

private void embedByte(BufferedImage img, byte b, int start, int storageBit) { 
    int maxX = img.getWidth(), maxY = img.getHeight(), 
     startX = start/maxY, startY = start - startX*maxY, count=0; 
    for(int i=startX; i<maxX && count<8; i++) { 
     for(int j=startY; j<maxY && count<8; j++) { 
     int rgb = img.getRGB(i, j), bit = getBitValue(b, count); 
     rgb = setBitValue(rgb, storageBit, bit); 
     img.setRGB(i, j, rgb); 
     count++; 
     } 
     } 
    } 

回答

1

你需要存储的消息长度所以你知道要读取多少个像素来提取消息。由于消息的长度无法预测,因此分配了32位(前32个像素)。

函数embedInteger和embedByte几乎相似。

  • embedInteger涉及将消息的长度嵌入前32个像素中。
  • embedByte嵌入你的消息字符,一个接一个。每次调用时,都会以字节形式输入消息中的下一个字符,b[i]。在那里,它每像素嵌入一位,每字节总共8位。
+0

谢谢你的回复,embedInteger是处理消息长度在前32个像素中的嵌入??但如何embedByte? –

+0

我已更新答案以获得更好的说明。 – Reti43

+0

对不起,这可能是一个愚蠢的问题,为什么需要embedInteger,因为我们可以通过使用embedByte逐个嵌入消息。 它只能嵌入每像素一位? –