summaryrefslogtreecommitdiff
path: root/webcam/webcam/NeaveWebcam.as
diff options
context:
space:
mode:
Diffstat (limited to 'webcam/webcam/NeaveWebcam.as')
-rwxr-xr-xwebcam/webcam/NeaveWebcam.as196
1 files changed, 196 insertions, 0 deletions
diff --git a/webcam/webcam/NeaveWebcam.as b/webcam/webcam/NeaveWebcam.as
new file mode 100755
index 0000000..811446b
--- /dev/null
+++ b/webcam/webcam/NeaveWebcam.as
@@ -0,0 +1,196 @@
+/**
+ * Neave Webcam ...play with webcam effects
+ *
+ * Copyright (C) 2008 Paul Neave
+ * http://www.neave.com/
+ *
+ * @author Paul Neave
+ * @version 1.0.0
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation at http://www.gnu.org/licenses/gpl.html
+ */
+
+package com.neave.webcam
+{
+ import flash.display.*;
+ import flash.events.*;
+ import flash.media.*;
+ import flash.ui.*;
+ import com.neave.media.*;
+ import com.neave.webcam.effects.*;
+
+ public class NeaveWebcam extends Sprite
+ {
+ // Main variables
+ private var camera:Camera;
+ private var video:Video;
+ private var videoBitmap:Bitmap;
+ private var videoContainer:Sprite;
+ private var uiEnabled:Boolean;
+
+ /**
+ * Manages the webcam effects
+ */
+ public var effects:EffectsManager;
+
+ /**
+ * Creates a new instance of Neave Webcam
+ *
+ * @param camera The camera object to use to create the webcam effects
+ * @param interactive Enable mouse click or arrow keys to change the webcam effect
+ */
+ public function NeaveWebcam(camera:Camera, interactive:Boolean = true)
+ {
+ this.camera = camera;
+
+ initVideo();
+ initEffects();
+
+ this.interactive = interactive;
+ }
+
+ /**
+ * Sets up the main video object
+ */
+ private function initVideo():void
+ {
+ // Video dimensions must be 320x240 or higher
+ var w:int = camera.width < NeaveCamera.CAMERA_WIDTH ? NeaveCamera.CAMERA_WIDTH : camera.width;
+ var h:int = camera.height < NeaveCamera.CAMERA_HEIGHT ? NeaveCamera.CAMERA_HEIGHT : camera.height;
+
+ // Attach the camera object a video object
+ video = new Video(w, h);
+ video.attachCamera(camera);
+
+ // Create a bitmap object for the video effect, flipping to create a mirror image
+ videoBitmap = new Bitmap(new BitmapData(w, h, false, 0xFF000000), PixelSnapping.AUTO, false);
+ videoBitmap.scaleX = -1;
+ videoBitmap.x = w;
+
+ // Create a sprite to hold the bitmap
+ videoContainer = new Sprite();
+ videoContainer.addChild(videoBitmap);
+ addChild(videoContainer);
+ }
+
+ /**
+ * Sets up the webcam effects for the video object
+ */
+ private function initEffects():void
+ {
+ // Set up the effects manager for this video object
+ effects = new EffectsManager(video, videoBitmap.bitmapData);
+ paused = false;
+ }
+
+ /**
+ * Enable mouse click or arrow keys to change the webcam effect
+ */
+ public function set interactive(i:Boolean):void
+ {
+ uiEnabled = i;
+
+ if (uiEnabled)
+ {
+ // Arrow keys select previous or next webcam effect
+ addEventListener(KeyboardEvent.KEY_DOWN, keyDownListener);
+
+ // Clicking on the video selects the next webcam effect
+ videoContainer.addEventListener(MouseEvent.CLICK, videoClickListener);
+ videoContainer.buttonMode = true;
+ }
+ else
+ {
+ // Remove interactivity
+ removeEventListener(KeyboardEvent.KEY_DOWN, keyDownListener);
+ videoContainer.removeEventListener(MouseEvent.CLICK, videoClickListener);
+ videoContainer.buttonMode = false;
+ }
+ }
+
+ /**
+ * Moves to the next webcam effect on mouse click
+ */
+ private function videoClickListener(e:MouseEvent):void
+ {
+ if (paused || camera.muted || camera.width == 0) return;
+
+ effects.nextEffect();
+ }
+
+ /**
+ * Moves to the next or previous webcam effect when the arrow keys are pressed
+ */
+ private function keyDownListener(e:KeyboardEvent):void
+ {
+ if (paused || camera.muted || camera.width == 0) return;
+
+ switch (e.keyCode)
+ {
+ case Keyboard.LEFT:
+ effects.previousEffect();
+ break;
+
+ case Keyboard.RIGHT:
+ effects.nextEffect();
+ break;
+ }
+ }
+
+ /**
+ * The bitmap data containing the current webcam effect
+ */
+ public function get effectBitmap():BitmapData
+ {
+ return videoBitmap.bitmapData;
+ }
+
+ /**
+ * Pause or resume the current webcam effect
+ */
+ public function set paused(p:Boolean):void
+ {
+ if (p == paused) return;
+
+ // Pause or resume updating the current webcam effect every frame
+ if (p) removeEventListener(Event.ENTER_FRAME, update);
+ else addEventListener(Event.ENTER_FRAME, update);
+ }
+
+ /**
+ * Pause or resume the current webcam effect
+ */
+ public function get paused():Boolean
+ {
+ return !hasEventListener(Event.ENTER_FRAME);
+ }
+
+ /**
+ * Removes the webcam and all other referenced objects
+ */
+ public function destroy():void
+ {
+ interactive = false;
+ paused = true;
+ effects.destroy();
+ videoContainer.removeChild(videoBitmap);
+ videoBitmap.bitmapData.dispose();
+ videoBitmap.bitmapData = null;
+ videoBitmap = null;
+ removeChild(videoContainer);
+ videoContainer = null;
+ video = null;
+ camera = null;
+ }
+
+ /**
+ * Updates the current webcam effect
+ */
+ private function update(e:Event):void
+ {
+ effects.update();
+ }
+ }
+} \ No newline at end of file