Get pixel color in background on Android

python get pixel color from screen
c# get pixel color from screen
android get pixel color on touch
vba get pixel color
how to get pixel value of image in android
how to get pixel value of image in android studio
get color under mouse python
bitmap pixel color

I need to create a service that keeps checking a coordinate of an android screen for the color of a pixel.

Eg.

Bitmap bitmap = ((BitmapDrawable)getBackground()).getBitmap();
int pixel = bitmap.getPixel(x,y);

if(pixel == Color.MAGENTA){
   //Stop Service
}

However instead of just my app, I need it to be in a background service which keeps checking for the color even when I switch apps.

Assuming the device is rooted, is there any superuser commands that does what I need?

public class TouchView extends ImageView {

 Bitmap bitmap;
 double bmWidth, bmHeight; 

 public TouchView(Context context) {
  super(context);
  // TODO Auto-generated constructor stub
  init();
 }

 public TouchView(Context context, AttributeSet attrs) {
  super(context, attrs);
  // TODO Auto-generated constructor stub
  init();
 }

 public TouchView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  // TODO Auto-generated constructor stub
  init();
 }

 private void init(){

  bitmap = ((BitmapDrawable)getBackground()).getBitmap();
  bmWidth = (double)bitmap.getWidth();
  bmHeight = (double)bitmap.getHeight();
 }

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  // TODO Auto-generated method stub
  setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
       MeasureSpec.getSize(heightMeasureSpec));
 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {
  // TODO Auto-generated method stub


  switch(event.getAction()){
  case MotionEvent.ACTION_DOWN:
  case MotionEvent.ACTION_MOVE:
   float x = event.getX();
   float y = event.getY();

   int color = getColor(x, y);
      ((AndroidDetechTouchActivity)getContext()).updateMsg("Touched@" + x + " : " + y, color);

   break;
  case MotionEvent.ACTION_UP:
   ((AndroidDetechTouchActivity)getContext()).updateMsg("", 0);
   break;
  }

  return true;
 }

 private int getColor(float x, float y){

  if ( x < 0 || y < 0 || x > (float)getWidth() || y > (float)getHeight()){
   return 0; //Invalid, return 0
  }else{
   //Convert touched x, y on View to on Bitmap
   int xBm = (int)(x * (bmWidth / (double)getWidth()));
      int yBm = (int)(y * (bmHeight / (double)getHeight()));

   return bitmap.getPixel(xBm, yBm);
  }

 }

}

Modify updateMsg() method of main activity, AndroidDetechTouchActivity.java, to include color and update TextView's TextColor.

public class AndroidDetechTouchActivity extends Activity {

 TextView msg;
 TouchView touchView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        msg = (TextView)findViewById(R.id.msg);
        touchView = (TouchView)findViewById(R.id.touchview);

    }

    public void updateMsg(String tMsg, int color){
     msg.setTextColor(color);
     msg.setText(tMsg);
    }

}

How To Get Pixel Color in Android, How to build a simple Calculator app using Android Studio? Java Robot Class | Get the pixel Color of a given point. Robot  First, you can get the Bitmap with the BitmapFactory like this: Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic1); Now you have the Bitmap. On this Bitmap, you can call the function getPixel(int x, int y) to get the Color of this Pixel.

To those who need to do something like I am doing,

I used the system's command /system/bin/screencap -p to capture the screen and check for the pixel on the screenshot.

This method requires the root permission

private void getPixelColor(int xcoord, int ycoord)
{
    try {
        Process sh = Runtime.getRuntime().exec("su", null,null);

        OutputStream os = sh.getOutputStream();
        os.write(("/system/bin/screencap -p " + "/sdcard/colorPickerTemp.png").getBytes("ASCII"));
        os.flush();

        os.close();
        sh.waitFor();

        Bitmap screen = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + File.separator + "colorPickerTemp.png");
        int pixel = screen.getPixel(xcoord ,ycoord + statusHeight);
        Log.d("pixel color", "Pixel Color: + " + Integer.toHexString(pixel) + " at x:" + xcoord + " y:" + ycoord);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

Java Robot Class, had set src to ImageView, in case of background drawable it will be null Bitmap​… getPixel(xCoord, yCoord); //then do what you want with the pixel data, e.g int /questions/12496339/android-imageview-get-pixel-color-from-scaled-image  Change wallpaper on your Pixel phone Y o u can change the background images on your phone's Home screens and lock screens. You can choose from your own photos or use images that come on your phone, including images that change automatically.

you can draw to bitmap and take color from bitmap, code as follow:

Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), 
view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
view.draw(c);
int color = bitmap.getPixel(x, y);

Color of a screen pixel, This will be a very long read, because first I'll give you some background I created this script to get pixel colors of an app that's always in landscape mode. which mentions this plugin, but that also appears to be broken in Android 10. An Android color is a 32-bit integer value consisting of four eight bit parts (4x8=32). The four parts are tagged ARGB. This is the amount of Red, Green and Blue in the color, plus how opaque (see through) it is, called the Alpha value, the lower the alpha value the more transparent the color appears.

As stated. inefficient in general... probably a better way for you to implement your application.

 public boolean checkColor() {
       View rootView = findViewById(android.R.id.content).getRootView();
       rootView.setDrawingCacheEnabled(true);
       Bitmap bitmap = rootView.getDrawingCache();
       rootView.setDrawingCacheEnabled(false);
       int pixel = bitmap.getPixel(x,y);
       if(pixel == Color.MAGENTA){
           return true;
       }
       return false;
    }

Get color at the spot of a image on touch event: Android, You can change the background images on your phone's Home screens and lock screens. Some of these steps work only on Android 10 and up. Tip: The color of your notifications, Quick Settings, and All apps list depends on which Manage screen & display settings · Get help in an emergency using your Pixel phone. The google has reviled their pretty smartphone Pixel 2 and Pixel 2 XL, both devices have come with the OLED displays, but its show color is very different to each other. So in this ways, if you want to change the color profile of the Pixel devices then you need’s to set up few option that really easy.

[GUIDE] Check pixels if they match a specific color [Semi Advanced , Some of these steps work only on Android 10 and up. Tip: You can get to the settings you use most from any screen with Quick Settings. To save your phone's battery, darken the background on some of your phone's screens. Styles & wallpapers (Pixel 4, 3, and 2): Change your phone's accent color, icon shapes, and  If a width and a height are specified, get(x, y, w, h) returns a PImage corresponding to the part of the original PImage where the top left pixel is at the (x, y) position with a width of w a height of h. Getting the color of a single pixel with get(x, y) is easy, but not as fast as grabbing the data directly from pixels[].

Change wallpaper on your Pixel phone, Here's how to reverse the color scheme on your Pixel / Pixel XL. Remove the background: be it a green screen, a blue screen or simply a white background behind an object in an image that you found on Google images; PixelLab can make it transparent for you.

Manage screen & display settings, Returns an optional array of private data, used by the UI system for some bitmaps​. int, getPixel(int x, int y). Returns the Color  When you get back up, go ahead and open the Pixel Dock app, which you'll now find in your app drawer. From here, tap the "Choose Color" button, then use the following menu to adjust your dock's background color and transparency. The first slider changes transparency, and the other three can be used to change the color.

Comments
  • This sounds horribly inefficient and cumbersome. Why not explore other options?
  • use broadcast reciever in your service and send broadcast from your activity, and subscribe to the broadcast in your service
  • What is the aim here? What are you trying to check? You want to know when for example a magenta pixel is displayed at x, y position on the screen from any app (eg Facebook)? The answers are all looking at things rendered only within your app so you need to be clear
  • Hi, thanks for the reply and this great code, however does this gets the pixel color of other apps too?
  • Found a better way to go about this ? Screencap is kinda slow
  • Needs to be done from a background service though. This only works in-app.