借助MediaProjection
Android L中提供的API,可以实现
将主屏幕的内容(默认显示)捕获到Surface对象中,然后您的应用可以通过网络发送该对象
我已经设法让VirtualDisplay
工作,我SurfaceView
正确显示屏幕的内容.
我想要做的是捕获显示在其中的框架Surface
,并将其打印到文件.我尝试过以下内容,但我得到的只是一个黑色文件:
Bitmap bitmap = Bitmap.createBitmap
(surfaceView.getWidth(), surfaceView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
surfaceView.draw(canvas);
printBitmapToFile(bitmap);
Run Code Online (Sandbox Code Playgroud)
有关如何从中检索显示数据的任何想法Surface
?
因此,作为@j__m建议,现在我的设置VirtualDisplay
使用Surface
的ImageReader
:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
displayWidth = size.x;
displayHeight = size.y;
imageReader = ImageReader.newInstance(displayWidth, displayHeight, ImageFormat.JPEG, 5);
Run Code Online (Sandbox Code Playgroud)
然后我创建虚拟显示传递Surface
到MediaProjection
:
int flags = DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY | DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
DisplayMetrics metrics = getResources().getDisplayMetrics();
int density = metrics.densityDpi;
mediaProjection.createVirtualDisplay("test", …
Run Code Online (Sandbox Code Playgroud) 我正在看这个课程:https://developer.android.com/reference/android/media/ImageReader.html
我想要的是从ImageReader类中获取一个InputStream,并获取正在渲染到当前表面的图像数据.Current Surface表示当前显示的android屏幕.我在AOSP代码中查看了一个示例,但实际上很难理解.如果有人为我提供了一个很好的ImageReader类示例,那将非常有用.