Bits & Bytes

Posts Tagged ‘capture’

Capturing and Playing Video From a Camera in Actionscript 3.0

This program gives a simple demonstration of how to display video that is captured by a camera. If you have a camera attached to your computer (most laptops do), then you can click the box above to display the video capture from your camera. Note that you will need click the “Allow” button to see the video. To stop the video, refresh the page.

The code for this program is simple and is listed below. The first three lines display the message “Click To Play.” The next line, sets the function CaptureVideo() as a callback to respond to mouse clicks.

After that, we have the CaptureVideo() function, which attempts to get access to a camera and checks whether the attempt was successful. If there is a camera, a Video object is created, attached to the camera, and added to the stage for display.

// Output the initial instructions to user
var qInstructions:TextField = new TextField();
qInstructions.text = "Click To Play";
addChild(qInstructions);

stage.addEventListener(MouseEvent.CLICK, CaptureVideo);

function CaptureVideo(e:Event):void {
	var qCamera:Camera = Camera.getCamera();
	if (qCamera != null) {
		var qVideo:Video = new Video(320, 240);
		qVideo.attachCamera(qCamera);
		addChild(qVideo);
	}
}

Saving a Screen Capture to a JPEG Image File in Actionscript 3.0

The program below demostrates how to capture an image of the stage and save it as a jpg image. We have drawn a yellow circle give the image capture something more interesting to render. To save the jpg image, just left-click the box above and select a location to save the image in.

The program for this jpg image capture is shown below. Additionally, you will need the JPGEncoder file which you can get here: https://github.com/mikechambers/as3corelib. To use the source file, select Edit->Preferences->Actionscript->Actionscript 3.0 Settings . . . and add the folder where you put “mikechambers-as3corelib-release.93-8-g24c6c16\mikechambers-as3corelib-24c6c16\src” to the src paths. Then you should be able to compile and execute the Actionscript code.

In this program, we import the JPGEncoder class and then draw a filled yellow circle on the stage. Next, we register an event listener to call the function SaveJPG() to save the image when a left-click occurs; Actionscript requires that file-saving code be contained in a user-initiated function.

Inside the callback, we create a BitmapData object that is the size of our stage and draw the stage into it. Then we create the encoder and call encode() to put the jpg data into a ByteArray. Finally, we create a FileReference object and save the jpg data.

import com.adobe.images.JPGEncoder;

// Draw a circle on the staqe
graphics.beginFill(0xFFFF00);
graphics.drawCircle(160, 120, 100);
graphics.endFill();

stage.addEventListener(MouseEvent.CLICK, SaveJPG);

function SaveJPG(e:MouseEvent):void {
	// Store the image capture of stage in a BitmapData Object
	var qImageData:BitmapData = new BitmapData(320, 240);
	qImageData.draw(stage);
	
	// Set the encoding to high-quality
	var qEncoder:JPGEncoder = new JPGEncoder(100);
	var qBytes:ByteArray = qEncoder.encode(qImageData);
	
	var qFile:FileReference = new FileReference();
	qFile.save(qBytes, "YellowCircle.jpg");
}