Android: Render OpenGL on top of camera preview


I was searching for that today and thought I could write a short wrap-up of what I ended up with. Particularly helpful was the Android TranslucentGLSurface sample code and a thread in the Android Beginners group.

Keep reading below for the fully commented source code.

Android: Render OpenGL on top of camera preview

Let’s start with the onCreate method of the main activity code.

When I was writing this, I was wondering why I had to set the GL view first and then add the preview because actually I want to have the preview below the GL surface, so this looks a bit strange to me. I didn’t find an answer on that one yet, but it works that way.

The rendering is implemented in a separate class GLClearRenderer which is pretty much like the one used in the GLSurfaceView introduction. It’s trivial since I’ve used it only for testing purposes. A real application would most likely subclass GLSurfaceView, too, in order overwrite onTouchEvent etc.

If you comment out adding the camera view in onCreate, you will already be able to run the application and see the flashing screen.

Now for the last part, the camera access. As you’ve seen that earlier, the whole logic is implemented in CameraView. Thanks to Androids easy-to-use camera API, rendering the preview into a surface is straightforward.

If you like this article, please share a slice of your pie and click here:

Information and Links

Join the fray by commenting, tracking what others have to say, or linking to it from your blog.


Other Posts

Write a Comment

Take a moment to comment and tell me what you think. Some basic HTML is allowed for formatting.

Reader Comments

Be the first to leave a comment!