Skip to content

Instantly share code, notes, and snippets.

@mash
Created April 7, 2010 05:35
Show Gist options
  • Save mash/358573 to your computer and use it in GitHub Desktop.
Save mash/358573 to your computer and use it in GitHub Desktop.
package org.libspark.flartoolkit.example {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.PixelSnapping;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.media.Camera;
import flash.media.Video;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
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.detector.FLARSingleMarkerDetector;
[Event(name="init",type="flash.events.Event")]
[Event(name="init",type="flash.events.Event")]
[Event(name="ioError",type="flash.events.IOErrorEvent")]
[Event(name="securityError",type="flash.events.SecurityErrorEvent")]
public class ARAppBase extends Sprite {
private var _loader:URLLoader;
private var _cameraFile:String;
private var _codeFile:String;
private var _width:int;
private var _height:int;
private var _codeWidth:int;
protected var _param:FLARParam;
protected var _code:FLARCode;
protected var _raster:FLARRgbRaster_BitmapData;
protected var _detector:FLARSingleMarkerDetector;
protected var _webcam:Camera;
protected var _video:Video;
protected var _capture:Bitmap;
public function ARAppBase() {
}
protected function init(cameraFile:String, codeFile:String, canvasWidth:int = 320, canvasHeight:int = 240, codeWidth:int = 80):void {
_cameraFile = cameraFile;
_width = canvasWidth;
_height = canvasHeight;
_codeFile = codeFile;
_codeWidth = codeWidth;
_loader = new URLLoader();
_loader.dataFormat = URLLoaderDataFormat.BINARY;
_loader.addEventListener(Event.COMPLETE, _onLoadParam);
_loader.addEventListener(IOErrorEvent.IO_ERROR, dispatchEvent);
_loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, dispatchEvent);
_loader.load(new URLRequest(_cameraFile));
}
private function _onLoadParam(e:Event):void {
_loader.removeEventListener(Event.COMPLETE, _onLoadParam);
_param = new FLARParam();
_param.loadARParam(_loader.data);
_param.changeScreenSize(_width, _height);
_loader.dataFormat = URLLoaderDataFormat.TEXT;
_loader.addEventListener(Event.COMPLETE, _onLoadCode);
_loader.load(new URLRequest(_codeFile));
}
private function _onLoadCode(e:Event):void {
_code = new FLARCode(16, 16);
_code.loadARPatt(_loader.data);
_loader.removeEventListener(Event.COMPLETE, _onLoadCode);
_loader.removeEventListener(IOErrorEvent.IO_ERROR, dispatchEvent);
_loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, dispatchEvent);
_loader = null;
// setup webcam
_webcam = Camera.getCamera();
if (!_webcam) {
throw new Error('No webcam!!!!');
}
_webcam.setMode(_width, _height, 30);
_video = new Video(_width, _height);
_video.attachCamera(_webcam);
_capture = new Bitmap(new BitmapData(_width, _height, false, 0), PixelSnapping.AUTO, true);
// setup ARToolkit
_raster = new FLARRgbRaster_BitmapData(_capture.bitmapData);
_detector = new FLARSingleMarkerDetector(_param, _code, _codeWidth);
_detector.setContinueMode(true);
dispatchEvent(new Event(Event.INIT));
}
protected function onInit():void {
}
}
}
package org.libspark.flartoolkit.example {
import flash.display.Sprite;
import flash.events.Event;
import org.libspark.flartoolkit.core.transmat.FLARTransMatResult;
import org.libspark.flartoolkit.support.pv3d.FLARBaseNode;
import org.libspark.flartoolkit.support.pv3d.FLARCamera3D;
import org.papervision3d.render.LazyRenderEngine;
import org.papervision3d.scenes.Scene3D;
import org.papervision3d.view.Viewport3D;
public class PV3DARApp extends ARAppBase {
protected var _base:Sprite;
protected var _viewport:Viewport3D;
protected var _camera3d:FLARCamera3D;
protected var _scene:Scene3D;
protected var _renderer:LazyRenderEngine;
protected var _markerNode:FLARBaseNode;
protected var _resultMat:FLARTransMatResult = new FLARTransMatResult();
public function PV3DARApp() {
}
protected override function init(cameraFile:String, codeFile:String, canvasWidth:int = 320, canvasHeight:int = 240, codeWidth:int = 80):void {
addEventListener(Event.INIT, _onInit, false, int.MAX_VALUE);
super.init(cameraFile, codeFile, canvasWidth, canvasHeight, codeWidth);
}
private function _onInit(e:Event):void {
_base = addChild(new Sprite()) as Sprite;
_capture.width = 640;
_capture.height = 480;
_base.addChild(_capture);
_viewport = _base.addChild(new Viewport3D(320, 240)) as Viewport3D;
_viewport.scaleX = 640 / 320;
_viewport.scaleY = 480 / 240;
_viewport.x = -4; // 4pix ???
_camera3d = new FLARCamera3D(_param);
_scene = new Scene3D();
_markerNode = _scene.addChild(new FLARBaseNode()) as FLARBaseNode;
_renderer = new LazyRenderEngine(_scene, _camera3d, _viewport);
addEventListener(Event.ENTER_FRAME, _onEnterFrame);
}
private function _onEnterFrame(e:Event = null):void {
_capture.bitmapData.draw(_video);
var detected:Boolean = false;
try {
detected = _detector.detectMarkerLite(_raster, 80) && _detector.getConfidence() > 0.5;
} catch (e:Error) {}
if (detected) {
_detector.getTransformMatrix(_resultMat);
_markerNode.setTransformMatrix(_resultMat);
_markerNode.visible = true;
} else {
_markerNode.visible = false;
}
_renderer.render();
}
public function set mirror(value:Boolean):void {
if (value) {
_base.scaleX = -1;
_base.x = 640;
} else {
_base.scaleX = 1;
_base.x = 0;
}
}
public function get mirror():Boolean {
return _base.scaleX < 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment