One of the draws of developing for mobile devices is the array of input and output hardware that is built into these small units. Being able to tap into cameras, speakers, and microphones programmatically within our applications is not only desired functionality - but is something that is expected and necessary for many mobile applications. With AIR 3, developers receive a lot more control over these bits of hardware than in previous versions of the mobile runtime.
Previously available on iOS and TabletOS platforms, the ability to access a front-facing device camera has now been extended to Android.
In order to differentiate between different cameras on a device, we can check the new position
property of the flash.media.Camera
class. There is also a new flash.media.CameraPosition
class which specifies a set of constants that can be used when determining the current camera position.
CameraPosition.FRONT = front-facing device camera |
CameraPosition.BACK = back-facing device camera |
CameraPosition.UNKNOWN = indeterminate device camera |
In the following example, we determine how many cameras are being reported on a device, and subsequently loop through each Camera
object, testing against the position
property to determine which one is the front-facing device camera. When the desired camera is located, we use that to draw to the screen.
package { import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.media.Camera; import flash.media.CameraPosition; import flash.media.Video; [SWF(backgroundColor="#CCCCCC")] public class FrontCamera extends Sprite { private var video:Video; private var camera:Camera; public function FrontCamera() { super(); stage.scaleMode = StageScaleMode.NO_SCALE; stage.align = StageAlign.TOP_LEFT; if(Camera.isSupported){ setupCamera(); } } private function setupCamera():void { for(var i:int=0; i<Camera.names.length; ++i) { camera = Camera.getCamera(String(i)); if (camera.position == CameraPosition.FRONT){ setupVideo(); break; } } } private function setupVideo():void { video = new Video(stage.stageWidth, stage.stageHeight); video.attachCamera(camera); addChild(video); } } }
The front-facing camera will most likely be used to capture the user’s face as seen in Figure 6-1.
This is useful when a particular camera is needed within an AIR application, specifically in terms of video chat applications.
Be sure to provide proper permissions for the camera in your application descriptor file when targeting Android:
<uses-permission android:name="android.permission.CAMERA"/>