Search code examples
androidandroid-cameraandroid-service

Camera frames in a service / background process


For scientific measurements, I would like to access the camera frames from a service. The idea is to analyze the images coming from the camera in real-time under some conditions. Since the phone might be restarted or simply locked, it should be possible to start the process from a service.

For now, I was satisfied by using a Camera.Callback and the onPreviewFrame callback, but it appears that it only works if my application is running in foreground. More precisely, the camera requires a valid SurfaceView in order to call the onPreviewFrame function. And a SurfaceView created in an Activity is destroyed when the activity is minimized / finished.

I just cannot find a way to get the frames from a background process. Actually, it works on a Galaxy Note 10.1, but the Galaxy S4 requires a valid SurfaceView.

Is there a way to achieve this?

There are many topics about this on StackOverflow, but none worked for me.


Solution

  • You can send the preview to a SurfaceTexture instead (setPreviewTexture()). This won't disappear when the app is paused. Requires API 11+.

    You can see various applications of this technique in Grafika, which is generally doing video or GLES manipulation rather than stills, but the idea is similar.