2

The Media Projection package is new Lollipop, and allows an app to capture the device's screen in realtime for streaming to video. I was hoping this could also be used to capture a single still screenshot, but so far I have not been successful. Of course, the first frame of a captured video could work, but I'm aiming for a perfect, lossless screenshot matching the pixel resolution of the device. A still from a captured video cannot provide that.

I've tried a lot of things, but the closest I came to a solution was to first launch an invisible activity. This activity then follows the API example for starting screen capture, which can include asking the user's permission. Once screen capture is enabled, the screen image is live in a SurfaceView. However, I cannot find a way to capture a bitmap from the SurfaceView. There are lots of questions and discussions about this, but no solutions seem to work, and there is some evidence that it is impossible.

Any ideas?

4

1 回答 1

3

您无法捕获 SurfaceView 的内容。

您可以做的是将 SurfaceView 替换为具有进程内使用者的 Surface 对象,例如SurfaceTexture。在从问题链接的 android-ScreenCapture 示例中,mMediaProjection.createVirtualDisplay()希望 Surface 向其发送图像。如果您创建 SurfaceTexture 并使用它来构造 Surface,则 MediaProjection 生成的图像将可从 OpenGL ES 纹理中获得。

如果 GLES 不适合您,则可以使用ImageReader类。它还提供了一个 Surface 可以传递给createVirtualDisplay(),但更容易从软件访问像素。

于 2015-05-07T17:25:46.963 回答