Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
642 views
in Technique[技术] by (71.8m points)

android - Getting a frame from SurfaceView

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...