14

I want to get the color of the spot or pixel where I will touch a image in Android. I searched a lot on net, but got nothing. Please anyone help me.

Shirish Herwade
  • 11,461
  • 20
  • 72
  • 111

2 Answers2

37

try this:

final Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
imageView.setOnTouchListener(new OnTouchListener(){
        @Override
        public boolean onTouch(View v, MotionEvent event){
        int x = (int)event.getX();
        int y = (int)event.getY();
        int pixel = bitmap.getPixel(x,y);

        //then do what you want with the pixel data, e.g
        int redValue = Color.red(pixel);
        int blueValue = Color.blue(pixel);
        int greenValue = Color.green(pixel);        
        return false;
        }
   });
Tarsem Singh
  • 14,139
  • 7
  • 51
  • 71
roarster
  • 4,058
  • 2
  • 25
  • 38
  • 6
    Beware that image can be scaled. Solution: http://stackoverflow.com/questions/12496339/android-imageview-get-pixel-color-from-scaled-image – fikr4n Jan 23 '15 at 08:10
3

You can compute the image coordinates of the pixel that was clicked and read the pixel from the image data, like

Bitmap.getPixel(xcord,ycord)
Karan_Rana
  • 2,813
  • 2
  • 26
  • 35
  • I need to get the pixel when i have scaled my image. I tried every solution but get only white color in return – Yyy Jun 27 '17 at 14:35