Getting a frame from SurfaceView

…衆ロ難τιáo~ 提交于 2019-12-29 06:28:47

问题


The difficulty I am having now is to take a screenshot of the SurfaceView. Could anyone please help?

My team is doing an Android project where we use MediaCodec to decode the video feed and render the feed onto a Surface. The activity implements

SurfaceHolder.Callback

Then we created a SurfaceView and add the Callback:

mySurfaveView.getHolder().addCallback(this);

Then we start decode the video feed in (we have a method that does the decode)

surfaceChanged(SurfaceHolder holder, int format, int width, int height)

The video work fine, now I want to take screenshot of what gets rendered onto the SurfaceView. I have tried several ways to do this, but I didn't succeed. Following are what I have tried:

  1. I have tired getting rootView's drawingcache.
  2. Tried getting the frame directly from MediaCodec.
  3. Tried the draw() method to draw on a canvas.

As of now I am tring to create a custom SurfaceView and override the onDraw method there. However, I have never created a custom SurfaceView. Am I on the right direction?

Help please. Thank you!


回答1:


You can't capture a SurfaceView, as explained in this other question.

SurfaceViews will rarely want to override onDraw(). Generally the View part of the SurfaceView is a transparent hole.

For the specific case of MediaCodec output, you have a couple of choices. You can direct the output to a SurfaceTexture, and then render the video frame twice using OpenGL ES. Once to the SurfaceView for display, once to an off-screen buffer from which you can capture the pixels.

Another approach is to replace the SurfaceView with a TextureView, and then use the getBitmap() call to grab video frames.

Various examples can be found in Grafika.



来源:https://stackoverflow.com/questions/29678652/getting-a-frame-from-surfaceview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!