How to Get Pixel Color in Android How to Get Pixel Color in Android android android

How to Get Pixel Color in Android


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.


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


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