4

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!

Chen
  • 55
  • 1
  • 1
  • 3
  • What you are trying to do is explained very well here: http://stackoverflow.com/questions/27817577/android-take-screenshot-of-surface-view-shows-black-screen – Willis Apr 16 '15 at 15:14

1 Answers1

8

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.

Community
  • 1
  • 1
fadden
  • 51,356
  • 5
  • 116
  • 166
  • Thanks for the help. I used a TextureView and it worked. – Chen Apr 17 '15 at 01:52
  • could you put the example code that how you replace `TextureView` with `surfaceView` ? the `MediaCodec.configure` function only get `SurfaceObject` as input and I have no idea how and where could I replace it with `TextureView` – MDK Feb 22 '19 at 10:09