Drawing on Live Video in Flex - Snapshot issue - actionscript-3

I am working on a project, where I have to make an annotation video player!
Basically, I followed this tutorial[1] where the author presents how to draw(in this case just a rectangle) on the own web camera, using Flex.
Everything went great, but now I want to add a listener to get a snapshot of the image(what I draw + the web camera image) and then save it on my computer.
I have created the listener, but the problem is that this listener saves only the image from the web camera without my drawing(even though I added that draw to the camera).
private function save():void {
var bitmapData:BitmapData = new BitmapData(videoDisplay.width,videoDisplay.height);
bitmapData.draw(videoDisplay);
var ba:ByteArray = (new PNGEncoder()).encode(bitmapData);
(new FileReference()).save(ba, "doodle.png");
}
I don't have experience at all with Flex/Flash so maybe I did smth wrong.
Can you please help me?
[1]http://narinderkumar.wordpress.com/2012/02/16/drawing-on-live-video-in-flex/

this example works fine - i dont see the problem in that codesnippet of yours but maybe you are just drawing vis graphics on the video, in thatcase the graphics layer is behind the camera... hope hat helps ;)
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.media.Camera;
import flash.media.Video;
public class cameratest extends Sprite
{
private var holder:Sprite;
private var layover:Sprite;
private var display:Sprite;
public function cameratest()
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
var cam:Camera = Camera.getCamera();
var video:Video =new Video(cam.width, cam.height);
video.attachCamera(cam);
//holder hollds the video and on top the layover to draw on
holder = new Sprite();
stage.addChild(holder);
holder.addChild(video);
//layover to draw on
layover = new Sprite();
holder.addChild(layover);
//will show the snapshot
display = new Sprite();
display.x= 350;
stage.addChild(display);
//listener for onclick do snapshot
stage.addEventListener(MouseEvent.CLICK, drawAndSave);
}
private function drawAndSave(E:Event=null):void{
layover.graphics.beginFill(0xff0000,1);
layover.graphics.drawCircle(10,10,10);
var bmd:BitmapData=new BitmapData(holder.width,holder.height);
bmd.draw(holder);
var bmp:Bitmap = new Bitmap(bmd,'auto',true);
display.addChild(bmp);
}
}
}

Related

Move projectile to mouse-click position AS3

I have a cannon and cannonball. How do I make a cannonball to move in a line from cannon to the Mouse Click position and stop/disappear/activate Explode animation?
I've tried different solutions and none of them would seem to work for me so I cleared it a bit.
And yes, I know it's ugly.
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.display.MovieClip;
import flash.events.Event;
import flash.ui.Mouse;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.utils.Timer;
import flash.display.Sprite;
addEventListener(Event.ENTER_FRAME, enterFr);
function enterFr(e:Event)
{
aims.x = mouseX;
aims.y = mouseY;
}
Mouse.hide();
zamok.addEventListener(MouseEvent.CLICK, fire);
function fire(m:MouseEvent)
{
var s:Sound = new cannonFire();
s.play();
var explo:boom = new boom();
explo.x = mouseX;
explo.y = mouseY;
addChild(explo);
}
Well you should think of the process that you want to implement.
First of all it is not instant, it takes some time for cannonball to move to the point where mouse clicked.
Let's start with some function that will create a cannonball:
private function fireCannonBall(target: Point):void
{
const cannonBall:CannonBall = new CannonBall(); // you need to implement this class, or just use some MovieClip from library;
cannonBall.x = initialPosition.x; // initial position is a point where your cannon is located.
cannonBall.y = initialPosition.y;
addChild(cannonBall);
// I suggest using TweenNano, but it has some limitations, read the License Agreement carefully
TweenNano.to(cannonBall, 15 /* animation duration */, {x: target.x, y: target.y, onComplete: makeExplosion, onCompleteParams: [cannonBall]});
}
private function makeExplosion(cannonBall: CannonBall):void
{
/* I leave this part to you, here you might want to launch some explosion animation */
}
Now we need to handle the click:
private function onMouseClick(e: MouseEvent):void
{
const target: Point = new Point(stage.mouseX, stage.mouseY);
//and launch the cannonBall:
fireCannonBall(target);
}
That's it, roughly.
To know more about TweenNano please follow the link:
https://greensock.com/tweennano-as

Problems with STAGE

I am pretty new to as3 programming. This forum helped me already a lot, but now I have a problem where I don't know how to get on. So this is my first Post on stack overflow.com.
I need StageWebView for displaying a PDF-document. After several hours, I was successful. I created the code in a new blank document and tested it step by step.
This is my code:
import flash.display.MovieClip;
import flash.media.StageWebView;
import flash.geom.Rectangle;
import flash.filesystem.File;
import flash.display.Sprite;
import flash.display.Stage;
public function StageWebViewExample(pdfdoc:String, xpos:Number, ypos:Number, breite:Number, hoehe:Number)
{
var webView:StageWebView = new StageWebView();
webView.stage = this.stage; //PROBLEM LINE
webView.viewPort = new Rectangle (xpos, ypos, breite, hoehe);
var file:String = pdfdoc;
var pdf:File = File.applicationDirectory.resolvePath(file);
webView.loadURL(pdf.nativePath);
}
StageWebViewExample("test.pdf", 200, 200, 600, 1200);
After testing, I copied the code in my existing flash-document. (The code in a several as-File and the "calling" (StageWebViewExample("....) in the existing flash-document...)
But now the code does't work anymore and there are the following Errors:
- 1119 Access of possibly undefined property stage...
- 1059 Property is read-only.
--> Both Errors referring to the same line I marked in the Code.
Has anyone an idea why it don't work?
I would really appreciate a good hint!
Answer for question is your actions ;) You moved code from the Document Class, in another one, that don't have access to the stage as instance property. I mean this.stage.
Pass Stage also to the method StageWebViewExample, function signature will look like:
public function stageWebViewExample(stage: Stage, pdfdoc:String, xpos:Number, ypos:Number, breite:Number, hoehe:Number):void {
var webView:StageWebView = new StageWebView();
webView.stage = stage;
webView.viewPort = new Rectangle (xpos, ypos, breite, hoehe);
var file:String = pdfdoc;
var pdf:File = File.applicationDirectory.resolvePath(file);
webView.loadURL(pdf.nativePath);
}
Working AIR example:
package {
import flash.display.Sprite;
import flash.display.Stage;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.media.StageWebView;
public class StackOverflow extends Sprite {
public function StackOverflow() {
addEventListener(Event.ADDED_TO_STAGE, onAdded);
}
private function onAdded(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, onAdded);
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
start();
}
private function start():void {
showWebViewAt(this.stage, "http://www.iab.net/media/file/VASTv3.0.pdf", new Rectangle(0, 0, stage.stageWidth * 0.5, stage.stageHeight));
}
private function showWebViewAt(stage:Stage, path:String, frame:Rectangle):void {
var webView:StageWebView = new StageWebView();
webView.stage = stage;
webView.viewPort = frame;
webView.loadURL(path);
}
}
}

How to remove stream video in next scene AS3

I am making a project with a video in a scene, but when I go to the next scene the video keeps appearing. How can I remove it.
My code so far is:
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.media.Video;
import flash.events.MouseEvent;
var videoConnection:NetConnection = new NetConnection();
videoConnection.connect(null);
var videoStream:NetStream = new NetStream(videoConnection);
videoStream.play("short_jump.flv");
var metaListener:Object = new Object();
metaListener.onMetaData = onMetaData;
videoStream.client = metaListener;
var video:Video = new Video();
video.attachNetStream(videoStream);
stage.addChild(video);
video.x=200;
function onMetaData(data:Object):void
{
play_btn.addEventListener(MouseEvent.CLICK, playMovie);
stop_btn.addEventListener(MouseEvent.CLICK, stopMovie);
}
function playMovie(event:MouseEvent):void
{
videoStream.play("short_jump.flv");
}
function stopMovie(event:MouseEvent):void
{
videoStream.pause();
}
Thanks for your support!
video.attachCamera( null ) stops audio and video but the last frame remains in the video. you should just video.visible = false afterwards so you don't see that frame. there's a bug where clear() doesn't do what it should.

Embedding an SWF animation at a specific location in an AS3 project

I'm doing a project in AS3 (no external libraries or Flex) and I need to embed an explosion animation over a tile when it gets destroyed. I have a small explosion animation but I don't know how I can get it to play at a specific location. Is there a way to do this? I've tried embedding it like so:
[Embed(source="../assets/64x48.swf", mimeType="application/octet-stream")] private var Explosion:Class
var explosion:MovieClip;
explosion = new Explosion();
explosion.play();
but this doesn't seem to do anything. If not an SWF, I also have a sprite sheet of an explosion I could use, but I'm not sure how to animate a sprite without using an external library.
Approach within your swf Movieclip you want to play, you must perform the following. swf convert to bytearray after, it must be restored.
EDIT
I checked your swf file. however, your swf file contents not MovieClip, but AVM1Movie file. so It is quite difficult to directly embedded. because AVM1Movie file convert to MovieClip Algorithm. but don't worry. using a ForcibileLoader easily available.
download a ForcibileLoader
original ForcibileLoader here
refer a following code. I tested great work.
package
{
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
import flash.utils.ByteArray;
import flash.utils.getTimer;
import org.libspark.utils.ForcibleLoader;
public class TestProject2 extends Sprite
{
private var loader:Loader = new Loader();
private var mc:MovieClip;
public function TestProjec2t()
{
var fLoader:ForcibleLoader = new ForcibleLoader(loader);
fLoader.load(new URLRequest("../asset/64x48.swf"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onSwfLoaded);
this.addChild(loader);
}
private function onSwfLoaded(e:Event):void
{
trace(e.currentTarget.content);
mc = e.currentTarget.content as MovieClip;
mc.x = Math.random() * stage.stageWidth;
mc.y = Math.random() * stage.stageHeight;
mc.gotoAndPlay(1);
}
}
}

what is the simpliest way to get jpg from bitmapData

I need to convert bitmapData grabbed from movie clip to jpeg or png. Is there some lib for that?
see this example in take portion that convert byte to image, i have given whole for your reference
package {
import encoding.JPGEncoder;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Sprite;
import flash.utils.ByteArray;
import flash.events.Event
public class Converter extends Sprite {
[Embed(source = "image.jpg")]
private var image:Class
private const QUALITY:uint = 80;
public function Converter():void {
var bitmap:Bitmap = new image();
var bitmapData:BitmapData = bitmap.bitmapData;
//encode BitmapData to JPG
var encoder:JPGEncoder = new JPGEncoder(QUALITY);
var rawBytes:ByteArray = encoder.encode(bitmap.bitmapData);
//decode JPG ByteArray back to BitmapData
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, getBitmapData)
loader.loadBytes(rawBytes);
}
private function getBitmapData(e:Event):void {
var decodedBitmapData:BitmapData = Bitmap(e.target.content).bitmapData
trace(decodedBitmapData);
}
}
}
You can now use the native jpeg encoding built into Flash Player 11.3 and AIR 3.3.
var quality:int = 50;
var bounds:Rectangle = bitmapData.rect;
var result:ByteArray = bitmapData.encode(bounds, new JPEGEncoderOptions(50));
This requires -swf-version=16 be passed to the compiler.
This is not the simplest way, but depending on the dimensions of your image, may be worth a look. Jens Krause has compiled jpeglib with Alchemy, and it encodes much faster than as3corelib's version, or even Thibault Imbert's improved AS3 version:
http://www.websector.de/blog/2009/06/21/speed-up-jpeg-encoding-using-alchemy/