【问题标题】:Android: Screencap to bytes directly - Skip saving to fileAndroid:屏幕截图直接转换为字节 - 跳过保存到文件
【发布时间】:2014-06-12 08:46:49
【问题描述】:

我使用以下代码在有根设备上截屏:

    sh =  Runtime.getRuntime().exec("su", null,null);
    os = sh.getOutputStream();
    os.write(("/system/bin/screencap -p " + "/sdcard/img.jpg" + "\n").getBytes("ASCII"));
    os.flush();
    os.close();
    sh.waitFor();
    myBitmap = BitmapFactory.decodeFile("/sdcard/img.jpg");

由于我想经常截屏,我不想先将其保存到文件中,然后再读取字节。有没有更好的方法直接获取截图位图?我只是不想消除这种不必要的延迟。 非常感谢!

更新:

screencap.cpp代码可以查看here

另外,它提到了这一点:

    "usage: %s [-hp] [-d display-id] [FILENAME]\n"
    " -h: this message\n"
    " -p: save the file as a png.\n"
    " -d: specify the display id to capture, default %d.\n"
    "If FILENAME ends with .png it will be saved as a png.\n"
    "If FILENAME is not given, the results will be printed to stdout.\n"

请帮帮我。谢谢!

【问题讨论】:

  • 你可以用命名管道做一些事情。
  • @ChrisStratton 谢谢哥们,你能详细说明一下吗?
  • 虽然有点晚了,你可以在这里查看我的相关问答:stackoverflow.com/a/29832329/1112789

标签: java android root screen-capture su


【解决方案1】:

这将有效,除非您使用的是 8.1 Pixel2 XL,否则您会遇到安全异常。

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.IOException;
import java.io.OutputStreamWriter;

public class BitmapScreencap {
    public final static BitmapScreencap Get = new BitmapScreencap();
    private BitmapScreencap() { }
    public Bitmap Screen() {
        try {
            Process process = Runtime.getRuntime().exec("su");
            OutputStreamWriter outputStream = new OutputStreamWriter(process.getOutputStream());
            outputStream.write("/system/bin/screencap -p\n");
            outputStream.flush();
            Bitmap screen = BitmapFactory.decodeStream(process.getInputStream());
            outputStream.write("exit\n");
            outputStream.flush();
            outputStream.close();
            return screen;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

放置在代码中的任意位置以获取位图

BitmapScreencap.Get.Screen();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多