Search code examples
androidmultithreadingopengl-escamera

Background OpenGL ES thread with camera preview on Android


Is there a way to have a background thread access the camera preview using an OpenGL ES texture?

It is perfectly fine to access the camera preview using OpenGL ES within a view that is visible to the user. I need to access the camera frame within a background thread in an OpenGL context. Download the resulting data using glReadPixels after transformed by a shader and do further processing in the CPU. The user view should not be affected.


Solution

  • The supported way to process frame after frame in the background is by using the onPreviewFrame() callback. Whatever you do there will not effect the user preview, be it surface or texture.

    If you want the onPreviewFrame() callbacks to arrive on a background thread, you should openCamera() on this thread, and it must be a Handler thread (i.e. to have its Looper), see Best use of HandlerThread over other similar classes