Monday, October 11, 2010

Capture And Save An Image with A Webcam

Flex Builder 4 Flash Video Flash Video Flash Video


Webcams can serve as a camera that you can use with ActionScript. The Camera class is built into ActionScript allowing you to build applications that include webcams. Setting up a webcam in Flash is easy. You can use the webcam in a project to streaming video for video chat (with a media server account),or use as a tool to take a photo for members of your site.

Record and Save The Image

  1. Get and install the ActionScript Core Library
  2. Import the JGPEncoder
  3. Create 2 MC and name capture_mc and save_mc
  4. After entering the code change the X & Y values to place the 2 cameras where you want.
  5. Use the FileReference.save() method and pass in the ByteArray and the file name.
  6. Test the movie and activate the camera.

The Code

import flash.display.Bitmap;
import flash.display.BitmapData;
import com.adobe.images.JPGEncoder;
import flash.net.FileReference;

//Set the quality of the bitmap
var quality:int = 90;
var bandwidth:int = 0;
var cam:Camera = Camera.getCamera();

//camOn will be used later to turn off the webcam
var camOn:Boolean = false;

cam.setQuality(bandwidth, quality);
var video:Video = new Video(241,185);
video.attachCamera(cam);
video.x = 30;
video.y = 50;
addChild(video);

var bitmapData:BitmapData = new BitmapData(video.width,video.height);

var bitmap:Bitmap = new Bitmap(bitmapData);
bitmap.x = 285;
bitmap.y = 50;
addChild(bitmap);

//Button functions
capture_mc.buttonMode = true;
capture_mc.addEventListener(MouseEvent.CLICK,captureImage);

save_mc.buttonMode = true;
save_mc.addEventListener(MouseEvent.CLICK,saveImage);

function captureImage(e:MouseEvent):void {
bitmapData.draw(video);
}

var i:Number=1;
var fileRef:FileReference = new FileReference();

function saveImage(e:MouseEvent):void{
var encoder:JPGEncoder = new JPGEncoder();
var ba:ByteArray = encoder.encode(bitmapData);
fileRef.save(ba,"capture"+i+".jpg");
i++;
}

//Turn the camera on and off
stop_btn.addEventListener(MouseEvent.CLICK, movieStop);

function movieStop(e:Event) {
if (camOn){
video.attachCamera(null);
} else {
video.attachCamera(cam);
}

camOn = !camOn;

}

 

ActionScript 3.0 Reference

Camera — final class, package flash.media
Use the Camera class to capture video from the client system's camera.

CAMERA
— Constant Static Property, class flash.system.SecurityPanelWhen passed to Security.showSettings(), displays the Camera panel in Flash Player Settings.

CameraRoll — class, package flash.mediaThe CameraRoll class allows you to save image data to a system's "camera roll." AIR profile support: This feature is supported on mobile devices, but it is not supported on desktop operating systems.

2 comments:

  1. Hi, this tut is great, thanks!

    Have you thought of storing it locally using PHP and maybe capture a movie ?

    That would we awesome!

    Cheers,

    ReplyDelete
  2. i have downloaded the action script 3 core library..but can u tell me where shud i save this library to?
    coz am getting error 5001 = that name ofpackage 'com.adobe.images' doesn't reflect the location of file
    Thank you

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...