Skip to content Skip to sidebar Skip to footer

How Do I Select A Region By Color In A Bitmap?

Please suggest an efficient way to select a region by color in a bitmap. Then replace this selected region-color to your desired color. For example, If an image contains four colo

Solution 1:

If you want to get the color of every pixel you could do it this way:

for(int i=0;i<bitmap.getWidth();i++){
for(int j=0;j<bitmap.getHeigth();j++){
    int pixel = bitmap.getPixel(i,j);
    if(pixel == Color.RED){
        //Do something
    }
}
}

So what you could do is first find the color of the pixel tapped by the user and then use the above technique to find the pixels of that particular color.

You can use the following to get the color code of the pixel tapped :

int ColorCode = imageView.getDrawingCache().getPixel(x, y);

Here x,y are the coordinates. You can refer this link for additional info - link

And don't forget to scale down the bitmap before loading it into memory to prevent OutOfMemory issues. You can refer android documentation to find how to do that. Handling large bitmaps

Solution 2:

After spending a lot of time I got a useful link to do this work. It's called Kids Palette and here I'm sharing the link of source on GitHub.

Solution 3:

What about using Palette API?

Post a Comment for "How Do I Select A Region By Color In A Bitmap?"