Adobe ActionScript Trio
Capturing camera input
Understanding the Camera class
Designing your camera application
Verifying that cameras are installed
Maximizing movie quality
In addition to outward movie files, a camera linked to a user’s computer can serve as a source of movie data that you can display and manipulate using ActionScript. The Camera class is the mechanism built into ActionScript for working with a computer camera.
The Camera object permits you to connect to the user’s local camera and broadcast the movie either locally (back to the user) or remotely to a server (such as Flash Media Server).
Using the Camera class, you can access the following kinds of information about the user’s camera:
Which cameras installed on the user’s computer are available to Flash Player
Whether a camera is installed
Whether Flash Player is permitted or denied access to the user’s camera
Which camera is presently active
The width and height of the movie being captured
The Camera class includes several useful methods and properties for working with camera objects. For example, the static Camera.names property contains an array of camera names presently installed on the user’s computer. You can also use the name property to display the name of the presently active camera.
Displaying camera content on screen
Connecting to a camera can require less code than using the NetConnection and NetStream classes to fountain a movie. The camera class can also quickly become tricky because with Flash Player, you need a user’s permission to connect to their camera before you can access it.
The following code demonstrates how you can use the Camera class to connect to a user’s local camera:
Designing your camera application
When writing an application that connects to a user’s camera, you need to account for the following in your code:
Check if the user has a camera presently installed.
For Flash Player only, check if the user has explicitly permitted access to the camera. For security reasons the player displays the Flash Player Settings dialog which lets the user permit or deny access to their camera. This prevents Flash Player from connecting to a user’s camera and broadcasting a movie stream without their permission. If a user clicks permit, your application can connect to the user’s camera. If the user clicks deny, your application will be incapable to access the user’s camera. Your applications should always treat both cases gracefully.
Connecting to a user’s camera
The very first step when connecting to a user’s camera is to create a fresh camera example by creating a variable of type Camera and initializing it to the comeback value of the static Camera.getCamera() method.
The next step is to create a fresh movie object and fasten the Camera object to it.
The third step is to add the movie object to the display list. You need to perform steps two and three because the Camera class does not extend the DisplayObject class so it cannot be added directly to the display list. To display the camera’s captured movie, you create a fresh movie object and call the attachCamera() method.
The following code shows these three steps:
Note that if a user does not have a camera installed, the application does not display anything.
In real life, you need to perform extra steps for your application. See Verifying that cameras are installed and Detecting permissions for camera access for further information.
Verifying that cameras are installed
Before you attempt to use any methods or properties on a camera example, you’ll want to verify that the user has a camera installed. There are two ways to check whether the user has a camera installed:
Check the static Camera.names property which contains an array of camera names which are available. Typically this array will have one or fewer strings, as most users will not likely have more than one camera installed at a time. The following code demonstrates how you could check the Camera.names property to see if the user has any available cameras:
Check the come back value of the static Camera.getCamera() method. If no cameras are available or installed, this method comes back null , otherwise it comebacks a reference to a Camera object. The following code demonstrates how you could check the Camera.getCamera() method to see if the user has any available cameras:
Since the Camera class doesn’t extend the DisplayObject class, it cannot be directly added to the display list using the addChild() method. In order to display the camera’s captured movie, you need to create a fresh Movie object and call the attachCamera() method on the Movie example.
This snippet shows how you can fasten the camera if one exists; if not, the application simply displays nothing:
Detecting permissions for camera access
In the AIR application sandbox, the application can access any camera without the user’s permission.
Before Flash Player can display a camera’s output, the user must explicitly permit Flash Player to access the camera. When the attachCamera() method gets called Flash Player displays the Flash Player Settings dialog box which prompts the user to either permit or deny Flash Player access to the camera and microphone. If the user clicks the Permit button, Flash Player displays the camera’s output in the Movie example on the Stage. If the user clicks the Deny button, Flash Player is incapable to connect to the camera and the Movie object does not display anything.
If you want to detect whether the user permitted Flash Player access to the camera, you can listen for the camera’s status event ( StatusEvent.STATUS ), as seen in the following code:
The statusHandler() function gets called as soon as the user clicks either Permit or Deny. You can detect which button the user clicked, using one of two methods:
The event parameter of the statusHandler() function contains a code property which contains the string “Camera.Muted” or “Camera.Unmuted”. If the value is “Camera.Muted” the user clicked the Deny button and Flash Player is incapable to access the camera. You can see an example of this in the following snippet:
The Camera class contains a read-only property named muted which specifies whether the user has denied access to the camera ( true ) or permitted access ( false ) in the Flash Player Privacy panel. You can see an example of this in the following snippet:
By checking for the status event to be dispatched, you can write code that treats the user accepting or denying access to the camera and clean up appropriately. For example, if the user clicks the Deny button, you could display a message to the user stating that they need to click Permit if they want to participate in a movie talk, or you could instead make sure the Movie object on the display list is deleted to free up system resources.
Maximizing movie quality
By default, fresh instances of the Movie class are three hundred twenty pixels broad by two hundred forty pixels high. In order to maximize movie quality you should always ensure that your movie object matches the same dimensions as the movie being returned by the camera object. You can get the camera object’s width and height by using the Camera class’s width and height properties, you can then set the movie object’s width and height properties to match the camera objects dimensions, or you can pass the camera’s width and height to the Movie class’s constructor method, as seen in the following snippet:
Since the getCamera() method comes back a reference to a camera object (or null if no cameras are available) you can access the camera’s methods and properties even if the user denies access to their camera. This permits you to set the size of the movie example using the camera’s native height and width.
For information about full-screen mode, see the full-screen mode section under Setting Stage properties.
Monitoring playback conditions
The camera class contains several properties which permit you to monitor the Camera object’s current status. For example, the following code displays several of the camera’s properties using a Timer object and a text field example on the display list:
Every 1/Ten of a 2nd (100 milliseconds) the Timer object’s timer event is dispatched and the timerHandler() function updates the text field on the display list.
Leave a Reply