0

I'm trying to get the background color of image that I download with glide.

I think(please correct me if I'm wrong) that the best way is to get the edge pixel(top - left most, bottom - right most etc, my images are usually center image with solid background color)

I'm getting specific pixel color im my recycler view like this(based on the accepted answer here: How to Get Pixel Color in Android):

       val bitmap = (binding.image.drawable as BitmapDrawable).bitmap
        val pixel = bitmap.getPixel(x,y)

My question is how can I get the edge pixel of the image so I can determine the color of the background?

Noam
  • 485
  • 1
  • 7
  • 18

1 Answers1

1

If you have the bitmap, you can use getWidth() and getHeight() (or just bitmap.width and bitmap.height in Kotlin) to get the X and Y coordinates of the far edges (it starts at 0 so it will be height - 1 etc).

Then you can plug those into getColor

with(bitmap) {
    // origin (0,0) is the top left corner
    val topLeft = getColor(0, 0)
    val bottomRight = getColor(width-1, height-1)
    ....
}

(I used with to avoid going bitmap.whatever over and over)


This might not be the best way to get the actual background colour, it really depends (what if the image has a thin border?) and this could be a tricky problem! Just in case it helps, Android has the Palette library which lets you generate a bunch of colour swatches from a Bitmap, so that might be useful if you want to kind of pull out the main colours from an image and pick one

cactustictacs
  • 17,935
  • 2
  • 14
  • 25