Get pixel color in background on Android

I need to create a service that will check the android screen coordinate for pixel color.

Eg.

Bitmap bitmap = ((BitmapDrawable)getBackground()).getBitmap(); int pixel = bitmap.getPixel(x,y); if(pixel == Color.MAGENTA){ //Stop Service } 

However, instead of my application, I need it to be in the background, which tracks color even when switching applications.

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

+5
source share
3 answers

Those who need to do something like I do

I used the system command / system / bin / screencap -p to capture the screen and check the pixel in the screenshot.

This method requires 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(); } } 
+1
source
 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); } } } 

Change the updateMsg () method of the main activity, AndroidDetechTouchActivity.java to enable color and update the TextView 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); } } 
+3
source

As noted. ineffective at all ... probably the best way 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; } 
-2
source

Source: https://habr.com/ru/post/1243727/


All Articles