25
Jan
08

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

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.

Full code after the jump.

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

View MXML

<?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 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.


4 Responses to “Getting specific camera instances using the static Camera.getCamera() method”


  1. 1 Ajitpal Singh Jun 4th, 2008 at 4:56 am

    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. 2 caleb cohoon Jun 25th, 2008 at 8:49 pm

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

  3. 3 Fotos Jul 22nd, 2008 at 5:53 am

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

  4. 4 Ed Hardy Jul 23rd, 2008 at 1:29 am

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

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".