无法在隐写术中提取文本

时间:2014-06-28 06:42:30

标签: java c# android steganography

我有一个代码,使用C#中的使用代码的速记来在图像上放置字符串值

http://www.codeproject.com/Tips/635715/Steganography-Simple-Implementation-in-Csharp

现在我必须在android端提取字符串。 这是我用于文本提取的android端代码

   private String extractText(Bitmap bmp) {
        // TODO Auto-generated method stub
        int colorUnitIndex = 0;
        int charValue = 0;

        // holds the text that will be extracted from the image
        String extractedText ="";

        for(int w=0;w<bmp.getHeight();w++)
        {
            for(int h=0;h<bmp.getWidth();h++)
            {
                int color=bmp.getPixel(h, w);
                green=Color.green(color);
                blue=Color.blue(color);
                red=Color.red(color);

                bred=(byte)red;
                bgreen=(byte)green;
                bblue=(byte)blue;

                // for each pixel, pass through its elements (RGB)
                for (int n = 0; n < 3; n++)
                {
                    switch (colorUnitIndex % 3)
                    {
                        case 0:
                            {
                                // get the LSB from the pixel element (will be pixel.R % 2)
                                // then add one bit to the right of the current character
                                // this can be done by (charValue = charValue * 2)
                                // replace the added bit (which value is by default 0) with
                                // the LSB of the pixel element, simply by addition
                                charValue = charValue * 2 + bred % 2;
                            } break;
                        case 1:
                            {
                                charValue = charValue * 2 + bgreen % 2;
                            } break;
                        case 2:
                            {
                                charValue = charValue * 2 + bblue % 2;
                            } break;
                    }

                    colorUnitIndex++;

                    // if 8 bits has been added, then add the current character to the result text
                    if (colorUnitIndex % 8 == 0)
                    {
                        // reverse? of course, since each time the process happens on the right (for simplicity)
                        charValue = reverseBits(charValue);

                        // can only be 0 if it is the stop character (the 8 zeros)
                        if (charValue == 0)
                        {
                            return extractedText;
                        }

                        // convert the character value from int to char
                        char c = (char)charValue;

                        // add the current character to the result text
                        extractedText += String.valueOf(c);
                    }
                }

            }

        }
        return extractedText;


    }

    private int reverseBits(int n) {
        // TODO Auto-generated method stub


         int result = 0;

            for (int i = 0; i < 8; i++)
            {
                result = result * 2 + n % 2;

                n /= 2;
            }

            return result;
    }

};

但是我没有在android端获得正确的字符串。 不知道这是什么问题。 任何人都可以帮忙吗? 提前谢谢。

2 个答案:

答案 0 :(得分:1)

最后我找到了解决方案 只需在每个位的循环上使用此代码,我就可以提取文本。

               result[b] = (byte)((result[b] << 1) | (buff[offset] & 1));

谢谢大家。

答案 1 :(得分:0)

Java没有无符号字节(如c#)。

int x = 176;
byte y = (byte)x; // if you print y, the result is: -80.

因此,在您的代码中,只要RGB值大于127,就会将其解释为不同的值(-80而不是176)。

该代码仅检查最右边的位的值,使用模块2,哪个可以在int上执行。所以你可以直接使用&#34; green&#34;,&#34; blue&#34;和&#34;红色&#34; (删除&#34; bgreen&#34;,&#34; bblue&#34;,&#34; bred&#34;):

private static String extractText(BufferedImage bmp) 
{
    int colorUnitIndex = 0;
    int charValue = 0;

    String extractedText ="";

    for(int i=0;i<bmp.getHeight();i++)
    {
      for(int j=0;j<bmp.getWidth();j++)
      {
        Color color = new Color(bmp.getRGB(j, i));

        int green=color.getGreen();
        int blue=color.getBlue();
        int red=color.getRed();

        // for each pixel, pass through its elements (RGB)
        for (int n = 0; n < 3; n++)
        {
          switch (colorUnitIndex % 3)
          {
            case 0:
            {
              // get the LSB from the pixel element (will be pixel.R % 2)
              // then add one bit to the right of the current character
              // this can be done by (charValue = charValue * 2)
              // replace the added bit (which value is by default 0) with
              // the LSB of the pixel element, simply by addition
              charValue = charValue * 2 + red % 2;
            } break;
            case 1:
            {
              charValue = charValue * 2 + green % 2;
            } break;
            case 2:
            {
              charValue = charValue * 2 + blue % 2;
            } break;
          }

          colorUnitIndex++;

          // if 8 bits has been added, then add the current character to the result text
          if (colorUnitIndex % 8 == 0)
          {
            // reverse? of course, since each time the process happens on the right (for simplicity)
            charValue = reverseBits(charValue);

            // can only be 0 if it is the stop character (the 8 zeros)
            if (charValue == 0)
            {
              return extractedText;
            }

            // convert the character value from int to char
            char c = (char)charValue;

            // add the current character to the result text
            extractedText += String.valueOf(c);
          }
        }
      }
    }
    return extractedText;
  }