【问题标题】:Android screenshot not capturing everythingAndroid屏幕截图没有捕获所有内容
【发布时间】:2020-08-10 22:51:05
【问题描述】:

我正在使用HelloAR demo application,我正在尝试截取整个屏幕(菜单栏和所有内容)。我可以截取屏幕截图,但并非所有内容都被捕获... GUI 元素在屏幕截图中可见,但摄像头馈送不可见。

这是我的代码:

int counter = 0;
private void takeScreenShot()
{
    View view = getWindow().getDecorView().getRootView();
    Bitmap bmp = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
    PixelCopy.request(getWindow(), bmp, copyResult -> {
        if(copyResult == PixelCopy.SUCCESS){
            String filename = getExternalFilesDir(null) + File.separator + "SCREENSHOTS" + File.seperator + "SS_" + counter + ".png";
            store(bmp, filename);
            counter++;
        }
    }, new Handler());
}

public static void storeit(Bitmap bm, String fileName){
    File file = new File(fileName);
    try {
        FileOutputStream fOut = new FileOutputStream(file);
        bm.compress(Bitmap.CompressFormat.PNG, 85, fOut);
        fOut.flush();
        fOut.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

截图如下:

如何捕获整个屏幕(摄像头、UI 元素、菜单栏等)?

【问题讨论】:

    标签: android screenshot arcore screen-capture


    【解决方案1】:

    您可以使用MediaProjection 来捕获屏幕内容。 这具有向用户请求屏幕共享权限的缺点。 (Reference SO answer with example)

    或者,您可以使用glReadPixels fn 获取GLSurfaceView 的内容并将您现有的视图图像叠加在它上面。 (Reference SO answer with example)


    您当前的实现不起作用的原因是因为

    SurfaceViewGLSurfaceView 在它们的窗口上打孔以显示它们的表面 (Android src reference)。换句话说,它们具有透明区域。 所以你不能通过调用GLSurfaceView.getDrawingCache()来捕获图像。

    如果你想从 GLSurfaceView 中获取图像,你应该在 GLSurfaceView.onDrawFrame() 中调用 glReadPixels()

    【讨论】:

    • 谢谢,但知道为什么我采用的方法不起作用吗?为什么只捕获了部分内容?
    • 基本上,GLSurfaceView 不会绘制自己的内容,它实际上有一个由GLSurfaceView.Renderer 渲染的空白空间。看看我在编辑后的答案中分享的链接。
    • 谢谢,这更清楚了。但是,如果我捕获 GLSurfaceView,那么它会包括 UI 元素和顶部菜单栏吗?
    • 如果通过捕获,你的意思是 glReadPixels() ,那么不。你只会得到在 GLSurfaceView 中显示的内容,但你应该能够叠加你现有的图像(使用生成PixelCopy) 在表面视图图像上获取您需要的图像。
    • 叠加应该相当简单(假设宽高相同),新建一个位图,用Canvas.drawBitmap()在新位图中绘制旧图。 (Example)
    猜你喜欢
    • 2012-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-20
    • 2016-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多