0

I was looking for a way how to extract the pixel value (RGB) of a selected (x,y) from my screen

(My display countains many images, not only the background)

thanks

manlio
  • 18,345
  • 14
  • 76
  • 126
Omar_0x80
  • 783
  • 1
  • 9
  • 16
  • The solutions is here: http://stackoverflow.com/questions/6272859/getting-the-pixel-color-value-of-a-point-on-an-android-view-that-includes-a-bitm – eleven Apr 13 '14 at 14:16
  • i have already checked it, but i didn't find how to get the values – Omar_0x80 Apr 13 '14 at 14:20

1 Answers1

3

First of all you need the root container of your activity. Suppose xml of your activity looks like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/container"
             android:orientation="vertical"
             android:layout_width="match_parent"
             android:layout_height="match_parent">

             <!-- All your stuff-->

</RelativeLayout>

Then in the activity find the root and using jkhouw1 method get Bitmap which has method getPixel:

@Override
protected void onCreate(Bundle savedInstanceState) {
    //...
    View container = findViewById(R.id.container);
    Bitmap bitmap = loadBitmapFromView(container);
    int pixel = bitmap.getPixel(100, 100);
    int r = Color.red(pixel);
    int b = Color.blue(pixel);
    int g = Color.green(pixel);
}

public static Bitmap loadBitmapFromView(View v) {
    Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
    v.draw(c);
    return b;
}
eleven
  • 6,779
  • 2
  • 32
  • 52