如何获取位图android上的像素红色数

时间:2017-02-01 13:38:17

标签: java android canvas bitmap

我希望获得位图图像上所有红色像素的数量,在我绘制它并与背面图像合并之后。 我怎样才能做到这一点?请给我更多细节,我将非常感谢,谢谢!

Example: Count of red pixels

3 个答案:

答案 0 :(得分:0)

遍历位图中的每个像素

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

答案 1 :(得分:0)

您有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);
    }
}

答案 2 :(得分:0)

我有这样的事情:

    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);*/
            }
        }
    }