如何在Android中获取像素颜色

Sea*_*ean 50 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;    }     
    });
Run Code Online (Sandbox Code Playgroud)

现在我需要做的是; 得到color (RGB value)确切的坐标用户选择,后来分配到每一个#FF0000,#00FF00#0000FF但现在,请大家帮帮忙让基于什么我的像素颜色.

干杯.

Raz*_*Raz 116

您可以从视图中获取像素:

ImageView imageView = ((ImageView)v);
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
int pixel = bitmap.getPixel(x,y);
Run Code Online (Sandbox Code Playgroud)

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

int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
Run Code Online (Sandbox Code Playgroud)

Color函数返回每个通道中的值.所以你要做的就是检查Red是255还是绿色和蓝色是0,而不是将textView文本设置为"它是红色".请注意,说某些东西是红色不仅仅是红色通道大于零.当然,Cos 255-Green和255-Red是黄色的.您还可以将像素与不同颜色进行比较.例如:

if(pixel == Color.MAGENTA){
   textView.setText("It is Magenta");
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.

  • 你的答案有点不对劲.请参阅此处以了解全貌:http://android-er.blogspot.ru/2012/10/get-touched-pixel-color-of-scaled.html (2认同)

one*_*exf 7

您可以根据自己的要求修改它。此代码段将帮助您获得像素颜色。

public static int getDominantColor(Bitmap bitmap) {
    Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, 1, 1, true);
    final int color = newBitmap.getPixel(0, 0);
    newBitmap.recycle();
    return color;
}
Run Code Online (Sandbox Code Playgroud)