【问题标题】:Get string from lsb of pixels of a PNG image从 PNG 图像的 lsb 像素中获取字符串
【发布时间】:2015-05-16 03:17:34
【问题描述】:

我编写了一个代码来修改 PNG 图像的每个像素的最低有效位,然后将图像保存在外部存储器中。现在,当我打开 PNG 图像时,我不知道如何进行反向操作,这意味着我不知道如何从这些位中获取字符串。有人可以解释一下吗?谢谢。

这是在最低有效节拍中插入字符串的代码。

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode)
    {
        case FILE_SELECT_CODE:
            if(resultCode==RESULT_OK)
            {
                Uri uri2 = data.getData();
                String[] proj = {MediaStore.Images.Media.DATA};
                CursorLoader cursorLoader = new CursorLoader(this,uri2,proj,null,null,null);
                Cursor cursor = cursorLoader.loadInBackground();
                int column_index=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                String uri = cursor.getString(column_index);
                Bitmap img = BitmapFactory.decodeFile(uri);
                int height = img.getHeight();
                int width = img.getWidth();
                int[] pixel = new int[height * width];
                img.getPixels(pixel, 0, width, 1, 1, width - 1, height - 1);
                String key = "prova";
                byte[] b = key.getBytes();
                int count = 0;
                for (byte current_byte : b) {
                    for (int j = 7; j >= 0; j--) {
                        int lsb = (current_byte >> j) & 1;
                        pixel[count] = (pixel[count] & 0xfffffffe) + lsb;
                        count++;
                    }
                }
                Bitmap newImg = Bitmap.createBitmap(pixel,0,width, width-1,height-1, Bitmap.Config.ARGB_8888);
                ImageView imageView = new ImageView(this);
                imageView.setImageBitmap(newImg);
                setContentView(imageView);
                String filename = "myimage.png";
                File file2 = new File(getExternalFilesDir(null), filename);
                try
                {
                    FileOutputStream fileOutputStream = new FileOutputStream(file2);
                    newImg.compress(Bitmap.CompressFormat.PNG,100,fileOutputStream);
                    fileOutputStream.flush();
                    fileOutputStream.close();
                }
                catch (Exception e1)
                {
                    e1.printStackTrace();
                }
            }
            break;
    }
}

以下是从该位读取字符串的代码的开头。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final Intent intent = getIntent();
    Uri uri2 = intent.getData();
    String[] proj = {MediaStore.Images.Media.DATA};
    CursorLoader cursorLoader = new CursorLoader(this,uri2,proj,null,null,null);
    Cursor cursor = cursorLoader.loadInBackground();
    int column_index=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    String uri = cursor.getString(column_index);
    Bitmap img = BitmapFactory.decodeFile(uri);
    int height = img.getHeight();
    int width = img.getWidth();
    int[] pixel = new int[height * width];
    img.getPixels(pixel, 0, width, 1, 1, width - 1, height - 1);

在这部分的最后,我从图像中获取了像素数组,我必须使用它从最不重要的节拍中获取字符串。

PS:我不能使用 BufferedImage。

【问题讨论】:

  • 如果你正在做速记算法,只需使用 Alpha 通道来存储字符(仅限 ASCII 0-255)。
  • 对不起,我不知道你在说什么。你能解释一下怎么做吗?
  • 您正试图在图像中隐藏文本。不是吗?
  • 是的,完全正确。我正在这样做。
  • 所以,使用图像透明度(不太明显)。但是你可以使用的字符是有限的:ASCII 0-255,而不是 Unicode。

标签: java android image steganography


【解决方案1】:

像这样?

int[] pixel;
byte[] b = new byte[5 * 2]; // Default Java string encoding uses 2 bytes per symbol
// Read .png, fill pixel array etc
// Also you might wish to save string length to the pixel array first

for (int count = 0; count < pixel.length / 8 && count < b.length; count++) {
    int current_byte = 0;
    for (int j = 7; j >= 0; j--) {
        int lsb = (pixel[count * 8 + j] & 1) << j;
        current_byte += lsb;
    }
    b[count] = current_byte;
}
String key = new String(b, java.nio.charset.Charset.defaultCharset ()); // You might want to consider UTF-8

【讨论】:

  • 我做不到 b[count]=current_byte;因为它们是不兼容的类型(byte - int)。有错误吗?
  • 很遗憾,你告诉我的所有代码都不起作用。
猜你喜欢
  • 2014-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多