Possible to draw on SurfaceTexture attached to cam

2019-03-22 04:45发布

问题:

I want to draw on top of a SurfaceTexture that is connected to a Camera via setPreviewTexture. I can get the SurfaceTexture by a SurfaceTextureListener. But if I just try to use eglCreateWindowSurface & eglMakeCurrent it fails due to the surface already being connected.

1) Is it possible to draw on this surface at all? Or does binding it to the camera make it impossible to do that.

2) If it isn't possible. Can the surface be used (or copied) quickly to an OpenGL texture and used in a separate surface? If so how?

3) If not, what would be the fastest way to get the camera preview out of a SurfaceTexture and into OpenGL?

Also note, the code that gave me the failure regarding already being connected was inspired by Romain Guy's post here: http://groups.google.com/group/android-developers/browse_thread/thread/539457146a401cf1

回答1:

Yes this is possible, indeed the whole point of a SurfaceTexture. The problem was that I was thinking about them backward. In order to get them to work you need to:

1) Create a texture through OpenGL.
2) Pass this texture to the constructor of new SurfaceTexture.
3) Give this new SurfaceTexture to the camera.
4) Make sure you are using OES_External (see documentation for
details).

My biggest problem was trying to figure out how to do the reverse. That is, create a SurfaceTexture and pass it into OpenGL. But the opposite is the correct way to do it.