FLAR Torch

source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
 * FLARToolKit basic Sample
 * 
 * Copyright (c) tarotarorg
 * 
 * This work complements FLARToolkit, developed by Saqoosha as part of the Libspark project.
 *     http:
 * FLARToolKit is Copyright (C)2008 Saqoosha,
 * and is ported from NyARToolKit, which is ported from ARToolKit.
 * 
 * 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, either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 package org.tarotaro.flash.ar
{
 
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.GradientType;
    import flash.display.PixelSnapping;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.geom.Matrix;
    import flash.media.Camera;
    import flash.media.Video;
    import flash.utils.ByteArray;
    import jp.nyatla.nyartoolkit.as3.core.types.NyARDoublePoint2d;
    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;
 
 
    public class FLARemilla extends Sprite
    {
 
        private var _raster:FLARRgbRaster_BitmapData;
        private var _detector:FLARSingleMarkerDetector;
        private var _video:Video;
        private var _capture:Bitmap;
 
        private var _resultMat:FLARTransMatResult;
 
        private var remilla:Shape = new Shape();
        private static const REMILLA_SIZE:int = 300;
 
 
        [Embed(source = "assets/tarotarorg32-100.pat", mimeType = "application/octet-stream")]
        private var CodeData:Class;
        public function FLARemilla() 
        {
 
 
            var webcam:Camera = Camera.getCamera();
            if (!webcam) throw new Error('No webcam!!!!');
            webcam.setMode(320, 240, 30);
            _video = new Video(320, 240);
            _video.attachCamera(webcam);
 
 
            var codeFile:ByteArray = new CodeData() as ByteArray;
            var code:FLARCode = new FLARCode(32,32,100,100);
            code.loadARPatt(codeFile.readMultiByte(codeFile.length, "shift-jis"));
 
 
            _capture = new Bitmap(new BitmapData(320, 240, false, 0), 
                                  PixelSnapping.AUTO, true);
            _capture.cacheAsBitmap = true;
            _capture.width = 640;_capture.height = 480;
            _raster = new FLARRgbRaster_BitmapData(_capture.bitmapData);
            var base:Sprite = addChild(new Sprite()) as Sprite;
            base.addChild(_capture);
 
 
            var param:FLARParam = new FLARParam();
            param.changeScreenSize(320, 240);
 
 
            _resultMat = new FLARTransMatResult();
 
 
            _detector = new FLARSingleMarkerDetector(param, code, 80);
            _detector.setContinueMode(true);
 
            base.cacheAsBitmap = true;
            remilla.cacheAsBitmap = true;
            addChild(remilla);
            base.mask = remilla;
 
            addEventListener(Event.ENTER_FRAME, _onEnterFrame);
        }
 
        private function _onEnterFrame(e:Event = null):void {
 
 
            _capture.bitmapData.draw(_video);
            remilla.graphics.clear();
 
 
            var detected:Boolean = false;
            try {
                detected = _detector.detectMarkerLite(_raster, 80) && 
                            _detector.getConfidence() > 0.5;
            } catch (e:Error) {}
 
 
 
            if (detected) {
                var confidence:Number = _detector.getConfidence();
                var center:NyARDoublePoint2d = new NyARDoublePoint2d();
                _detector.getSquare().getCenter2d(center);
                trace(center.x + "," + center.y + "," + confidence);
                var remWidth:Number = REMILLA_SIZE * confidence;
                var remHeight:Number = remWidth * 1.2;
                var remx:Number = center.x * 2 - remWidth / 2;
                var remy:Number = center.y * 2 - remHeight / 2 - 80 * confidence;
                var matrix:Matrix = new Matrix(); 
                matrix.createGradientBox(remWidth, remHeight, 0, remx, remy); 
 
                remilla.graphics.beginGradientFill(GradientType.RADIAL, 
                                                   [0xffffff, 0x000000],
                                                   [1, 0],
                                                   [0, 255],
                                                   matrix
                                                  );
                remilla.graphics.drawEllipse(remx, remy, remWidth, remHeight);
                remilla.graphics.endFill();
            } 
        }
    }
}
Ads

Ad

Ad

Share

  • Add this article to hatena bookmark
  • 0

Follow

Ads

Ad

Comments

  1. […] This post was mentioned on Twitter by Richard, 太郎. 太郎 said: I updated my blog. http://bit.ly/bcfCz1 […]

  2. andreabong says:

    CAN ANYONE OUT THERE HELP ME. I HAVE A PROBLEM WITH MY MARKER. I PRINTED MY IMAGE OUT HOWEVER, WHEN I REACHED THE STEP ‘ SAVE’ , IT JUST WOULDNT WORK.

    ALTHOUGH I PRESS’ SAVE’ BUT IT JUST DOESNT DO ANYTHING. IVE BEEN TRYING FOR FEW HOURS, ALMOST A DAY BUT STILL COULDNT GET IT TO WORK, IM REALLY FRUSTRATED ABOUT IT. CAN ANYONE HELP. THANK YOU SO MUCH

  3. sagar says:

    Can u share the source code for this??

  4. tarotarorg says:

    Hi, sagar.

    Yes you can get source from here.
    http://flash.tarotaro.org/blog/2010/11/01/flar-torch/2/

    Thank you.

  5. tarotarorg says:

    Hi, andreabong.

    Please check your Flash Player version.
    Is it latest ?

    Thank you.

Leave a Comment

Your email address will not be published. Required fields are marked *