Skip to content

Instantly share code, notes, and snippets.

@claus
Created November 3, 2010 02:06
Show Gist options
  • Save claus/660702 to your computer and use it in GitHub Desktop.
Save claus/660702 to your computer and use it in GitHub Desktop.
// Records a local webcam feed and converts the recording to SWF.
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.Loader;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.media.Camera;
import flash.media.Video;
import flash.net.FileReference;
import flash.net.URLRequest;
import flash.utils.ByteArray;
import flash.utils.getTimer;
import fl.controls.Button;
import fl.controls.Label;
import com.codeazur.as3swf.SWF;
import com.codeazur.as3swf.data.consts.BitmapFormat;
import com.codeazur.as3swf.data.SWFFillStyle;
import com.codeazur.as3swf.data.SWFMatrix;
import com.codeazur.as3swf.data.SWFRectangle;
import com.codeazur.as3swf.data.SWFShapeWithStyle;
import com.codeazur.as3swf.data.SWFShapeRecordStyleChange;
import com.codeazur.as3swf.data.SWFShapeRecordStraightEdge;
import com.codeazur.as3swf.data.SWFShapeRecordEnd;
import com.codeazur.as3swf.tags.TagDefineBitsLossless;
import com.codeazur.as3swf.tags.TagDefineShape;
import com.codeazur.as3swf.tags.TagEnd;
import com.codeazur.as3swf.tags.TagFileAttributes;
import com.codeazur.as3swf.tags.TagPlaceObject2;
import com.codeazur.as3swf.tags.TagRemoveObject2;
import com.codeazur.as3swf.tags.TagSetBackgroundColor;
import com.codeazur.as3swf.tags.TagShowFrame;
const w:Number = 240;
const h:Number = 180;
const wt:Number = w * 20;
const ht:Number = h * 20;
var swf:SWF;
var ba:ByteArray;
var frames:uint = 0;
var cam:Camera = Camera.getCamera();
var video:Video = new Video(w, h);
video.attachCamera(cam);
video.x = 20;
video.y = 20;
addChild(video);
var bitmapData:BitmapData = new BitmapData(w, h, false);
var bitmap:Bitmap = new Bitmap(bitmapData);
bitmap.x = 20;
bitmap.y = h + 40;
addChild(bitmap);
var loader:Loader = new Loader();
loader.x = 20;
loader.y = h + 40;
addChild(loader);
var recordButton:Button = new Button();
recordButton.x = w + 40;
recordButton.y = 20;
recordButton.label = "Start";
recordButton.toggle = true;
recordButton.addEventListener(Event.CHANGE, function(e:Event):void {
if(recordButton.selected) {
recordingStart();
} else {
recordingStop();
}
});
addChild(recordButton);
var saveButton:Button = new Button();
saveButton.x = w + 40;
saveButton.y = h + 40;
saveButton.visible = false;
saveButton.label = "Save";
saveButton.addEventListener(MouseEvent.CLICK, function(e:Event):void {
var ref:FileReference = new FileReference();
ref.save(ba, new Date().time + ".swf")
});
addChild(saveButton);
var progressLabel:Label = new Label();
progressLabel.x = w + 40;
progressLabel.y = h + 40;
progressLabel.width = 175;
progressLabel.visible = false;
addChild(progressLabel);
function recordingStart():void
{
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
recordButton.label = "Stop";
saveButton.visible = false;
progressLabel.visible = true;
loader.visible = false;
frames = 0;
ba = new ByteArray();
swf = new SWF();
swf.frameSize.xmax = wt;
swf.frameSize.ymax = ht;
swf.compressed = true;
swf.version = 6;
swf.frameRate = stage.frameRate;
swf.tags.push(new TagFileAttributes());
swf.tags.push(new TagSetBackgroundColor());
}
function recordingStop():void
{
removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
recordButton.label = "Start";
saveButton.visible = true;
progressLabel.visible = false;
loader.visible = true;
swf.tags.push(new TagEnd());
swf.frameCount = frames;
swf.publish(ba);
loader.loadBytes(ba);
}
function enterFrameHandler(event:Event):void
{
if(++frames > stage.frameRate * 5) {
// auto-stop after 5 seconds
recordingStop();
recordButton.selected = false;
return;
}
progressLabel.text = frames + " frames recorded (" + Math.round(frames / stage.frameRate) + " sec)";
bitmapData.draw(video);
var bitmapId:uint = frames << 1;
var shapeId:uint = bitmapId + 1;
var bitsLossless:TagDefineBitsLossless = new TagDefineBitsLossless();
bitsLossless.characterId = bitmapId;
bitsLossless.bitmapFormat = BitmapFormat.BIT_24;
bitsLossless.bitmapWidth = w;
bitsLossless.bitmapHeight = h;
bitsLossless.zlibBitmapData.writeBytes(bitmapData.getPixels(new Rectangle(0, 0, w, h)));
bitsLossless.zlibBitmapData.compress();
swf.tags.push(bitsLossless);
var shape:TagDefineShape = new TagDefineShape();
shape.characterId = shapeId;
shape.shapeBounds = new SWFRectangle();
shape.shapeBounds.xmin = 0;
shape.shapeBounds.ymin = 0;
shape.shapeBounds.xmax = wt;
shape.shapeBounds.ymax = ht;
shape.shapes = new SWFShapeWithStyle();
var fillStyle:SWFFillStyle = new SWFFillStyle();
fillStyle.type = 0x42;
fillStyle.bitmapId = bitmapId;
fillStyle.bitmapMatrix = new SWFMatrix();
fillStyle.bitmapMatrix.scaleX = 20;
fillStyle.bitmapMatrix.scaleY = 20;
shape.shapes.initialFillStyles.push(fillStyle);
var styleChange:SWFShapeRecordStyleChange = new SWFShapeRecordStyleChange();
styleChange.stateFillStyle1 = true;
styleChange.fillStyle1 = 1;
shape.shapes.records.push(styleChange);
var edge:SWFShapeRecordStraightEdge = new SWFShapeRecordStraightEdge();
edge.generalLineFlag = false;
edge.vertLineFlag = false;
edge.deltaX = wt;
edge.deltaY = 0;
shape.shapes.records.push(edge);
edge = new SWFShapeRecordStraightEdge();
edge.generalLineFlag = false;
edge.vertLineFlag = true;
edge.deltaX = 0;
edge.deltaY = ht;
shape.shapes.records.push(edge);
edge = new SWFShapeRecordStraightEdge();
edge.generalLineFlag = false;
edge.vertLineFlag = false;
edge.deltaX = -wt;
edge.deltaY = 0;
shape.shapes.records.push(edge);
edge = new SWFShapeRecordStraightEdge();
edge.generalLineFlag = false;
edge.vertLineFlag = true;
edge.deltaX = 0;
edge.deltaY = -ht;
shape.shapes.records.push(edge);
shape.shapes.records.push(new SWFShapeRecordEnd());
swf.tags.push(shape);
var removeObject:TagRemoveObject2 = new TagRemoveObject2();
removeObject.depth = 1;
swf.tags.push(removeObject);
var placeObject:TagPlaceObject2 = new TagPlaceObject2();
placeObject.depth = 1;
placeObject.characterId = shapeId;
placeObject.hasCharacter = true;
placeObject.hasColorTransform = false;
swf.tags.push(placeObject);
swf.tags.push(new TagShowFrame());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment