在Android上获取背景中的像素颜色

Blo*_*ops 6 android android-service

我需要创建一个服务,不断检查Android屏幕的坐标是否有像素的颜色.

例如.

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

if(pixel == Color.MAGENTA){
   //Stop Service
}
Run Code Online (Sandbox Code Playgroud)

然而,不仅仅是我的应用程序,我需要它在后台服务,即使我切换应用程序时仍会检查颜色.

假设设备已植根,是否有任何超级用户命令可以满足我的需求?

Blo*_*ops 3

对于那些需要像我一样做某事的人,

我使用系统的命令 /system/bin/screencap -p 捕获屏幕并检查屏幕截图上的像素。

此方法需要root权限

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();
    }
}
Run Code Online (Sandbox Code Playgroud)