![]() |
![]() |
![]() |
![]() |
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
- Get and install the ActionScript Core Library
- Import the JGPEncoder
- Create 2 MC and name capture_mc and save_mc
- After entering the code change the X & Y values to place the 2 cameras where you want.
- Use the FileReference.save() method and pass in the ByteArray and the file name.
- 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.



