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);
}
}
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With