In this post I’ll show you an approach for setting up the camera in Android devices for image processing. More exactly, access camera hardware, create a surface for the preview, handle camera and preview callbacks, get image data as frames.
First thing to do, create a new Android project in Eclipse with whatever settings you like. I would recommend a minimum target of 8(2.2). Keep the default activity.
Next, declare the following permissions in your manifest
<uses-permission android:name="android.permission.CAMERA"/> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus"/>
- the autofocus is optional, but recommended.
Next, let’s create the needed classes first. Create a class derived from android.view.SurfaceView and implementing android.view.SurfaceHolder.Callback. This will be the container for the camera image frames.
Create a class derived from android.view.View – this will be drawn on top of the original preview(call it something along the lines of DrawOnTop).
Briefly, this is how everything will work:
- your main activity will instantiate two class members of type the above classes you’ve created and add it to its view
- the preview class will handle camera creation and release, camera callback and top view instatiation. It will set camera params to highest FPS and minimum resolution on surface creation, will allocate and release camera when appropriate . In the surfaceCreated overridden method is the callback which retrieves camera frames from preview.
- the top view class declared will be instantiated in the preview class, and in its onDraw method will handle image processing and optionally, display processed images over the original one. This will be called for every frame.
I won’t post more source code as you can get a working eclipse project from the link below.
For image processing function, I would suggest OpenCV library, an open source project which has an android port also. Very easy to pick it up, and fits well with the “camera framework” I presented you. Look for comments in the source code for the right place to insert image processing function(TopView class).
Take care with you processing functions as mobile devices are pretty limited in terms of hardware resources. It can get quickly slow, when processing real-time image frames, and you’ll lose frames.