Android屏幕截图无法捕获所有内容

问题描述

我正在使用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();
    }
}

这是屏幕截图:

enter image description here

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

解决方法

您可以使用MediaProjection来捕获屏幕内容。 这样做的缺点是要求用户提供屏幕共享权限。 (Reference SO answer with example

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


您当前的实现无效的原因是

SurfaceViewGLSurfaceView在窗口上打孔以显示其表面(Android src reference)。换句话说,它们具有透明区域。 因此,您无法通过调用GLSurfaceView.getDrawingCache()来捕获图像。

如果要从GLSurfaceView获取图像,则应在glReadPixels()中调用GLSurfaceView.onDrawFrame()

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...