简体   繁体   English

如何编辑图像中像素的位图

[英]how to edit a bitmap of the pixels in an image

I have written a steganography algorithm, but it takes a long time to complete. 我已经写了一个密写算法,但是要花很长时间才能完成。 This is because I create a new instance of bitmap, BitmapStegan , and I take each pixel from my old bitmap, bitmap . 这是因为我创建了位图的新实例BitmapStegan ,并且从旧位bitmap获取了每个像素。 Whether I modify it or not, I have to set it in the new bitmap object. 无论是否修改它,都必须在新的位图对象中进行设置。 Therefore, I end up looping through all of the pixels, even though I only need to edit a few of them. 因此,即使我只需要编辑其中的一些像素,最终还是要遍历所有像素。

How can I address that problem? 我该如何解决这个问题?

Bitmap BitmapStegan = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
for(int i=0; i<bitmap.getWidth(); i++){
    for(int j=0; j<bitmap.getHeight(); j++){
        int pixel=bitmap.getPixel(i, j);
        int red= Color.red(pixel);
        int green=Color.green(pixel);
        int blue=Color.blue(pixel);

        if (NumberBitsInMessage>0) {
            /*
            I put here my bit to red and greed and blue with LSB method
            */
        }
        BitmapStegan.setPixel(i, j, Color.argb(Color.alpha(pixel), red, green, blue));
    }
}
imageView.setImageBitmap(BitmapStegan);

First things first, do you really need a copy of your original image? 首先,您是否真的需要原始图像的副本? If yes, because you want to compare statistical differences between the original and the stego image, you want to create a copy of your bitmap . 如果是,则因为要比较原始图像与隐秘图像之间的统计差异,所以要创建位图的副本 This way, you create all the pixels in one go, which is faster. 这样,您可以一次性创建所有像素,速度更快。 If you don't need a copy, just apply your changes directly to the original image object. 如果不需要副本,只需将更改直接应用于原始图像对象。 Either way, you need to modify only one image, which from now on I will call image . 无论哪种方式,您都只需要修改一个图像,从现在开始,我将其称为image

Now, you have two choices about how to iterate through only enough pixels for embedding. 现在,关于如何仅迭代足够的像素进行嵌入,您有两种选择。 Either use loops for the rows and columns of your image and break out of them after you have embedded the whole secret, or create a counter for NumberBitsInMessage and explicitly change the pixel coordinates as you embed your bits. 嵌入整个秘密后,要么对图像的行和列使用循环并在其中循环,要么为NumberBitsInMessage创建一个计数器,并在嵌入位时显式更改像素坐标。

1. Breaking out of the loops 1.打破循环

embedding:
for (int i = 0; i < image.getWidth(); i++) {
    for (int j = 0; j < image.getHeight(); j++) {
        if (NumberBitsInMessage == 0) {
            break embedding;
        }

        int pixel = image.getPixel(i, j);
        int red = Color.red(pixel);
        int green = Color.green(pixel);
        int blue = Color.blue(pixel);

        /*
        modify pixel logic here
        */
        image.setPixel(i, j, Color.argb(Color.alpha(pixel), red, green, blue));
    }
}

2. Embedding bits counter 2.嵌入位计数器

int width = 0;
int height = 0;
int maxHeight = image.getHeight();

for (int embeddedBits = 0; embeddedBits < NumberBitsInMessage; ) {
    int pixel = image.getPixel(width, height);
    int red = Color.red(pixel);
    int green = Color.green(pixel);
    int blue = Color.blue(pixel);

    /*
    modify pixel logic here
    don't forget to increase `embeddedBits` for each colour you modify
    */
    image.setPixel(width, height, Color.argb(Color.alpha(pixel), red, green, blue));

    height++;
    if (height == maxHeight) {
        width++;
        height = 0;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM