首页 > 解决方案 > Android屏幕截图没有捕获所有内容

问题描述

我正在使用HelloAR 演示应用程序,我正在尝试截取整个屏幕(菜单栏和所有内容)的屏幕截图。我可以截取屏幕截图,但并非所有内容都被捕获... 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 元素、菜单栏等)?

标签: androidscreenshotarcorescreen-capture

解决方案


您可以MediaProjection用来捕获屏幕内容。这具有向用户请求屏幕共享权限的缺点。(通过示例参考 SO 答案

或者,您可以获取GLSurfaceViewusing glReadPixelsfn 的内容并将现有的 View 图像叠加在其上。(通过示例参考 SO 答案


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

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

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


推荐阅读