Ok so lets save this image then….
There are a few more things we need to add to our little application file.
We need to import a jpeg encoder and a few filesystem classes. So underneath the other importing statements we add the following code:
import mx.graphics.codec.JPEGEncoder; import flash.filesystem.File; import flash.filesystem.FileMode; import flash.filesystem.FileStream;
We also need to add a call to a new function to save the image. We will add this to the takeSnapshot function. The take snapshot function will now look like this:
public function takeSnapshot():void{ var snapshotHolder:UIComponent = new UIComponent(); var snapshot:BitmapData = new BitmapData(160,120, true); var snapshotbitmap:Bitmap = new Bitmap(snapshot); snapshotHolder.addChild(snapshotbitmap); cnvSnapshot.addChild(snapshotHolder); snapshot.draw(video); saveImg(snapshot); }
Finally we add the saveImg() function.
private function saveImg( imgData:BitmapData ):void{ /* Create the necessary objects */ var imgEncoder:JPEGEncoder = new JPEGEncoder( 99 ); var fileStream:FileStream = new FileStream(); var myDate:Date = new Date(); /* Create the unique file name */ var thisFileName:String = “webcam_”+ myDate.getTime() +”.jpg”; /* Create the a file object for the new file */ var theFile:File = File.desktopDirectory.resolve( thisFileName ); /* Get the image data and store it in a byte array */ var iBytesA:ByteArray = imgEncoder.encode(imgData) ; /* Open the file and write the data to the file */ fileStream.openAsync(theFile, FileMode.WRITE); fileStream.writeBytes( iBytesA ); /* Close the file */ fileStream.close(); } There you have it. Now when you click the snapshot button you should get an image file created on the desktop.
Here is a link to the complete code.
Next on our to do list is to add a timer function to periodically take pictures…
|