Working with autofocus camera using Adobe AIR on Android

Autofocus With Adobe AIR

Last week i have been getting some insights on using Adobe AIR on Android. While most of the experiences  were pleasant, there were times when i wished things could have been simpler.But then again there is a long way to go for both Adobe AIR and me on mobile devices.

In this article we will see how to deal with a mobile device camera’s autofocus feature using actionscript 3.
While on traditional web/desktop apps we haven’t worried ourselves much with this, on smartphone this is a rather basic feature that you come across. The last week i was very much amazed with the autofocus camera on my HTC Sensation , and how it would impact the development of video apps using AIR. For those who dont require their own camera interface, the android CameraRoll api is good enough.

However for those of you who need complete control, there is the raw camera access, using the Camera.getCamera() method.I will recommend you downloading the Tour de Flex Mobile app from android marketplace and exploring it. So now back to the topic….. i noticed that as many times as i reloaded the Tour de flex raw camera example on the device the focusing was set differently. so as a experiment i just created this little example which acquires the camera at first launch. Henceforth for getting the lens to change focus, simple re-request the camera. This causes the camera hardware to refocus the lens automatically. Since autofocus is a hardware feature as far as i understand you should not make the Camera.getCamera() request rapidly.

Below you will find the code snippet along with a link to download the entire project files (fla,apk).

Note: Device orientation is not accounted for in this example !


import flash.display.MovieClip;
import flash.utils.getTimer;

var camera:Camera;
var video:Video;
var screen:MovieClip;
var focusSound:Sound;
var lastFocus:Number = 0;


function init()
// init the movie clip that holds the video object
screen = this.screen;
screen.mouseChildren = false;
screen.doubleClickEnabled = true;
screen.addEventListener(MouseEvent.DOUBLE_CLICK, onDoubleTap);

// the video object which actually reenders the camera
video = screen.videoDisplay;

// a fancy camera lens sound
focusSound = new Lens();

// acquire camera

function acquireCamera()
camera = Camera.getCamera();
/**** EVENT HANDLERS **************/

function onDoubleTap(me:MouseEvent)
// check that refocus is requested not before 6 seconds of previous request
if(getTimer() - lastFocus < 6000)return;

// play lens sound for fun;

// acquire the camera

// store last focus request time
lastFocus = getTimer();

Download Autofocus example

1 Comment - Leave a comment
  1. Paul says:

    Hi Rajdeep.

    This is AWSOME. I am still experimenting but It would seem this would solve the autofocus issue reported here;

    I will keep playing with it to see exactly what its limitations and power might be.

    Thanks for posting and for your work.


Leave a Reply

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