官方教程 收藏本版 +发表新主题
查看: 12892|回复: 0
打印 上一主题 下一主题

开发基于FLARToolKit的增强现实(AR)基础教程(三)

[复制链接]

开发基于FLARToolKit的增强现实(AR)基础教程(三)

精华AngleYF 发表于 2013-5-29 10:18:32 浏览:  12892 回复:  0 只看该作者 复制链接
Creating An AR Detector(创建一个AR检测器)

当尝试了其他的一些关于FLARToolKit的例子后很容易忘记它是如何工作的。当我一行一行地分析代码才发现了它是如何构建的。我经常努力地去创建简单的,独立的类,并且可以轻松地重用和扩展。这就是我将FLARToolKit的构建密封到我们自己的叫做ARDetector类中的原因。让我们一同来创建这个类。
  1. /**
  2. * Original Author:  Jesse Freeman
  3. * Class File: ARDetector.as
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. *
  23. * Licensed under The MIT License
  24. * Redistributions of files must retain the above copyright notice.
  25. *
  26. * Revisions
  27. *                 1.0  Initial version April 29, 2009
  28. *
  29. */

  30. package com.insideria.flar{
  31.          
  32.          import flash.display.BitmapData;
  33.          import flash.events.Event;
  34.          import flash.events.EventDispatcher;
  35.          import flash.events.IEventDispatcher;
  36.          import flash.events.IOErrorEvent;
  37.          import flash.events.SecurityErrorEvent;
  38.          import flash.net.URLLoader;
  39.          import flash.net.URLLoaderDataFormat;
  40.          import flash.net.URLRequest;
  41.          
  42.          import org.libspark.flartoolkit.core.FLARCode;
  43.          import org.libspark.flartoolkit.core.param.FLARParam;
  44.          import org.libspark.flartoolkit.core.raster.rgb.FLARRgbRaster_BitmapData;
  45.          import org.libspark.flartoolkit.core.transmat.FLARTransMatResult;
  46.          import org.libspark.flartoolkit.detector.FLARSingleMarkerDetector;
  47.          
  48.          /**
  49.           * The ARDetector is a manager for the FLARToolKit and helps facilitate
  50.           * the setup, configuration, and detection of markers while using the
  51.           * FLARToolKit's underlining core classes.
  52.           *
  53.           * This class is a modification of an example on
  54.           * http://saqoosha.net/en/flartoolkit/start-up-guide
  55.           *
  56.           *
  57.           * @author Jesse Freeman
  58.           *
  59.           */        
  60.          public class ARDetector extends EventDispatcher{
  61.                   
  62.                   protected var cameraURL:String;
  63.                   protected var markerURL:String;
  64.                   protected var codeWidth:int;
  65.                   protected var _flarParam:FLARParam;
  66.                   protected var code:FLARCode;
  67.                   protected var raster:FLARRgbRaster_BitmapData;
  68.                   protected var detector:FLARSingleMarkerDetector;
  69.   
  70.                   public var width:int;
  71.                   public var height:int;
  72.                   public var markerWidth:Number = 16;
  73.                   public var markerHeight:Number = 16;
  74.                   
  75.                   /**
  76.                    *
  77.                    * This is the ARDetector consturctor. By default we set the width and
  78.                    * height to 320 x 240 to match our video source.
  79.                    *
  80.                    * @param canvasWidth set to 320 by default
  81.                    * @param canvasHeight set to 240 by default
  82.                    * @param codeWidth set to 80 by default. This represents the width of the marker.
  83.                    *
  84.                    */                                                
  85.                   public function ARDetector(canvasWidth:int = 320, canvasHeight:int = 240, codeWidth:int = 80) {
  86.                            width = canvasWidth;
  87.                            height = canvasHeight;
  88.                            this.codeWidth = codeWidth;
  89.                    }
  90.                   
  91.                   /**
  92.                    * This will return an instance of the FLARParam.
  93.                    *
  94.                    */               
  95.                   public function get flarParam():FLARParam
  96.                   {
  97.                            return _flarParam;        
  98.                    }
  99.                   
  100.                   /**
  101.                    * This sets the BitmapData src to be monitored for patterns.
  102.                    *
  103.                    */               
  104.                   public function set src(target:BitmapData):void
  105.                   {
  106.                            // setup ARToolkit
  107.                            raster = new FLARRgbRaster_BitmapData(target);
  108.                            detector = new FLARSingleMarkerDetector(_flarParam, code, codeWidth);
  109.                    }
  110.                   
  111.                   /**
  112.                    * @private
  113.                    *
  114.                    * Loads in the camera.dat file. This file contains information about
  115.                    * the webcam.
  116.                    *  
  117.                    * @param url path to camera.dat file.
  118.                    *
  119.                    */                        
  120.                   protected function loadCameraFile(url:String):void
  121.                   {
  122.                            var camLoader:URLLoader = new URLLoader();
  123.                                    camLoader.dataFormat = URLLoaderDataFormat.BINARY;
  124.                            
  125.                            camLoader.addEventListener(Event.COMPLETE, onCameraFileLoad, false, 0, true);
  126.                            addErrorListeners(camLoader);
  127.                            
  128.                            camLoader.load(new URLRequest(url));        
  129.                            
  130.                    }
  131.                   
  132.                   /**
  133.                    * @private
  134.                    *
  135.                    * Triggered on a successful camera.dat file load. Once a complete
  136.                    * event is received we remove the listeners, create a new FLARParam,
  137.                    * and set it's screen size. Finally we load the marker.
  138.                    *
  139.                    * @param event
  140.                    *
  141.                    */                        
  142.                   protected function onCameraFileLoad(event:Event):void
  143.                   {
  144.                            event.stopImmediatePropagation();
  145.                            
  146.                            var target:URLLoader = event.target as URLLoader;
  147.                            
  148.                            target.removeEventListener(Event.COMPLETE, onCameraFileLoad);
  149.                            removeErrorListeners(target);
  150.                            
  151.                            _flarParam = new FLARParam();
  152.                            _flarParam.loadARParam(target.data);
  153.                            _flarParam.changeScreenSize(width, height);
  154.                            
  155.                            loadMarkerFile(markerURL);
  156.                    }
  157.                   
  158.                   /**
  159.                    * @private
  160.                    *
  161.                    * This loads the marker.pat file. The marker represents the image we
  162.                    * look for from the src BitmapData.
  163.                    *
  164.                    */               
  165.                   protected function loadMarkerFile(url:String):void
  166.                   {
  167.                            var patLoader:URLLoader = new URLLoader();
  168.                                    patLoader.dataFormat = URLLoaderDataFormat.TEXT;
  169.                                    
  170.                            patLoader.addEventListener(Event.COMPLETE, onMarkerFileLoad, false, 0, true);
  171.                            addErrorListeners(patLoader);
  172.                            
  173.                            patLoader.load(new URLRequest(url) );
  174.                    }
  175.                   
  176.                   /**
  177.                    * @private
  178.                    *
  179.                    * Triggered when a marker.pat file is loaded. We remove the listeners
  180.                    * create a new 16x16 FLARCode instance, and pass in the pattern. This
  181.                    * represents the resolution of your marker. By default 16x16 works for
  182.                    * lower detailed patterns. After this the ARDetector is configured and
  183.                    * dispatches a Complete event.
  184.                    *
  185.                    * @param event
  186.                    *
  187.                    */                        
  188.                   protected function onMarkerFileLoad(event:Event):void
  189.                   {
  190.                            event.stopImmediatePropagation();
  191.                            
  192.                            var target:URLLoader = event.target as URLLoader;
  193.                            
  194.                            target.removeEventListener(Event.COMPLETE, onCameraFileLoad);
  195.                            removeErrorListeners(target);
  196.                            
  197.                            code = new FLARCode(markerWidth, markerHeight);
  198.                            code.loadARPatt(target.data);
  199.    
  200.                            dispatchEvent(new Event(Event.COMPLETE, true, true) );
  201.                    }
  202.                   
  203.                   /**
  204.                    * @private
  205.                    *
  206.                    * Adds error event listeners and re-dispatches them once received.
  207.                    *
  208.                    */               
  209.                   protected function addErrorListeners(target:IEventDispatcher):void
  210.                   {
  211.                            target.addEventListener(IOErrorEvent.IO_ERROR, dispatchEvent);
  212.                            target.addEventListener(SecurityErrorEvent.SECURITY_ERROR, dispatchEvent);
  213.                    }
  214.                   
  215.                   /**
  216.                    * @private
  217.                    *
  218.                    * Removes error event listeners.
  219.                    *
  220.                    */               
  221.                   protected function removeErrorListeners(target:IEventDispatcher):void
  222.                   {
  223.                            target.removeEventListener(IOErrorEvent.IO_ERROR, dispatchEvent);
  224.                            target.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, dispatchEvent);
  225.                    }
  226.                   
  227.                   /**
  228.                    * In order to set up the FLARDetector we will need two things: the
  229.                    * camera.dat and the marker.pat files. The camera.dat
  230.                    *  
  231.                    * @param cameraURL this is the path to the camera.dat file.
  232.                    * @param markerURL this is the path to the marker.pat file.
  233.                    *
  234.                    */                                    
  235.                   public function setup(cameraURL:String, markerURL:String):void {
  236.                            this.cameraURL = cameraURL;
  237.                            this.markerURL = markerURL;
  238.                            loadCameraFile(cameraURL);
  239.                    }
  240.                   
  241.                   /**
  242.                    *
  243.                    * This calculates the transformation Matrix to match the found
  244.                    * marker's coordinates.
  245.                    *
  246.                    * @param resultMat - a type of matrix that can be used to store values
  247.                    * of the detected marker's orientation.
  248.                    *
  249.                    */                        
  250.                   public function calculateTransformMatrix(resultMat:FLARTransMatResult):void
  251.                   {
  252.                            detector.getTransformMatrix(resultMat);
  253.                    }
  254.                   
  255.                   /**
  256.                    *
  257.                    * This validates if we have found a marker from the set src (raster).
  258.                    * It returns a true/false based on the supplied values. You must supply
  259.                    * a source for this to work correctly.
  260.                    *
  261.                    * @param threshold The threshold value to be used for detecting the marker.
  262.                    * @param confidence This is how confident the detector is that it found
  263.                    * a marker.
  264.                    *
  265.                    */               
  266.                   public function detectMarker(threshold:int = 90, confidence:Number = .5):Boolean
  267.                   {
  268.                            return (detector.detectMarkerLite(raster,threshold) && detector.getConfidence() > confidence)
  269.                    }
  270.   
  271.           }
  272. }
复制代码
转自:http://miyaonanhai.blogbus.com/logs/70836798.html


分享至:
| 人收藏
回复

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Copyright © 2013-2017 ARinChina-增强现实中国技术论坛   All Rights Reserved.