topwangpeng 2016-12-22 02:23 采纳率: 0%
浏览 2708

为会么SurfaceControl.screenshot返回null?

这两天在搞android后台截屏,试了目前网上的各种方式,基本都失败了,反编译了Vysor,参考他的实现,但SurfaceControl.screenshot返回总是null,不知道是哪里有问题,请高手指点!

PS:我的手机已ROOT,CommandLine.execCommand是用root权限执行的。

demo代码如下:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final String packageName = getPackageName();

    ApplicationInfo appInfo = null;
    List<ApplicationInfo> apps = MainActivity.this.getPackageManager().getInstalledApplications(0);
    for (ApplicationInfo app : apps) {
        if (packageName.equals(app.packageName)) {
            appInfo = app;
            break;
        }
    }

    final String sourcePath = appInfo.sourceDir;
    new Thread() {
        @Override
        public void run() {
            String savePath = "/sdcard/temp/";
            for (int i = 0; i < 30; i++) {
                String saveFile = savePath + (i+1) + ".png";
                StringBuilder command=new StringBuilder()
                        .append("CLASSPATH=" + sourcePath)
                        .append(" /system/bin/app_process32 /system/bin " + packageName + ".Main 720 1280 0 " + saveFile);
                CommandLine.CommandResult commandResult = CommandLine.execCommand(command.toString(), true, true);
                if (commandResult.successMsg != null && (commandResult.successMsg.contains("Success") || commandResult.successMsg.contains("success"))) {
                    Log.d("demo", "success");
                }

                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }.start();
}

}

public class Main {
public static void main(String[] args) {
System.out.println("screencap start");

    if (args.length < 4) {
        System.out.println("invalid parameters");
        System.exit(1);
    }

    try {
        int width = Integer.parseInt(args[0]);
        int height = Integer.parseInt(args[1]);
        int rotation = Integer.parseInt(args[2]);
        String savePath = args[3];

        System.out.println("width: " + width);
        System.out.println("height: " + height);
        System.out.println("rotation: " + rotation);
        System.out.println("savePath: " + savePath);

        Bitmap bitmap = screenshot(width, height, rotation);
        if (bitmap == null) {
            System.out.println("screencap return null");
            System.exit(1);
        }

        if (savePic(bitmap, savePath)) {
            System.out.println("screencap success [" + savePath + "]");
            System.exit(0);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    System.exit(1);
}

private static Bitmap screenshot(int width, int height, int rotation) throws Exception
{
    String surfaceClassName;
    if (Build.VERSION.SDK_INT <= 17) {
        surfaceClassName = "android.view.Surface";
    } else {
        surfaceClassName = "android.view.SurfaceControl";
    }

    Class localClass = Class.forName(surfaceClassName);
    Class[] arrayOfClass = new Class[2];
    arrayOfClass[0] = Integer.TYPE;
    arrayOfClass[1] = Integer.TYPE;
    Method localMethod = localClass.getDeclaredMethod("screenshot", arrayOfClass);
    Object[] arrayOfObject = new Object[2];
    arrayOfObject[0] = Integer.valueOf(width);
    arrayOfObject[1] = Integer.valueOf(height);
    Bitmap b = (Bitmap)localMethod.invoke(null, arrayOfObject);
    //Bitmap b = (Bitmap) Class.forName(surfaceClassName).getDeclaredMethod("screenshot", new Class[]{Integer.TYPE, Integer.TYPE}).invoke(null, new Object[]{width, height});
    if (b == null) {
        System.out.println("screenshot fail");
        return null;
    }

    if (rotation == 0) {
        return b;
    }

    Matrix m = new Matrix();
    if (rotation == 1) {
        m.postRotate(-90.0f);
    } else if (rotation == 2) {
        m.postRotate(-180.0f);
    } else if (rotation == 3) {
        m.postRotate(-270.0f);
    }

    return Bitmap.createBitmap(b, 0, 0, width, height, m, false);
}

// 保存图片到Sd卡中
private static boolean savePic(Bitmap bitmap, String strName) {
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(strName);
        if (null != fos) {
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);

            fos.flush();
            fos.close();
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

}

  • 写回答

7条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序