簡體   English   中英

如何在位圖android上獲取像素紅色的計數

[英]How to get count of pixels red color on bitmap android

我想在位圖圖像上繪制所有紅色像素的數量,然后將其與背面圖像合並。 我怎樣才能做到這一點? 請給我更多詳細信息,我將不勝感激,謝謝!

示例:紅色像素數

遍歷位圖中的每個像素

//define your red
static final int RED = Color.RED;

//counting
int count = 0;
for (int x = 0; x <= myBitmap.getWidth(); x++) {
    for (int y = 0; x <= myBitmap.getHeight(); y++) {
        if(myBitmap.getPixel(x, y))
            count++;
    }
}
//done. use the variable count

您有一個Bitmap ,您可以使用以下代碼從中獲取像素顏色:

int countX = bitmap.getWidth();
int countY = bitmap.getHeight();
int redCount = 0;

for (int x = 0; x < countX; x++) {
    for (int y = 0; y < countY; y--) {
        int colorXY = bitmap.getPixel(x, y);
        redCount += Color.red(colorXY);
    }
}

我得到了這樣的東西:

    int countX = bm.getWidth();
    int countY = bm.getHeight();

    int redCount = 0;

    for (int rangeX = 0; rangeX < countX; rangeX++) {
        for (int rangeY = 0; rangeY < countY; rangeY++) {
            int colorXY = bm.getPixel(rangeX, rangeY);

            int r = Color.red(colorXY);
            int g = Color.green(colorXY);
            int b = Color.blue(colorXY);

            if(Color.rgb(r,g,b) == Color.RED) {
                redCount++;
                /*bm.setPixel(rangeX,rangeY,Color.GREEN);*/
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM