Sean Sean - 1 year ago 108
Android Question

How to Get Pixel Colour in Android?

I'm using Intent to call and show an image from Gallery, and now I made it enable to get me the coordinates of the image in a TextView using these:

final TextView textView = (TextView)findViewById(;
final TextView textViewCol = (TextView)findViewById(;
targetImage.setOnTouchListener(new ImageView.OnTouchListener(){
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 =;
int blueValue =;
int greenValue =;
if(pixel == Color.RED){
textViewCol.setText("It is RED");

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

Now what I need to do is; to get the
colour (RGB value)
of the exact coordinates the user selects and later on assign each to
but for now, please help to get the Pixel colour based on what I have.


Raz Raz
Answer Source

You can get the pixel from the view like this:

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

Now you can get each channel with:

int redValue =;
int blueValue =;
int greenValue =;

The Color functions return the value in each channel. So all you have to do is check if Red is 255 and green and blue are 0, than set the textView text to "it is red". Just pay attention that saying that something is red is not simply that the red channel is the greater than zero. 'Cos 255-Green and 255-Red is yellow, of course. You can also just compare the pixel to different color. for example:

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

Hope it helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download