Live Camera Previews in Android

The Android SDK provided by Google doesn't provide any camera emulation beyond a stand-in animation (see below). Since Google haven't yet provided a working implementation (or even a date by which one will be available), I'm publishing the source code for a set of temporary classes that I wrote to overcome this limitation. It's the first code I wrote on the Android platform, and it was written in haste, though it has functioned very well for me.

The code is public domain. I make no warranty as to its fitness for any particular purpose.

The code consists of:


When drawing the output of the genuine camera to screen, you will see something that looks like this:

Device's Camera preview output.

When drawing the output of a remote camera that is obtaining frames from a running webcam broadcaster you would see this (if you were stood over my desk).

Still obtained from a webcam broadcaster.

To save myself more time, I chose to produce a small settings configuration activity too (the code for which is not included).

A simple configuration activity for the camera settings.


Using a camera source is very simple:

CameraSource cs = new RemoteCamera("", 9889, 320, 240); if (! { /* deal with failure to obtain camera */ } while(/*some condition*/) { cs.capture(canvas) //capture the frame onto the canvas } cs.close();

The RemoteCamera implementation relies on a WebcamBroadcaster to be serving webcam stills from the address & port specified in its constructor.

To run a WebcamBroadcaster you will need JMF installed and you may need to supply the library location to the JVM when you run the application (this depends on how you install it). For example, on my machine (currently Windows XP) I use:

java "-Djava.library.path=C:\Program Files\JMF2.1.1e\lib" WebcamBroadcaster

You can pass the following combination of parameters to the application, all of which are strictly positive integers (all dimensions in pixels):

Alternatively, you may provide no parameters, in which case the defaults are width: 320, height: 240 and port: 9889.