Showing camera images with OpenGL on Android example

Showing camera images with OpenGL on Android example

I fiddeled a small example together that shows how to get images from the camera and render them with OpenGL. The example is for Android phones and consists of three classes:

  • GLCamTest is the application’s main Activity. It does nothing special apart from putting the app in fullscreen mode and creating a GLLayer object as well as a Preview object.
  • The Preview class handles the camera. In particular, the method setPreviewCallback is used to receive camera images. The camera images are not processed in this class but delivered directly to the GLLayer. This class itself does not display the camera images.
  • GLLayer uses OpenGL ES to render the camera’s viewfinder image on the screen. Unfortunately I don’t know much about OpenGL (ES). The code is mostly copied from some examples. The only interesting stuff happens in the main loop (the run method) and the onPreviewFrame method.

Furthermore, we have the BooleanLock class which is completely boring. I uploaded the eclipse project containing the source code. I have only tested it on the emulator and with my tuned G1 not sure if it works with normal devices.

I just tested it on a normal G1. Performance is horrible; the Garbage Collector jumps in a few times per second and stops the video. It’s because of the Camera Preview Callback memory Issue. Unfortunately I assume that this can’t be changed without touching the firmware. I also uploaded the example to the Android Market.

9 Responses »

  1. Pingback: Camera image as an OpenGL texture on top of the native camera viewfinder — Things about me

  2. Niels, I am curious on the performance difference on a normal G1 and on your tuned G1. Could you tell me how you tuned your G1? Thanks.

  3. I can’t really tell how the frame rate changed but the most obvious difference was that on a normal G1 the graphic freezes for a short time every 1-2 seconds. However, my impression is that the behavior changed with Android 1.6. The fps is still higher on my device than on the average G1 but the graphic is much more fluid with 1.6.

    I changed two things in the firmware of my G1: Normally a new buffer is allocated by native code for each frame when the onPreviewFrame method is called. Instead I allocate a buffer once and reuse it for the following frames. In addition, the onPreviewFrame method is usually called by the Android message queue but I call it directly omitting this detour.

  4. Thank you for your comment, Niels. I tested with my G1 (ADP1) with a plane Android 1.6, and it seems just okay with just so-so frame rate, but without freezing severely, as you mentioned. :-)

  5. Yeah I guess you are right. I did the same on one of our G1s and was quite happy about the performance. However, after updating my own phone to 1.6 I had some issues with another test. I assume that the problem is that if your app uses a lot of CPU resources it performs very bad :-/

  6. Hi Niels,

    I have tested your application into my HTC Magic (firm version 1.6) and it was broken just started…..

    Should I change anything into the code to it works properly?

    Thanks.

  7. Same as newer comment other demo, it should be possible to dma buffer the preview to the same layer as read to gl texture.. never worked last year. Also no work on moto droid. G1adp was slow refresh due to cam buffer load speed, this android mod needs replaced. Send email will reply with code libs.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>