簡體   English   中英

如何編輯圖像中像素的位圖

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

我已經寫了一個密寫算法,但是要花很長時間才能完成。 這是因為我創建了位圖的新實例BitmapStegan ,並且從舊位bitmap獲取了每個像素。 無論是否修改它,都必須在新的位圖對象中進行設置。 因此,即使我只需要編輯其中的一些像素,最終還是要遍歷所有像素。

我該如何解決這個問題?

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

首先,您是否真的需要原始圖像的副本? 如果是,則因為要比較原始圖像與隱秘圖像之間的統計差異,所以要創建位圖的副本 這樣,您可以一次性創建所有像素,速度更快。 如果不需要副本,只需將更改直接應用於原始圖像對象。 無論哪種方式,您都只需要修改一個圖像,從現在開始,我將其稱為image

現在,關於如何僅迭代足夠的像素進行嵌入,您有兩種選擇。 嵌入整個秘密后,要么對圖像的行和列使用循環並在其中循環,要么為NumberBitsInMessage創建一個計數器,並在嵌入位時顯式更改像素坐標。

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.嵌入位計數器

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