OSDN Git Service

3cf477927e612ed1fbe788c4263c77122413b3fd
[nyartoolkit-and/nyartoolkit-and.git] / src / jp / nyatla / nyartoolkit / sample / NyIdTest.java
1 /* \r
2  * PROJECT: NyARToolkit\r
3  * --------------------------------------------------------------------------------\r
4  * This work is based on the original ARToolKit developed by\r
5  *   Hirokazu Kato\r
6  *   Mark Billinghurst\r
7  *   HITLab, University of Washington, Seattle\r
8  * http://www.hitl.washington.edu/artoolkit/\r
9  *\r
10  * The NyARToolkit is Java edition ARToolKit class library.\r
11  * Copyright (C)2008-2009 Ryo Iizuka\r
12  *\r
13  * This program is free software; you can redistribute it and/or\r
14  * modify it under the terms of the GNU Lesser General Public License\r
15  * as published by the Free Software Foundation; either version 3\r
16  * of the License, or (at your option) any later version.\r
17  * \r
18  * This program is distributed in the hope that it will be useful,\r
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
21  * GNU Lesser General Public License for more details\r
22  * \r
23  * You should have received a copy of the GNU Lesser General Public\r
24  * License along with this program. If not, see <http://www.gnu.org/licenses/>.\r
25  * \r
26  * For further information please contact.\r
27  *      http://nyatla.jp/nyatoolkit/\r
28  *      <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>\r
29  * \r
30  */\r
31 package jp.nyatla.nyartoolkit.sample;\r
32 \r
33 import java.io.*;\r
34 \r
35 \r
36 import jp.nyatla.nyartoolkit.core.param.NyARParam;\r
37 import jp.nyatla.nyartoolkit.core.raster.rgb.*;\r
38 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquare;\r
39 import jp.nyatla.nyartoolkit.core.transmat.*;\r
40 import jp.nyatla.nyartoolkit.nyidmarker.data.*;\r
41 import jp.nyatla.nyartoolkit.processor.*;\r
42 \r
43 /**\r
44  * 320x240のBGRA32で記録されたIdmarkerを撮影したRAWイメージから、\r
45  * Idマーカを認識します。\r
46  *\r
47  */\r
48 public class NyIdTest\r
49 {\r
50     public class MarkerProcessor extends SingleNyIdMarkerProcesser\r
51     {\r
52         private Object _sync_object = new Object();\r
53         public NyARTransMatResult transmat = null;\r
54         public int current_id = -1;\r
55 \r
56         public MarkerProcessor(NyARParam i_cparam, int i_raster_format) throws Exception\r
57         {\r
58                 super();//\r
59             initInstance(i_cparam, new NyIdMarkerDataEncoder_RawBit(), i_raster_format);\r
60             //アプリケーションフレームワークの初期化\r
61             return;\r
62         }\r
63         /**\r
64          * アプリケーションフレームワークのハンドラ(マーカ出現)\r
65          */\r
66         protected void onEnterHandler(INyIdMarkerData i_code)\r
67         {\r
68             synchronized (this._sync_object)\r
69             {\r
70                 NyIdMarkerData_RawBit code = (NyIdMarkerData_RawBit)i_code;\r
71                 if (code.length > 4)\r
72                 {\r
73                     //4バイト以上の時はint変換しない。\r
74                     this.current_id = -1;//undefined_id\r
75                 }\r
76                 else\r
77                 {\r
78                     this.current_id = 0;\r
79                     //最大4バイト繋げて1個のint値に変換\r
80                     for (int i = 0; i < code.length; i++)\r
81                     {\r
82                         this.current_id = (this.current_id << 8) | code.packet[i];\r
83                     }\r
84                 }\r
85                 this.transmat = null;\r
86             }\r
87         }\r
88         /**\r
89          * アプリケーションフレームワークのハンドラ(マーカ消滅)\r
90          */\r
91         protected void onLeaveHandler()\r
92         {\r
93                 synchronized (this._sync_object)\r
94             {\r
95                 this.current_id = -1;\r
96                 this.transmat = null;\r
97             }\r
98             return;\r
99         }\r
100         /**\r
101          * アプリケーションフレームワークのハンドラ(マーカ更新)\r
102          */\r
103         protected void onUpdateHandler(NyARSquare i_square, NyARTransMatResult result)\r
104         {\r
105                 synchronized (this._sync_object)\r
106             {\r
107                 this.transmat = result;\r
108             }\r
109         }\r
110     }\r
111         private final String data_file = "../Data/320x240NyId.raw";\r
112         private final String camera_file = "../Data/camera_para.dat";\r
113     public NyIdTest()\r
114     {\r
115     }\r
116     public void Test() throws Exception\r
117     {\r
118         //AR用カメラパラメタファイルをロード\r
119         NyARParam ap = new NyARParam();\r
120         ap.loadARParamFromFile(camera_file);\r
121         ap.changeScreenSize(320, 240);\r
122 \r
123                 // 試験イメージの読み出し(320x240 BGRAのRAWデータ)\r
124                 File f = new File(data_file);\r
125                 FileInputStream fs = new FileInputStream(data_file);\r
126                 byte[] buf = new byte[(int) f.length()];\r
127                 fs.read(buf);           \r
128 \r
129         NyARRgbRaster_RGB ra = NyARRgbRaster_RGB.wrap(buf, 320, 240);\r
130 \r
131         MarkerProcessor pr = new MarkerProcessor(ap, ra.getBufferReader().getBufferType());\r
132         pr.detectMarker(ra);\r
133         return;\r
134     }\r
135         public static void main(String[] args)\r
136         {\r
137 \r
138                 try {\r
139                         NyIdTest t = new NyIdTest();\r
140                         // t.Test_arGetVersion();\r
141                         t.Test();\r
142                 } catch (Exception e) {\r
143                         e.printStackTrace();\r
144                 }\r
145         }    \r
146 }\r