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:
- I have tired getting rootView's drawingcache.
- Tried getting the frame directly from MediaCodec.
- 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!
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.