The following example shows how you can get the array of available cameras in ActionScript by using the static Camera.names property, which returns an array of camera names. You’ll also see how you can get a specific camera instance (in case users have multiple cameras installed, which may be the case if they have a laptop with a built in webcam as well as an external USB/Firewire webcam) by passing a parameter to the static Camera.getCamera() method.

This example won’t be very interesting unless you actually have a webcam installed.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/01/25/getting-specific-camera-instances-using-the-static-cameragetcamera-method/ -->
<mx:Application name="Camera_getCamera_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="horizontal"
        verticalAlign="top"
        backgroundColor="white"
        creationComplete="init();">
 
    <mx:Script>
        <![CDATA[
            import flash.media.Camera;
            import mx.controls.Alert;
            import mx.events.ListEvent;
            import mx.utils.ObjectUtil;
 
            private var camera:Camera;
 
            private function init():void {
                if (Camera.names.length == 0) {
                    Alert.show("You do not have any cameras attached.");
                    list.enabled = false;
                    textArea.enabled = false;
                }
            }
 
            private function list_change(evt:ListEvent):void {
                var tList:List = evt.currentTarget as List;
                var cameraName:String = tList.selectedIndex.toString();
                camera = Camera.getCamera(cameraName);
                textArea.text = ObjectUtil.toString(camera);
            }
        ]]>
    </mx:Script>
 
    <mx:Panel id="panel"
            title="Installed cameras:"
            status="{list.dataProvider.length} camera(s)">
        <mx:List id="list"
                dataProvider="{Camera.names}"
                width="200"
                change="list_change(event);" />
    </mx:Panel>
 
    <mx:TextArea id="textArea"
            editable="false"
            width="100%"
            height="100%" />
 
</mx:Application>

View source is enabled in the following example.

Note that since we aren’t actually accessing the camera’s video, we don’t get the Adobe Flash Player Settings dialog box asking is if we want to allow/deny access to the current Flex application, but we can still get basic camera information such as available camera names, and various properties of each camera.

 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

7 Responses to Getting specific camera instances using the static Camera.getCamera() method

  1. Ajitpal Singh says:

    it is a great example to clear fundamental!!!! i have one question how can we get color of object placed in front of camera. suppose i have red color paper make it in fornt of camera and flash application just make it swf in color red. i was trying bitmap.getpixel() at xy position

  2. caleb cohoon says:

    Thanks a lot for the code. It helped me a lot on my project! :)

  3. Fotos says:

    I didn’t know this was even possible. Thanks for posting!

  4. Ed Hardy says:

    wow, you made my day! thank you very much! great job!

  5. wfg says:

    When using this code example in Snow Leopard, my iMac has an external web cam connected, yet I was only able to get a single camera listed in the Camera.names Array. I would like to see both the iSight camera and the external webcam listed, and allow my user to pick one from a list. The behavior seems to be only picking up one camera automatically by Adobe Air. Is this a bug in Air 2.0 Beta? Anyway to fix this?

  6. Wow says:

    Cool, but in my another computer , when getCamera(), the flashplayer will crash….
    when i disable my web camera, it works

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree