Category Archives: Flex

Quick Tip: How to use your microphone in AS 3.0

Hello again and thanks for all messages. But please, no more SPAM. Well, let’s get back to our bussines, today I’ll present to you a new Quick Tip: How to use your microphone in AS 3.0. A few months ago I posted something about How to use your camera, and now you will learn how to play with your microphone.

The code is not hard at all, I’m sure that you will understand immediately. But if you have any questions please contact me or leave me a comment:

 
package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	import flash.media.Microphone;
	import flash.utils.setInterval;
 
	public class Mic extends Sprite
	{
		private var microphone:Microphone;
		private var volumeBar:Bitmap = new Bitmap(new BitmapData(100,10,true));
 
		public function Mic()
		{
			this.addChild(volumeBar);
			microphone = Microphone.getMicrophone();
			microphone.setLoopBack(true);
			microphone.setUseEchoSuppression(true);
			setInterval(show,30);
		}
 
		private function show():void
		{
			with(volumeBar.bitmapData)
			{
				fillRect(new Rectangle(0,0,100,10), 0x00000000);
				fillRect(new Rectangle(0,0,microphone.activityLevel,10),0xff00ff00);
 
			}
 
 
		}
	}
}

As you can see this is not only a simple exemple. In this code a have a rectange that measure the sound intensity recorded by microphone. The show function draw the rectangle and make him to oscilate depends of sound intensity.

Bounce Effect

Hello again. I come back with a simple exemple of ‘bouncing on stage’. Very simple to understand, and if you have any questions, feel free to ask. I have a lot on my head right now, sorry for my little introduction for this ActionScript class.

package  
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.geom.ColorTransform;
	import flash.geom.Transform;
 
	public class Ball extends MovieClip
	{
		public var speedX:int = 10;
		public var speedY:int = -10;
 
		public function Ball() 
		{
			// constructor code
			addEventListener(Event.ENTER_FRAME, onE);
			var cT:ColorTransform = new ColorTransform();
			cT.color = Math.random() * 0xFFFFFF;
			transform.colorTransform = cT;
		}
 
		private function onE(e:Event):void
		{
			x += speedX;
			y += speedY;
 
			var bounds:Rectangle = getBounds(parent);
 
			if(bounds.left < 0 || bounds.right > stage.stageWidth)
			{
				speedX *= -1;
			}
 
			if(bounds.top < 0 || bounds.bottom > stage.stageHeight)
			{
				speedY *= -1;
			}
		}
 
	}
 
}

Download
Downloaded 364 times

Quick Tip: How to use your web camera in AS 3.0

In the following code, I will access the camera that I have at my computer.

Video type objects can be used to render images from the webcam. flash.media.Camera class extends EventDispatcher – are inherited property / events / methods in this class. The camera will be attached to a Video object using the method attachCamera. A camera object will not be added through the operator “new “, the reason being that, every time, we must access the same memory area in which we find images from the webcam.Assume that the system is attached a single webcam.

Code:

package  
{
	import flash.media.Video;
	import flash.media.Camera;
	import flash.display.Sprite;
 
	public class main extends Sprite
	{
		private var v:Video = new Video();
		private var c:Camera = Camera.getCamera();
 
		public function main() 
		{
			v.attachCamera(c);
			this.addChild(v);
			c.setMode(320,240,32,true);
 
		}
 
	}
 
}

Moving Around

I’m sorry for my absence, but from now on I promise I’ll post more often. I’m back with a very simple project and easy to understand. I have a single object that moves.

Code:

package
{
	import flash.display.Sprite;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
 
	public class KeyCodes extends Sprite
	{
		private var ball:Sprite;
 
		public function KeyCodes() 
 
		{
			init();
		}
 
		private function init():void
		{
			ball = new Sprite();
			addChild(ball);
			ball.graphics.beginFill(0xff0000);
			ball.graphics.drawCircle(0, 0, 40);
			ball.graphics.endFill();
			ball.x = stage.stageWidth / 2;
			ball.y = stage.stageHeight / 2;
 
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyboardEvent);
		}
 
		public function onKeyboardEvent(event:KeyboardEvent):void
		{
			switch(event.keyCode)
			{
				case Keyboard.UP :
				ball.y -= 10;
				break;
 
				case Keyboard.DOWN :
				ball.y += 10;
				break;
 
				case  Keyboard.LEFT :
				ball.x -= 10;
				break;
				case Keyboard.RIGHT :
				ball.x += 10;
				break;
 
				default:
				break;
			}
		}
	}
}

Download
Downloaded 255 times

AS 3.0 Augmented Reality with Blender and PV3D

Introduction

In this tutorial you will learn how to create a simple AR(Augmented Reality) scene, at first appearance this code is a little hairy but you don’t have to understand every part of it to use it.

Requirements

 

 

1. Web Camera

 

2. Adobe Flash CS4

 

Try or Buy

 

3. Flex Builder

 

Try or Buy

 

4. Blender

 

Download

 

5. PV 3D

 

Download

 

6. Augmented Reality

 

Download

 

7. Marker File

 

Download

 

8. Pattern

 

Download

Pre-Requesites

It would be useful for you if you are familiar with PV3D. It would be good to know 3d modeling with Blender and other 3d modeling program

Save a Pattern

First of all, this is not a tutorial for beginers, but I will try to explain very well all the steps that must be taken to obtain the final result.

Well, download the Marker File. You will see a AIR file, this AIR file will help ous to save a pattern for our Augmented Reality project. Instal the Marker File…on the desktop you will see a ARToolKit Marker Generator AIR file.

Open it, in the same time you Web Camera will start, than you need to save a pattern, you will see a button named Save Pattern and same red lines walking there, those lines will try to find a pattern. Well, now you need to show a paper with a pattern.

A exemple of pattern:

While, you show the paper, on the camera you will see that the red linis are around the pattern.Like in this picture:

In that moment click the save button and save the pattern. In this tutorial my pattern is named pattern.pat.

That was the first step.

3D Model

For designers, this is maybe the most important step, but I’m not a designer ao something like that. I’m only a ActionScript developer, and I will analyze the step from my point of view. For this step you can use Blender, Swift 3D or another modeling program, but unse only the program able to export in Collada. First if you are a beginer in 3d (like me), you can draw a cube or something. If you are use Blender, and have already the made 3d model. Go to File->Export->Collada 1.4(dae).

After exporting you will have a file named something.dae. This file contains our 3d model that we use in PV3D.

If you are useing Swift 3D, the first thing again is draw you model, and export to PV3D. In Swift 3D, we use other option caled Export Selected object To Papervision3D…. Like in this picture:

After that you will have a .swf file , a .fla file, and the most important thing a .dae file. Well now we have the pattern and the 3d model. Is time to do same develop work with ActionScript 3.0 .

Final step 2

Make a new project

If you use Flex Builder just create a new ActionScript 3.0 Project and called augmentedReality. If you unse Flash CS3 or CS4 create a Flash File(ActionScript 3.0), you can give what name you want. At this file create a class called augmentedReality.as. The Flash File and the class must be in the same folder.

For Flex:

And if you are useing flash, there is a simple pictutre to help you:

Well, now we are ready to write the code.

Final step 3.

Start to write the code

package
{
	import flash.display.Sprite;
 
	public class augmentedReality extends Sprite
	{
		public function augmentedReality()
		{
 
		}
	}
}

Here we have the augmentedReality class copy this in Flash, if you are useing flash.

Same imports thing

package
{
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.media.Camera;
    import flash.media.Video;
    import flash.utils.ByteArray;
 
    import org.libspark.flartoolkit.core.FLARCode;
    import org.libspark.flartoolkit.core.param.FLARParam;
    import org.libspark.flartoolkit.core.raster.rgb.FLARRgbRaster_BitmapData;
    import org.libspark.flartoolkit.core.transmat.FLARTransMatResult;
    import org.libspark.flartoolkit.detector.FLARSingleMarkerDetector;
    import org.libspark.flartoolkit.pv3d.FLARBaseNode;
    import org.libspark.flartoolkit.pv3d.FLARCamera3D;
    import org.papervision3d.objects.parsers.Collada;
    import org.papervision3d.render.BasicRenderEngine;
    import org.papervision3d.scenes.Scene3D;
    import org.papervision3d.view.Viewport3D;
 
    [SWF(width="640", height="480", frameRate="30", backgroundColor="#FFFFFF")]
 
    public class augmentedReality extends Sprite
    {
         public function augmentedReality()
	   {
 
         }
     }
}

Well, here we need the Papervision 3d library, and the Augmented Reality library.

Private Variables and Embled files

Now we need to embled the pattern and a most important file the camera_para.dat This file is in the Augmented Reality lib . You don’t need to know want is in this file just embled this file.

package
{
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.media.Camera;
    import flash.media.Video;
    import flash.utils.ByteArray;
 
    import org.libspark.flartoolkit.core.FLARCode;
    import org.libspark.flartoolkit.core.param.FLARParam;
    import org.libspark.flartoolkit.core.raster.rgb.FLARRgbRaster_BitmapData;
    import org.libspark.flartoolkit.core.transmat.FLARTransMatResult;
    import org.libspark.flartoolkit.detector.FLARSingleMarkerDetector;
    import org.libspark.flartoolkit.pv3d.FLARBaseNode;
    import org.libspark.flartoolkit.pv3d.FLARCamera3D;
    import org.papervision3d.objects.parsers.Collada;
    import org.papervision3d.render.BasicRenderEngine;
    import org.papervision3d.scenes.Scene3D;
    import org.papervision3d.view.Viewport3D;
 
    [SWF(width="640", height="480", frameRate="30", backgroundColor="#FFFFFF")]
 
	public class augmentedReality extends Sprite
	{
		[Embed(source="pattern.pat", mimeType="application/octet-stream")]
		private var pattern:Class;
 
		[Embed(source="camera_para.dat", mimeType="application/octet-stream")]
		private var params:Class;
 
		private var FlarParams:FLARParam;
		private var FlarCode:FLARCode;
		private var vid:Video;
		private var camera:Camera;
		private var FlarCamera:FLARCamera3D;
		private var bmd:BitmapData;
		private var FlarRaster:FLARRgbRaster_BitmapData;
		private var FlarDetector:FLARSingleMarkerDetector;
		private var container:FLARBaseNode;
		private var viewPort:Viewport3D;
		private var scene:Scene3D;
		private var renderEngine:BasicRenderEngine;
		private var TransMatResult:FLARTransMatResult;
 
            public function augmentedReality()
		{
			setFLAR();
			setCamera();
			setBitmap();
			setPV3D();
			addEventListener(Event.ENTER_FRAME, loop);
		}
     }
}

In the constructor we have same function that we will have to explain, in the following steps.

We have the setFLAR(); function. In this function we will load the camera_para, and the pattern.

In the setCamera(); function, we will set the option for the Web Camera.. the resolution, the fps.

Well in the setBitmap(); function, we will do a bitmap data for our FLARRgbRaster_BitmapData. And we will draw the bitmap to the video.

And here at the setPV3D(); function, we will add the 3d model.

Private Methods

            private function setFLAR():void
		{
			FlarParams = new FLARParam();
			FlarParams.loadARParam(new params() as ByteArray);
			FlarCode = new FLARCode(16,16);
			FlarCode.loadARPatt(new pattern());
		}
 
		private function setCamera():void
		{
			vid = new Video(640,480);
			camera = Camera.getCamera();
			camera.setMode(640,480,31);
			vid.attachCamera(camera);
			addChild(vid);
		}
 
		private function setBitmap():void
		{
			bmd = new BitmapData(640,480,true);
			bmd.draw(vid);
			FlarRaster = new FLARRgbRaster_BitmapData(bmd);
			FlarDetector = new FLARSingleMarkerDetector(FlarParams,FlarCode,80);
		}
 
		private function setPV3D():void
		{
			scene = new Scene3D();
			FlarCamera = new FLARCamera3D(FlarParams);
			container = new FLARBaseNode();
			scene.addChild(container);
 
			var obj:Collada = new Collada("tank.dae");
			container.addChild(obj);
 
			renderEngine = new BasicRenderEngine();
			TransMatResult = new FLARTransMatResult();
 
			FlarCamera.z = 0;
 
			viewPort = new Viewport3D();
			addChild(viewPort);
 
			obj.rotationY += 20;
			obj.rotationX += 10;
		}

Well,for a better understanding about this private methods. I will explain again what deals they have.

            private function setFLAR():void
		{
			FlarParams = new FLARParam();
			FlarParams.loadARParam(new params() as ByteArray);
			FlarCode = new FLARCode(16,16);
			FlarCode.loadARPatt(new pattern());
		}

In setFLAR we have a FlarParams instance, and we load the params in this instance. And because the params are a binary file, we load this params as a ByteArray. And in the FLARCode instance we load the pattern file, and give a resolution, in this case 16×16.

private function setCamera():void
		{
			vid = new Video(640,480);
			camera = Camera.getCamera();
			camera.setMode(640,480,31);
			vid.attachCamera(camera);
			addChild(vid);
		}

The setCamera function set up the option for our camera. Well, I set up the resolution 640×480, but if you have a more performance camera, give a larger resolution. And I think 31 fps is good for all kind of camera. And after that we attach the camera on the Video instance. And of course we addChild the vid(Video).

Well, after you set this part of code, if you run you project, ypu will see, samething like that:

And if you click Allow, your camera will start.

private function setBitmap():void
		{
			bmd = new BitmapData(640,480,true);
			bmd.draw(vid);
			FlarRaster = new FLARRgbRaster_BitmapData(bmd);
			FlarDetector = new FLARSingleMarkerDetector(FlarParams,FlarCode,80);
		}

Well, now we have the setBitmap, here we create a BitmapData, give the same resolution as the camera. And we “draw” the vid that contains all the information about our camera.After that we have the FlarRaster and the FlarDetector. Well the FlarDetector, is a detector, which will see the pattern on the stage… 80 reprents the marker width.

private function setPV3D():void
		{
			scene = new Scene3D();
			FlarCamera = new FLARCamera3D(FlarParams);
			container = new FLARBaseNode();
			scene.addChild(container);
 
			var obj:Collada = new Collada("tank.dae");
			container.addChild(obj);
 
			renderEngine = new BasicRenderEngine();
			TransMatResult = new FLARTransMatResult();
 
			FlarCamera.z = 0;
 
			viewPort = new Viewport3D();
			addChild(viewPort);
 
			obj.rotationY += 20;
			obj.rotationX += 10;
		}

And finaly the setPV3D function. In this function I add the 3d model to the FLARBaseNode.

Final Code

package
{
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.media.Camera;
    import flash.media.Video;
    import flash.utils.ByteArray;
 
    import org.libspark.flartoolkit.core.FLARCode;
    import org.libspark.flartoolkit.core.param.FLARParam;
    import org.libspark.flartoolkit.core.raster.rgb.FLARRgbRaster_BitmapData;
    import org.libspark.flartoolkit.core.transmat.FLARTransMatResult;
    import org.libspark.flartoolkit.detector.FLARSingleMarkerDetector;
    import org.libspark.flartoolkit.pv3d.FLARBaseNode;
    import org.libspark.flartoolkit.pv3d.FLARCamera3D;
    import org.papervision3d.objects.parsers.Collada;
    import org.papervision3d.render.BasicRenderEngine;
    import org.papervision3d.scenes.Scene3D;
    import org.papervision3d.view.Viewport3D;
 
    [SWF(width="640", height="480", frameRate="30", backgroundColor="#FFFFFF")]
 
	public class augmentedReality extends Sprite
	{
		[Embed(source="pattern.pat", mimeType="application/octet-stream")]
		private var pattern:Class;
 
		[Embed(source="camera_para.dat", mimeType="application/octet-stream")]
		private var params:Class;
 
		private var FlarParams:FLARParam;
		private var FlarCode:FLARCode;
		private var vid:Video;
		private var camera:Camera;
		private var FlarCamera:FLARCamera3D;
		private var bmd:BitmapData;
		private var FlarRaster:FLARRgbRaster_BitmapData;
		private var FlarDetector:FLARSingleMarkerDetector;
		private var container:FLARBaseNode;
		private var viewPort:Viewport3D;
		private var scene:Scene3D;
		private var renderEngine:BasicRenderEngine;
		private var TransMatResult:FLARTransMatResult;
 
		public function augmentedReality()
		{
			setFLAR();
			setCamera();
			setBitmap();
			setPV3D();
			addEventListener(Event.ENTER_FRAME, loop);
		}
 
		private function setFLAR():void
		{
			FlarParams = new FLARParam();
			FlarParams.loadARParam(new params() as ByteArray);
			FlarCode = new FLARCode(16,16);
			FlarCode.loadARPatt(new pattern());
		}
 
		private function setCamera():void
		{
			vid = new Video(640,480);
			camera = Camera.getCamera();
			camera.setMode(640,480,31);
			vid.attachCamera(camera);
			addChild(vid);
		}
 
		private function setBitmap():void
		{
			bmd = new BitmapData(640,480,true);
			bmd.draw(vid);
			FlarRaster = new FLARRgbRaster_BitmapData(bmd);
			FlarDetector = new FLARSingleMarkerDetector(FlarParams,FlarCode,80);
		}
 
		private function setPV3D():void
		{
			scene = new Scene3D();
			FlarCamera = new FLARCamera3D(FlarParams);
			container = new FLARBaseNode();
			scene.addChild(container);
 
			var obj:Collada = new Collada("tank.dae");
			container.addChild(obj);
 
			renderEngine = new BasicRenderEngine();
			TransMatResult = new FLARTransMatResult();
 
			FlarCamera.z = 0;
 
			viewPort = new Viewport3D();
			addChild(viewPort);
 
			obj.rotationY += 20;
			obj.rotationX += 10;
		}
 
		private function loop(e:Event):void
		{
			bmd.draw(vid);
 
			try
			{
				if(FlarDetector.detectMarkerLite(FlarRaster, 80) &amp;&amp; FlarDetector.getConfidence() &gt; 0.5)
				{
					FlarDetector.getTransformMatrix(TransMatResult);
					container.setTransformMatrix(TransMatResult);
					renderEngine.renderScene(scene, FlarCamera, viewPort);
				}
			}
 
			catch(e:Error){}
		}
	}
}

And the final result must be something like that,my 3d model is a thank, and look how he is:

That was the Augmented Reality tutorial.

Cya next time

 

 

/* */