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 !
var lastFocus:Number = 0;
// init the movie clip that holds the video object
screen = this.screen;
screen.mouseChildren = false;
screen.doubleClickEnabled = true;
// the video object which actually reenders the camera
video = screen.videoDisplay;
// a fancy camera lens sound
focusSound = new Lens();
// acquire camera
camera = Camera.getCamera();
/**** EVENT HANDLERS **************/
// 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();