I have an image view on my screen. The image consist of shades of gray. When I click on the picture I want to get rgb code of where I click. It is possible on Android?
            Asked
            
        
        
            Active
            
        
            Viewed 4,335 times
        
    0
            
            
        - 
                    I think this is pretty fully answered at http://stackoverflow.com/questions/7807360/how-to-get-pixel-colour-in-android. – Neil Townsend Jun 05 '13 at 12:09
 - 
                    When I put the color, I set a tag to the image view. Later, I do getTag() to get the information – mromer Jun 05 '13 at 12:10
 
2 Answers
5
            Try this in your onTouch call:
public boolean onTouch (View v, MotionEvent ev) 
{
    final int action = ev.getAction();
    final int evX = (int) ev.getX();
    final int evY = (int) ev.getY();
    switch (action) {
        case MotionEvent.ACTION_DOWN :
           break;
        case MotionEvent.ACTION_UP :
           ImageView img = (ImageView) findViewById (YOUR_IMG_DRAWABLE);
           img.setDrawingCacheEnabled(true); 
           Bitmap imgbmp = Bitmap.createBitmap(img.getDrawingCache()); 
           img.setDrawingCacheEnabled(false);
           int pxl = imgbmp.getPixel(evX, evY);
           int redComponent = Color.red(pxl);
           int greenComponent = Color.green(pxl);
           int blueComponent = Color.blue(pxl);
           ...USE COLOR HERE...
           break;
    }
}
Hope that helps!
        dberm22
        
- 3,187
 - 1
 - 31
 - 46
 
1
            
            
        You can use the below for reference. You can override imageview on touch listener and get the pixel and rgb values of the imageview.
public class MainActivity extends Activity {
ImageView iv;
int redValue,blueValue,greenValue;
int pixel; 
Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    iv= (ImageView) findViewById(R.id.imageView1);
    BitmapDrawable bitmapDrawable = (BitmapDrawable)iv.getDrawable();
    bitmap = bitmapDrawable.getBitmap();
    iv.setOnTouchListener(new OnTouchListener()
    {
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:   
               // int color = bitmap.getPixel(event.getX(),event.getY());
                break;
            case MotionEvent.ACTION_UP: 
                pixel = bitmap.getPixel((int)event.getX(),(int)event.getY());
                redValue = Color.red(pixel);
                blueValue = Color.blue(pixel);
                greenValue = Color.green(pixel);
                System.out.println("...."+redValue+"..blue"+blueValue+"..."+greenValue+"color"+pixel);
                Toast.makeText(MainActivity.this,""+pixel, 1000).show();
                break;
            }
            return true;
    }
    });
}
}
        Raghunandan
        
- 132,755
 - 26
 - 225
 - 256