问题描述
我正在使用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元素,菜单栏等)?
解决方法
您可以使用MediaProjection
来捕获屏幕内容。
这样做的缺点是要求用户提供屏幕共享权限。
(Reference SO answer with example)
或者,您可以使用GLSurfaceView
fn获取glReadPixels
的内容,并将现有的View图像叠加在其上。
(Reference SO answer with example)
您当前的实现无效的原因是
SurfaceView
和GLSurfaceView
在窗口上打孔以显示其表面(Android src reference)。换句话说,它们具有透明区域。
因此,您无法通过调用GLSurfaceView.getDrawingCache()
来捕获图像。
如果要从GLSurfaceView获取图像,则应在glReadPixels()
中调用GLSurfaceView.onDrawFrame()
。