Android不识别Bitmap.getColor,版本问题,这是为什么?

Android not recognizing Bitmap.getColor, something about version, why is this?

我正在尝试获取位图对象中特定像素的颜色值。 我的 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;
    }
}

注意:@RequiresApi(api = Build.VERSION_CODES.Q) 行添加到顶部是因为在 Color color = bitmap.getColor(150, 150); android 行上工作室的工具提示抱怨如下:

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

所以我遵循了 android 工作室建议的解决方案并添加了 @RequiresAPI 行。 结果我得到了以下错误:

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)

有什么方法可以使 Bitmap.getColor 功能发挥作用?

试试这个

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

了解更多点击here!我认为这可以帮助