简体   繁体   中英

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 . 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 .

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.

1. Breaking out of the loops

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

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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