【问题标题】:How to Get Pixel Color in Android如何在 Android 中获取像素颜色
【发布时间】:2011-10-18 12:41:14
【问题描述】:

我正在使用 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;    }     
    });

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

干杯。

【问题讨论】:

    标签: android pixel


    【解决方案1】:

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

    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);
    

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

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

    希望对你有帮助。

    【讨论】:

    • 感谢您快速而正确的回复,Raz。如您所见,您的回答竖起大拇指两次。你给我的是整个图像像素的 RGB 值但你可能弄错了。 lemme 以这种方式解释它:我得到了我的坐标(例如在 32.0 x 112.999985 中),现在我希望在 TextView 中设置这个坐标的颜色。假设坐标为红色,TextView 显示“It is RED”。仅供参考,我只在我的图像中处理红色、绿色和蓝色。我不想在这方面看起来像个笨蛋,但你应该知道我刚刚开始使用 android,所以请帮帮我。干杯。
    • 再次感谢拉兹。我根据上面问题中的代码编辑了我的代码。但是这里肯定有一个愚蠢的错误,它不能正常运行。它运行时没有错误,但在 TextView (textViewCol) 中不显示任何内容。你能看出问题吗? (再次,不要评判我,我是新来的)干杯。
    • 嘿,每个人在某些时候都是新手,我想我仍然是:-)。您是否检查过 setText 语句是否已执行?确保按下位图的位置是真正的红色,包括 alpha 通道 (Color.RED = #ffff0000),您可以使用 Paint.net 或其他一些图形程序检查图像。顺便说一句,如果您只是比较 int 值,则不需要获得通道的 int 值的三行。
    • 顺便说一下,你应该在android开发者网站上阅读如何正确使用onTouchListener。因为你在它收到的每一个事件中都这样做。您可能打算在向下事件上执行此操作,您可以使用“if(event.getAction() == MotionEvent.ACTION_DOWN)”进行过滤。比执行你的代码。
    • 你的回答有点错误。看这里以获得全貌:android-er.blogspot.ru/2012/10/…
    【解决方案2】:

    您可以根据自己的需要进行修改。这个sn-p会帮你获取像素颜色。

    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;
    }
    

    【讨论】:

      【解决方案3】:

      这对我来说更准确。这里的关键是使用View.getDrawingCache 而不是DrawableBitmap

      palleteView.setOnTouchListener(new View.OnTouchListener() {
      
          @Override
          public boolean onTouch(View v, MotionEvent ev) {
              // TODO Auto-generated method stub
              ImageView img = (ImageView) v;
      
              final int evX = (int) ev.getX();
              final int evY = (int) ev.getY();
      
              img.setDrawingCacheEnabled(true);
              Bitmap imgbmp = Bitmap.createBitmap(img.getDrawingCache());
              img.setDrawingCacheEnabled(false);
      
              try {
                  int pxl = imgbmp.getPixel(evX, evY);
      
                  pickedColorView.setBackgroundColor(pxl);
      
              }catch (Exception ignore){
              }
              imgbmp.recycle();
      
              return true;   
          }
      });
      

      【讨论】:

        猜你喜欢
        • 2017-03-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-12
        • 1970-01-01
        相关资源
        最近更新 更多