How to Get Pixel Color in Android
Asked Answered
E

3

61

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

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

Cheers.

Ensemble answered 18/10, 2011 at 12:41 Comment(0)
M
135

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 = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);

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.

Monkshood answered 18/10, 2011 at 12:48 Comment(10)
I Appreciate your fast and correct reply, Raz. as you see, thumbs are up twice for your answer. what you gave me was the whole image pixel RGB values But you may got me wrong. lemme explain it in this way: i get my coordinates ( e.g. in 32.0 x 112.999985) and now i want the colour for this coordinate to be set in a TextView. let's say that coordinate is red and TextView shows " It is RED". FYI i'm working only on Red,Green and Blue in my image. i don't wanna look like a dumb in this, but you should know i just started working with android, so please help me out. cheers.Ensemble
Thanks again Raz. I edited my codes based on your codes in my question above. But there must be a stupid mistake here that it doesn't run properly. It runs with no errors, but it doesn't show anything in the TextView (textViewCol). Can you see the problem? (Again, don't judge me, I'm new in this) Cheers.Ensemble
Hey, everybody is a rookie at some point, I think i still am :-). Have you checked if the setText sentence is even executed? Make sure where you press the bitmap it's really red including the alpha channel (Color.RED = #ffff0000) you can check the image using Paint.net or some other graphic program. By the way, you don't need the three lines where you get the int value of the channels if you simply compare the int value.Monkshood
By the way, you should read how to use properly the onTouchListener in the android developer site. Because you are doing it in every event it receives. You probably meant to do it on down event which you can filter with "if(event.getAction() == MotionEvent.ACTION_DOWN)". Than execute your code.Monkshood
Hey! yeah, I keep on changing and checking setText sentence,but still it doesn't show anything. it remain blank while the other TextView is keep on showing the coordinates. the other TextView, is showing the coordinates using String.valueOf(event.getX()) and String.valueOf(event.getY()) and it is perfectly executing. only if we can get the colour for these two (coordinates) and then put it into a TextView ( i have it here named textViewCol. so in that case, the color of the exact place the user is selecting will be appear. Cheers, and BTW, u r not a rookie! ;)Ensemble
I couldn't get this to work. It is returning NullPointerExceptionHogfish
In what part does it return NullPointerException?Monkshood
I get an error as shown here: #21347529Ethology
Your answer is a bit wrong. See here to get the whole picture: android-er.blogspot.ru/2012/10/…Banal
How to get accent color from bitmap >Tidy
P
7

You can modify this for your requirement. This snippet will help you get the pixel color.

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;
}
Plenish answered 8/11, 2016 at 23:50 Comment(0)
O
3

This works more accurately for me. The key here is to use the View.getDrawingCache instead of 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;   
    }
});
Orphanage answered 23/9, 2017 at 7:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.