android - 如何在 Android 中获取像素颜色

标签 android pixel

我正在使用 Intent 调用并显示 Gallery 中的图像,现在我可以使用以下方法获取 TextView 中图像的坐标:

final TextView textView = (TextView)findViewById(R.id.textView); 
final TextView textViewCol = (TextView)findViewById(R.id.textViewColor);
targetImage.setOnTouchListener(new ImageView.OnTouchListener(){     
    @Override   
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub       
        int x=0;
        int y=0;
        textView.setText("Touch coordinates : " +       
        String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));
        ImageView imageView = ((ImageView)v);
        Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
        int pixel = bitmap.getPixel(x,y);
        int redValue = Color.red(pixel);
        int blueValue = Color.blue(pixel);
        int greenValue = Color.green(pixel);
        if(pixel == Color.RED){
               textViewCol.setText("It is RED");
            }

        /*if(redValue == 255){
            if(blueValue == 0)
                if(greenValue==0)
               textViewCol.setText("It is Red");
            }*/
        return true;    }     
    });

现在我需要做的是;获取用户选择的确切坐标的 颜色(RGB 值),然后将每个坐标分配给 #FF0000#00FF00 >#0000FF 但是现在,请根据我所拥有的帮助获取像素颜色。

干杯。

最佳答案

您可以像这样从 View 中获取像素:

ImageView imageView = ((ImageView)v);
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
int pixel = bitmap.getPixel(x,y);

现在您可以通过以下方式获取每个 channel :

int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);

颜色函数返回每个 channel 中的值。因此,您所要做的就是检查红色是否为 255,绿色和蓝色是否为 0,然后将 textView 文本设置为“它是红色的”。请注意,说某事是红色的不仅仅是红色 channel 大于零。 'Cos 255-Green 和 255-Red 当然是黄色的。 您也可以将像素与不同的颜色进行比较。 例如:

if(pixel == Color.MAGENTA){
   textView.setText("It is Magenta");
}

希望对你有帮助。

关于android - 如何在 Android 中获取像素颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7807360/

相关文章:

ios - ActionScript3.0 - 如何获取坐标处像素的颜色 (uint)? (Stage3D, Flare3D)

android - 如何在 android.. 的 Canvas 上绘制 TextView?

android - 第二次单击时显示下拉菜单 - 带有下拉菜单的 AutoCompleteTextView

java - 这会造成内存泄漏吗?

ios - 获取图像中的像素值

python - 从图像中删除背景 - Python

java - 将文本数据存储在一个/多个文件中以使其可用于各种平台的最佳方法是什么?

android - 错误 :Could not resolve all files for configuration ':app:debugCompileClasspath'

ios - 照片编辑应用 iOS

python - 在Windows 7上用Python快速获取屏幕上某些像素点的颜色