How do I select a region by color in a Bitmap?

五迷三道 提交于 2019-12-11 10:22:43

问题


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 colors say red, green, yellow, orange. Now suppose user clicked on yellow color, so select yellow colored region surrounding to the clicked position and now replace the yellow color to blue.

Hope, I explained the question properly. Kindly comment if need any more clarification.

As per my knowledge, bitmap is a memory consuming task and results into out of memory error.

Please share any link or sample code that will help me to implement this feature to my app.


回答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




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




回答3:


What about using Palette API?

  • Look the article [Extracting Colors to a Palette with Android]Lollipop(https://www.bignerdranch.com/blog/extracting-colors-to-a-palette-with-android-lollipop/)
  • Also read the official documentation about this tool


来源:https://stackoverflow.com/questions/33452785/how-do-i-select-a-region-by-color-in-a-bitmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!