Packageflash.media
Classpublic final class Camera
InheritanceCamera Inheritance EventDispatcher Inheritance Object

The Camera class is primarily for use with Macromedia Flash Media Server, but can be used in a limited way without the server.

The Camera class lets you capture video from a video camera attached to the computer that is running Macromedia Flash Player—for example, to monitor a video feed from a web camera attached to your local system. (Flash provides similar audio capabilities; for more information, see the Microphone class entry.)

Warning: When a SWF file tries to access the camera returned by getCamera(), Flash Player displays a dialog box that lets the user choose whether to allow or deny access to the camera. (Make sure your application window size is at least 215 x 138 pixels for the Camera class examples; this is the minimum size Flash requires to display the dialog box.) End users and administrative users may also disable camera access on a per-site or global basis.

To create or reference a Camera object, use the getCamera() method.

View the examples.

See also
flash.media.Microphone


Public Properties
Hide Inherited Public Properties
Show Inherited Public Properties
 PropertyDefined by
  activityLevel : Number
[read-only] A numeric value that specifies the amount of motion the camera is detecting.
Camera
  bandwidth : int
[read-only] An integer that specifies the maximum amount of bandwidth the current outgoing video feed can use, in bytes.
Camera
 Inheritedconstructor : Object
A reference to the class object or constructor function for a given object instance.
Object
  currentFPS : Number
[read-only] The rate at which the camera is capturing data, in frames per second.
Camera
  fps : Number
[read-only] The maximum rate at which you want the camera to capture data, in frames per second.
Camera
  height : int
[read-only] The current capture height, in pixels.
Camera
  index : int
[read-only] A zero-based integer that specifies the index of the camera, as reflected in the array returned by names.
Camera
  motionLevel : int
[read-only] A numeric value that specifies the amount of motion required to invoke the activity event.
Camera
  motionTimeout : int
[read-only] The number of milliseconds between the time the camera stops detecting motion and the time the activity event is invoked.
Camera
  muted : Boolean
[read-only] A Boolean value that specifies whether the user has denied access to the camera (true) or allowed access (false ) in the Flash Player Privacy panel.
Camera
  name : String
[read-only] A string that specifies the name of the current camera, as returned by the camera hardware.
Camera
  names : Array
[static][read-only] Retrieves an array of strings reflecting the names of all available cameras without displaying the Flash Player Privacy panel.
Camera
 Inheritedprototype : Object
[static] A reference to the prototype object of a class or function object.
Object
  quality : int
[read-only] An integer specifying the required level of picture quality, as determined by the amount of compression being applied to each video frame.
Camera
  width : int
[read-only] The current capture width, in pixels.
Camera
Events
 EventSummaryDefined by
   Flash Player dispatches the activity event when a device, such as a camera or microphone, begins or ends a session.Camera
   An info object with a code property of "Camera.muted" indicates that the user has refused to allow the SWF file access to the user's camera.Camera
Public Methods
Hide Inherited Public Methods
Show Inherited Public Methods
 FunctionDefined by
 Inherited
addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
Registers an event listener object with an EventDispatcher object so that the listener receives notification of an event.
EventDispatcher
 Inherited
Dispatches an event into the event flow.
EventDispatcher
  
getCamera(name:String = null):Camera
[static] Returns a reference to a Camera object for capturing video.
Camera
 Inherited
Checks whether the EventDispatcher object has any listeners registered for a specific type of event.
EventDispatcher
 Inherited
Indicates whether an object has a specified property defined.
Object
 Inherited
Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter.
Object
 Inherited
Indicates whether the specified property exists and is enumerable.
Object
 Inherited
removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
Removes a listener from the EventDispatcher object.
EventDispatcher
  
setMode(width:int, height:int, fps:Number, favorArea:Boolean = true):void
Sets the camera capture mode to the native mode that best meets the specified requirements.
Camera
  
setMotionLevel(motionLevel:int, timeout:int = 2000):void
Specifies how much motion is required to dispatch the activity event.
Camera
 Inherited
Sets the availability of a dynamic property for loop operations.
Object
  
setQuality(bandwidth:int, quality:int):void
Sets the maximum amount of bandwidth per second or the required picture quality of the current outgoing video feed.
Camera
 Inherited
Returns the string representation of the specified object.
Object
 Inherited
Returns the primitive value of the specified object.
Object
 Inherited
Checks whether an event listener is registered with this EventDispatcher object or any of its ancestors for the specified event type.
EventDispatcher
Property detail
activityLevel property
activityLevel:Number  [read-only]

A numeric value that specifies the amount of motion the camera is detecting. Values range from 0 (no motion is being detected) to 100 (a large amount of motion is being detected). The value of this property can help you determine if you need to pass a setting to setMotionLevel().

If the camera is available but is not yet being used because Video.attachCamera() has not been called, this property is set to -1.

If you are streaming only uncompressed local video, this property is set only if you have assigned a function to the event handler. Otherwise, it is undefined.



Implementation
    public function get activityLevel():Number

See also
motionLevel, setMotionLevel()
bandwidth property
bandwidth:int  [read-only]

An integer that specifies the maximum amount of bandwidth the current outgoing video feed can use, in bytes. A value of 0 means that Flash video can use as much bandwidth as needed to maintain the desired frame quality.

To set this property, use setQuality().



Implementation
    public function get bandwidth():int

See also
setQuality()
currentFPS property
currentFPS:Number  [read-only]

The rate at which the camera is capturing data, in frames per second. This property cannot be set; however, you can use the setMode() method to set a related property—fps—which specifies the maximum frame rate at which you would like the camera to capture data.

Implementation
    public function get currentFPS():Number

See also
setMode()
fps property
fps:Number  [read-only]

The maximum rate at which you want the camera to capture data, in frames per second. The maximum rate possible depends on the capabilities of the camera; that is, if the camera doesn't support the value you set here, this frame rate will not be achieved.

Implementation
    public function get fps():Number

See also
currentFPS, setMode()
height property
height:int  [read-only]

The current capture height, in pixels. To set a value for this property, use setMode().

Implementation
    public function get height():int

See also
width, setMode()
index property
index:int  [read-only]

A zero-based integer that specifies the index of the camera, as reflected in the array returned by names.

Implementation
    public function get index():int

See also
names, getCamera()
motionLevel property
motionLevel:int  [read-only]

A numeric value that specifies the amount of motion required to invoke the activity event. Acceptable values range from 0 to 100. The default value is 50.

Video can be displayed regardless of the value of the motionLevel property. For more information, see setMotionLevel().



Implementation
    public function get motionLevel():int

See also
setMotionLevel()
motionTimeout property
motionTimeout:int  [read-only]

The number of milliseconds between the time the camera stops detecting motion and the time the activity event is invoked. The default value is 2000 (2 seconds).

To set this value, use setMotionLevel().



Implementation
    public function get motionTimeout():int

See also
setMotionLevel()
muted property
muted:Boolean  [read-only]

A Boolean value that specifies whether the user has denied access to the camera (true) or allowed access (false ) in the Flash Player Privacy panel.

Implementation
    public function get muted():Boolean

See also
getCamera()
name property
name:String  [read-only]

A string that specifies the name of the current camera, as returned by the camera hardware.

Implementation
    public function get name():String

See also
names, getCamera()
names property
names:Array  [read-only]

Retrieves an array of strings reflecting the names of all available cameras without displaying the Flash Player Privacy panel. This array behaves in the same way as any other ActionScript array, implicitly providing the zero-based index of each camera and the number of cameras on the system (by means of names.length). For more information, see the names Array class entry.

Calling the names property requires an extensive examination of the hardware, and it may take several seconds to build the array. In most cases, you can just use the default camera.



Implementation
    public static function get names():Array

See also
getCamera(), index, name
quality property
quality:int  [read-only]

An integer specifying the required level of picture quality, as determined by the amount of compression being applied to each video frame. Acceptable quality values range from 1 (lowest quality, maximum compression) to 100 (highest quality, no compression). The default value is 0, which means that picture quality can vary as needed to avoid exceeding available bandwidth.

Implementation
    public function get quality():int
width property
width:int  [read-only]

The current capture width, in pixels. To set a desired value for this property, use setMode().

Implementation
    public function get width():int

See also
setMode()
Event detail
activity event
Event object type: flash.events.ActivityEvent
ActivityEvent.type property = flash.events.ActivityEvent.ACTIVITY

Flash Player dispatches the activity event when a device, such as a camera or microphone, begins or ends a session.

This event has the following characteristics:

PropertiesValues
targetThe object beginning or ending a session, such as a Camera or Microphone object. The target is not always the object in the display list that registered the event listener. Use the currentTarget property to access the object in the display list that is currently processing the event.
bubblesNo.
currentTargetIndicates the object that is actively processing the Event object with an event listener.
cancelableNo. There is no default behavior to cancel.
status event
Event object type: flash.events.StatusEvent
StatusEvent.type property = flash.events.StatusEvent.STATUS

An info object with a code property of "Camera.muted" indicates that the user has refused to allow the SWF file access to the user's camera. An info object with a code property of "Camera.unmuted" indicates that the user has allowed the SWF file access to the user's camera.

Flash Player dispatches this event when a device, such as a camera or microphone, or an object such as a LocalConnection object reports its status.

This event has the following characteristics:

PropertiesValues
targetThe object reporting its status. The target is not always the object in the display list that registered the event listener. Use the currentTarget property to access the object in the display list that is currently processing the event.
bubblesNo.
currentTargetIndicates the object that is actively processing the Event object with an event listener.
cancelableNo. There is no default behavior to cancel.
Method detail
getCamera method

public static function getCamera(name:String = null):Camera

Returns a reference to a Camera object for capturing video. To actually begin capturing the video, you must attach the Camera object to a Video object (see Video.attachVideo() ).

Multiple calls to getCamera() reference the same camera. Thus, if your code contains code like firstCam:Camera = getCamera() and secondCam:Camera = getCamera(), both firstCam and secondCam reference the same camera, which is the user's default camera.

In general, you shouldn't pass a value for the name parameter; simply use getCamera() to return a reference to the default camera. By means of the Camera settings panel (discussed later in this section), the user can specify the default camera Flash should use.

When a SWF file tries to access the camera returned by getCamera(), Flash Player displays a dialog box that lets the user choose whether to allow or deny access to the camera. (Make sure your application window size is at least 215 x 138 pixels; this is the minimum size Flash requires to display the dialog box.) When the user responds to this dialog box, Flash Player returns an information object in the status event that indicates the user's response: Camera.muted indicates the user denied access to a camera; Camera.unmuted indicates the user allowed access to a camera. To determine whether the user has denied or allowed access to the camera without handling the status event, use the muted property.

The user can also specify permanent privacy settings for a particular domain by right-clicking (Windows) or Control-clicking (Macintosh) while a SWF file is playing, selecting Settings, opening the Privacy panel, and selecting Remember.

You can't use ActionScript to set the Allow or Deny value for a user, but you can display the Privacy panel for the user by calling Security.showSettings(SecurityPanel.PRIVACY). If the user selects Remember, Flash Player no longer asks the user whether to allow or deny SWF files from this domain access to your camera.

If getCamera returns null, either the camera is in use by another application, or there are no cameras installed on the system. To determine whether any cameras are installed, use names.length. To display the Flash Player Camera Settings panel, which lets the user choose the camera to be referenced by getCamera(), use System.showSettings(SecurityPanel.CAMERA).

Scanning the hardware for cameras takes time. When Flash finds at least one camera, the hardware is not scanned again for the lifetime of the player instance. However, if Flash doesn't find any cameras, it will scan each time getCamera is called. This is helpful if a user has forgotten to connect the camera; if your SWF file provides a Try Again button that calls getCamera, Flash can find the camera without the user having to restart the SWF file.

Parameters
name:String (default = null) — Specifies which camera to get, as determined from the array returned by the names property. For most applications, get the default camera by omitting this parameter.

Returns
Camera — If name is not specified, this method returns a reference to the default camera or, if it is in use by another application, to the first available camera. (If there is more than one camera installed, the user may specify the default camera in the Flash Player Camera Settings panel.) If no cameras are available or installed, the method returns null.

See also
index, muted, names, setMode(), Video.attachCamera()
setMode method

public function setMode(width:int, height:int, fps:Number, favorArea:Boolean = true):void

Sets the camera capture mode to the native mode that best meets the specified requirements. If the camera does not have a native mode that matches all the parameters you pass, Flash selects a capture mode that most closely synthesizes the requested mode. This manipulation may involve cropping the image and dropping frames.

By default, Flash drops frames as needed to maintain image size. To minimize the number of dropped frames, even if this means reducing the size of the image, pass false for the favorArea parameter.

When choosing a native mode, Flash tries to maintain the requested aspect ratio whenever possible. For example, if you issue the command myCam.setMode(400, 400, 30), and the maximum width and height values available on the camera are 320 and 288, Flash sets both the width and height at 288; by setting these properties to the same value, Flash maintains the 1:1 aspect ratio you requested.

To determine the values assigned to these properties after Flash selects the mode that most closely matches your requested values, use width, height, and fps.

Parameters
width:int — The requested capture width, in pixels. The default value is 160.
height:int — The requested capture height, in pixels. The default value is 120.
fps:Number — The requested rate at which the camera should capture data, in frames per second. The default value is 15.
favorArea:Boolean (default = true) — Specifies whether to manipulate the width, height, and frame rate if the camera does not have a native mode that meets the specified requirements. The default value is true, which means that maintaining capture size is favored; using this parameter selects the mode that most closely matches width and height values, even if doing so adversely affects performance by reducing the frame rate. To maximize frame rate at the expense of camera height and width, pass false for the favorArea parameter.

See also
fps, height, width
setMotionLevel method

public function setMotionLevel(motionLevel:int, timeout:int = 2000):void

Specifies how much motion is required to dispatch the activity event. Optionally sets the number of milliseconds that must elapse without activity before Flash considers motion to have stopped and dispatches the event.

Note: Video can be displayed regardless of the value of the motionLevel parameter. This parameter determines only when and under what circumstances the event is dispatched—not whether video is actually being captured or displayed.

This method is similar in purpose to Microphone.setSilenceLevel(); both methods are used to specify when the activity event should be dispatched. However, these methods have a significantly different impact on publishing streams:

Parameters
motionLevel:int — Specifies the amount of motion required to dispatch the activity event. Acceptable values range from 0 to 100. The default value is 50.
timeout:int (default = 2000) — Specifies how many milliseconds must elapse without activity before Flash considers activity to have stopped and dispatches the activity event. The default value is 2000 milliseconds (2 seconds).

See also
motionLevel, motionTimeout, setSilenceLevel()
setQuality method

public function setQuality(bandwidth:int, quality:int):void

Sets the maximum amount of bandwidth per second or the required picture quality of the current outgoing video feed. This method is generally applicable only if you are transmitting video using Flash Media Server.

Use this method to specify which element of the outgoing video feed is more important to your application—bandwidth use or picture quality.

Parameters
bandwidth:int — Specifies the maximum amount of bandwidth that the current outgoing video feed can use, in bytes per second. To specify that Flash video can use as much bandwidth as needed to maintain the value of quality, pass 0 for bandwidth. The default value is 16384.
quality:int — An integer that specifies the required level of picture quality, as determined by the amount of compression being applied to each video frame. Acceptable values range from 1 (lowest quality, maximum compression) to 100 (highest quality, no compression). To specify that picture quality can vary as needed to avoid exceeding bandwidth, pass 0 for quality.

See also
getCamera(), quality
Class examples

The following example shows the image from a camera after acknowledging the security warning. The stage is set such that it can not be scaled and is aligned to the top-left of the player window. The activity event is dispatched at the start and end (if any) of the session and is captured by the activityHandler() method, which prints out information on the event.

Note: a camera must be attached to your computer for this example to work correctly.


package {
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.media.Camera;
    import flash.media.Video;
    import flash.events.*;

    public class CameraExample extends Sprite {
        private var video:Video;
        
        public function CameraExample() {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            
            var camera:Camera = Camera.getCamera();
            
            if(camera != null) {
                camera.addEventListener(ActivityEvent.ACTIVITY, activityHandler);
                video = new Video(camera.width * 2, camera.height * 2);
                video.attachCamera(camera);
                addChild(video);
            }
            else {
                trace("You need a camera.");
            }
        }
        
        private function activityHandler(event:ActivityEvent):void {
            trace("activityHandler: " + event);
        }
    }
}




 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/labs/flashauthoringpreview/flash/media/Camera.html