How to set image filter only in some part of image in Android

27 Views Asked by At

I have some image with white background that have some text and logo inside. I want to set a filter only to text and logo but not the white background.

I have tried to set filter but it just change the entire image color. The logic that I want in the filter is like

if(color == white){
  //do nothing
} else {
  //turn gray 
}

Is there a way to do this?

1

There are 1 best solutions below

0
Nattakul On

Just found answer to my question.

Assuming you have bitmap of an image name bitmap

                val width = bitmap.width
                val height = bitmap.height
                val pixels = IntArray(width*height)
                bitmap.getPixels(pixels, 0, width, 0, 0, width, height)

                for (i in pixels.indices) {

                    val red = Color.red(pixels[i])
                    val green = Color.green(pixels[i])
                    val blue = Color.blue(pixels[i])

                    if( /* check if pixel rgb meets your condiion*/ ){
                        //Change your rbg value here. In this example increase rgb by 1
                        pixels[i] = Color.rgb(red+1, green+1, blue+1)
                    }
                }

                val newBitmap = Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888)