Advanced control of the Camera using PhoneGap

(Guest post from Mike Burgher – thanks!)

I’ll share my experience using the Android camera; hope this doesn’t cause confusion. I wrote an NSBasic app that uses the camera to do motion detection on the area. It does time-lapse, auto-shoot the camera, and compares portions of the images over time. You use it, for example, if you are in a sketchy hotel room and you want some peace of mind. While you sleep, it will detect if the door starts to open, and alarms loudly and summons help.

It’s all NSBasic except the “getpicture” camera operation, which is a custom PhoneGap plugin. I know of no way to do this whole thing from JavaScript. The Android camera API sequence is non-trivial, but there is good documentation and code examples.

In a plugin, it’s fairly easy to select which camera, set the parameters, shoot the picture when the camera is ready, and save it.

The camera preview is more of an issue. The Android API sequence requires you to display a preview on the screen before the camera is ready to take a picture. For many applications, you don’t want to see that preview; you want to see your app. There are several approaches to hiding that preview, from making it ultra small (1px by 1px) to making it transparent on >= Android 4.0. But there is no single solution that works on all Android cameras. And there are various undocumented limitations on cameras, dictated by the manufacturer.

And it’s hard to debug a camera app! Things will work on the device, that won’t work in the Android emulator, and vice versa.

I started with an existing custom PhoneGap plugin called Foreground Camera, who’s purpose was to keep your app from going to the background and getting killed off by Android when you called the standard “getpicture”. It was modified to select the camera, hide the preview, and take the picture without user intervention.

Unfortunately, I’ve been living in an ancient PhoneGap 2.5 world for a long time. I suddenly have to bring all my stuff up to the current PhoneGap level. But I’d be happy to share my mods to this plugin. It’s not 100% stable yet, and I’d welcome any improvements.

— Mike