-1

I am trying to get the color value of a particular pixel in a Bitmap object. I have the following function in my MainActivity.java:

@RequiresApi(api = Build.VERSION_CODES.Q)
private boolean isCenterWhitePixel(Bitmap bitmap) {
    Color color = bitmap.getColor(150, 150);
    int[] rgbValues = { (int) color.red(), (int) color.green(), (int) color.blue()};
    if (rgbValues[0] == 255 && rgbValues[1] == 255 && rgbValues[2] == 255) {
        return true;
    }
    else {
        return false;
    }
}

Note: the line @RequiresApi(api = Build.VERSION_CODES.Q) was added to the top because on the line Color color = bitmap.getColor(150, 150); android studios' tooltip complained with the following:

Call requires API level Q (current min is 23): android.graphics.Bitmap#getColor

So I followed what android studio suggested as a solution and added that @RequiresAPI line. As a result I got the following error:

java.lang.NoSuchMethodError: No virtual method getColor(II)Landroid/graphics/Color; in class Landroid/graphics/Bitmap; or its super classes (declaration of 'android.graphics.Bitmap' appears in /system/framework/framework.jar)

Any way to get the Bitmap.getColor function to work?

  • You can use [the `getPixel(int, int)` method](https://developer.android.com/reference/android/graphics/Bitmap#getPixel(int,%20int)) instead. It returns a simple `int` value that you could use with static `Color` class methods to get the components; e.g., `int color = bimap.getPixel(150, 150);`, `int red = Color.red(color);`. However, you don't really need to grab the individual components just for that comparison. You could simply do `return (color == Color.WHITE);`. – Mike M. Jul 02 '20 at 03:13

1 Answers1

1

Try this

ImageView imageView = findViewById(R.id.image_view);
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
int pixel = bitmap.getColor(1,1);

for more Click here! i think this can help

  • Hi Tama, Please don't link external link because external link sharing can cause sometime link expired or removed. please share the answer appropriate to user based on snippet shared. – Dilip D Jul 02 '20 at 03:41