Prerequisite knowledge

Familiarity with ActionScript 3.


Additional Requirements

  • Android device

User level


You can use the Camera class to capture video from the camera on a mobile device. You can display the camera video using the Video class. This article covers:

  • How to create a camera object
  • How to declare Android permission to use device camera
  • How to attach the camera to a Video object

Creating a Camera object

Get a reference to the Camera object using the camera.getCamera() method.  The getCamera() method returns a Camera object if a camera is available and returns null otherwise.  On Android, only one camera is supported.

The code below creates a camera object. If the Camera object returns null, the code traces a message, "No camera is installed." 

private var camera:Camera = Camera.getCamera(); if (camera == null) { trace "No camera is installed."); } else { trace("Camera is installed."); camera.setMode( 800, 400, 15, true ); }

The default size of the camera output can be small. Call the setMode() method to set the desired video resolution.

The hardware camera maintains a natural landscape orientation. When you use the Camera class, set the landscape aspect ratio for your application as well. You can set the aspect ratio to landscape in the application descriptor. You can also set it at runtime using the stage aspectRatio property. Avoid enabling the autoOrients setting in the application descriptor. If enabled, the device switches to the portrait aspect ratio when the user rotates the phone.

Declaring Android permission to use device camera

To access device camera, provide the permission to use camera in Android Manifest. In the application descriptor, under <android>, add the following:

<manifestAdditions> <![CDATA[ <manifest> <uses-permission android:name="android.permission.CAMERA"/> </manifest> ]]> </manifestAdditions>

Attaching a Camera object to a Video object

The following method creates a Video object with the same width and height as of the device camera. To display video, attach the Camera object to the Video object. Then, add the Video object to the display list using the addChild() method.

private function connectCamera():void { var video:Video = new Video(camera.width, camera.height); video.x = 10; video.y = 10; video.attachCamera(camera); addChild(video); }

Where to go from here

In this article, you learned about creating a Camera object and providing permission to use the device camera. You also learned about attaching a Camera object to a Video object to capture a video input. Use the Camera and Video classes in AIR to build complex applications.

For more information, refer to the online documentation, ActionScript 3 API Reference for the Adobe Flash Platform.