3

Am working in H264 video rendering in Android application using SurfaceView. It has one feature to take snapshot while rendering the video on surface view. Whenever I take a snapshot, I get the Transparent/Black screen only. I use getDrawingCache() method to capture the screen that returns a null value only. I use the below code to capture the screen.

SurfaceView mSUrfaceView = new SurfaceView(this); //Member variable

if(mSUrfaceView!=null)
  mSUrfaceView.setDrawingCacheEnabled(true); // After video render on surfaceview i enable the drawing cache

Bitmap bm = mSUrfaceView.getDrawingCache(); // return null  
user692942
  • 16,398
  • 7
  • 76
  • 175
marimuthu
  • 33
  • 1
  • 4

1 Answers1

7

Unless you're rendering H.264 video frames in software with Canvas onto a View, the drawing-cache approach won't work (see e.g. this answer).

You cannot read pixels from the Surface part of the SurfaceView. The basic problem is that a Surface is a queue of buffers with a producer-consumer interface, and your app is on the producer side. The consumer, usually the system compositor (SurfaceFlinger), is able to capture a screen shot because it's on the other end of the pipe.

To grab snapshots while rendering video you can render video frames to a SurfaceTexture, which provides both producer and consumer within your app process. You can then render the texture for display with GLES, optionally grabbing pixels with glReadPixels() for the snapshot.

The Grafika app demonstrates various pieces, though none of the activities specifically solves your problem. For example, "continuous capture" directs the camera preview to a SurfaceTexture and then renders it twice (once for display, once for video encoding), which is similar to what you want to do. The GLES utility classes include a saveFrame() function that shows how to use glReadPixels() to create a bitmap.

See also the Android System-Level Graphics Architecture document.

Community
  • 1
  • 1
fadden
  • 51,356
  • 5
  • 116
  • 166
  • While rendering the video in textureview we have options called "Pause" and "PinchZoom".When the video is paused and zoom the video the view is extend upto the limit but image is not extend.I am tested the same functionality in Nexus,Samsung s4 and MicroMax.It is working fine except these model SamSung S2 and S3 both the devices running in jellybean 4.1.2 version. – marimuthu Aug 03 '15 at 05:49
  • While rendering the video in Textureview we have option is called "Snapshot".Whenever I take a snapshot, glReadPixels() return 0 in lollipop 5.0 version. – marimuthu Aug 03 '15 at 07:04
  • TextureView has http://developer.android.com/reference/android/view/TextureView.html#getBitmap(android.graphics.Bitmap), which gives you a copy of the contents in a Bitmap. Use that, not `glReadPixels()`. – fadden Aug 03 '15 at 16:05