Search code examples
actionscript-3flashflash-cs5

How to show HD Webcam


I'm new to AS, so please write the code in an easy way to understand: I am making a webcam application in Flash CS5 (using ActionScript 3), and have searched the interweb finding either tutorials that don't work or show the webcam in the wrong size, which I would like to be 1280p. Any help would be appreciated.

Current Code:

var cam:Camera = Camera.getCamera(); 
var vid:Video = new Video(); 
vid.attachCamera(cam); 
addChild(vid);

Solution

  • let's import the things we need and define a few constants we need later on

    import flash.media.Camera;
    import flash.media.Video;
    
    const CAMERA_WIDTH:uint = 1280;
    const CAMERA_HEIGHT:uint = 720;
    const CAMERA_FPS:uint = 12;
    

    Now we look up what cameras we have connected to our System and trace them out, just as information

    // Let's take a look what Cameras are available
    var cams:Array = Camera.names;
    var camLength:uint = Camera.names.length;
    for (var i:uint = 0; i < camLength; ++i)
        trace(i, cams[i]);
    

    Now that we know which cameras are installed we select one, there are basically 2 ways

    // get systems standard camera device
    var camera:Camera = Camera.getCamera(); // if you don't put a parameter into the getCamera method, it automatically takes the systems standard
    

    Or take a specific camera which we read out before (oddly this wants a string as parameter instead of a number)

    var camera:Camera = Camera.getCamera("0"); // takes the first camera from the Camera.names array
    

    to set the resolution of the selected camera you call the setMode method as seen here (width, height and frames per second), in this case we use our defined constants for that

    camera.setMode(CAMERA_WIDTH, CAMERA_HEIGHT, CAMERA_FPS);
    

    to display your pretty face on the stage you need a video, following code should be selfexplanatory

    var video:Video = new Video(CAMERA_WIDTH, CAMERA_HEIGHT);
    video.attachCamera(camera);
    addChild(video);
    

    that's it