OSDN Git Service

f19612f56fe9ea4ccd2ef0db167e26e8aaea2fc5
[nyartoolkit-and/nyartoolkit-and.git] / sample / jmf / jp / nyatla / nyartoolkit / jmf / sample / NyarToolkitLinkTest.java
1 /* 
2  * PROJECT: NyARToolkit JMF sample program.
3  * --------------------------------------------------------------------------------
4  * The MIT License
5  * Copyright (c) 2008 nyatla
6  * airmail(at)ebony.plala.or.jp
7  * http://nyatla.jp/nyartoolkit/
8  * 
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this software and associated documentation files (the "Software"), to deal
11  * in the Software without restriction, including without limitation the rights
12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  * 
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  * 
26  */
27 package jp.nyatla.nyartoolkit.jmf.sample;
28
29 import javax.media.*;
30
31 import javax.media.util.BufferToImage;
32 import javax.media.format.*;
33
34 import jp.nyatla.nyartoolkit.NyARException;
35 import jp.nyatla.nyartoolkit.jmf.utils.*;
36
37 import java.awt.*;
38
39 import jp.nyatla.nyartoolkit.core.*;
40 import jp.nyatla.nyartoolkit.core.param.NyARParam;
41 import jp.nyatla.nyartoolkit.core.transmat.*;
42 import jp.nyatla.nyartoolkit.detector.NyARSingleDetectMarker;
43
44 /**
45  * VFM+ARToolkitテストプログラム
46  * カメラから取り込んだデータからマーカーを検出して、一致度と変換行列を表示します。
47  */
48 public class NyarToolkitLinkTest extends Frame implements JmfCaptureListener
49 {
50         private static final long serialVersionUID = 6471434231970804953L;
51
52         private final String CARCODE_FILE = "../../Data/patt.hiro";
53
54         private final String PARAM_FILE = "../../Data/camera_para.dat";
55
56         private JmfCaptureDevice _capture;
57
58         private NyARSingleDetectMarker _nya;
59
60         private JmfNyARRaster_RGB _raster;
61
62         private NyARTransMatResult _trans_mat_result = new NyARTransMatResult();
63
64         public NyarToolkitLinkTest() throws NyARException, NyARException
65         {
66                 setTitle("JmfCaptureTest");
67                 setBounds(0, 0, 320 + 64, 240 + 64);
68                 //キャプチャの準備
69                 JmfCaptureDeviceList devlist=new JmfCaptureDeviceList();
70                 _capture=devlist.getDevice(0);
71                 _capture.setCaptureFormat(JmfCaptureDevice.PIXEL_FORMAT_RGB,320, 240,15f);
72                 _capture.setOnCapture(this);
73
74                 //NyARToolkitの準備
75                 NyARParam ar_param = new NyARParam();
76                 NyARCode ar_code = new NyARCode(16, 16);
77                 ar_param.loadARParamFromFile(PARAM_FILE);
78                 ar_param.changeScreenSize(320, 240);
79                 this._nya = new NyARSingleDetectMarker(ar_param, ar_code, 80.0);
80                 ar_code.loadARPattFromFile(CARCODE_FILE);
81                 //キャプチャイメージ用のラスタを準備
82                 this._raster = new JmfNyARRaster_RGB(320, 240);
83                 return;
84         }
85
86         public void onUpdateBuffer(Buffer i_buffer)
87         {
88                 try {
89                         //キャプチャしたバッファをラスタにセット
90                         this._raster.setBuffer(i_buffer);
91
92                         //キャプチャしたイメージを表示用に加工
93                         BufferToImage b2i = new BufferToImage((VideoFormat) i_buffer.getFormat());
94                         Image img = b2i.createImage(i_buffer);
95
96                         Graphics g = getGraphics();
97
98                         //マーカー検出
99                         boolean is_marker_exist = this._nya.detectMarkerLite(this._raster, 100);
100                         if (is_marker_exist) {
101                                 //変換行列を取得
102                                 this._nya.getTransmationMatrix(this._trans_mat_result);
103                         }
104                         //情報を画面に書く       
105                         g.drawImage(img, 32, 32, this);
106                         if (is_marker_exist) {
107                                 g.drawString("マーカー検出:" + this._nya.getConfidence(), 32, 50);
108                                 g.drawString("[m00]" +this._trans_mat_result.m00, 32, 50 + 16*1);
109                                 g.drawString("[m01]" +this._trans_mat_result.m01, 32, 50 + 16*2);
110                                 g.drawString("[m02]" +this._trans_mat_result.m02, 32, 50 + 16*3);
111                                 g.drawString("[m03]" +this._trans_mat_result.m03, 32, 50 + 16*4);
112                                 g.drawString("[m10]" +this._trans_mat_result.m10, 32, 50 + 16*5);
113                                 g.drawString("[m11]" +this._trans_mat_result.m11, 32, 50 + 16*6);
114                                 g.drawString("[m12]" +this._trans_mat_result.m12, 32, 50 + 16*7);
115                                 g.drawString("[m13]" +this._trans_mat_result.m13, 32, 50 + 16*8);
116                                 g.drawString("[m20]" +this._trans_mat_result.m20, 32, 50 + 16*9);
117                                 g.drawString("[m21]" +this._trans_mat_result.m21, 32, 50 + 16*10);
118                                 g.drawString("[m22]" +this._trans_mat_result.m22, 32, 50 + 16*11);
119                                 g.drawString("[m23]" +this._trans_mat_result.m23, 32, 50 + 16*12);
120                         } else {
121                                 g.drawString("マーカー未検出:", 32, 100);
122                         }
123                 } catch (Exception e) {
124                         e.printStackTrace();
125                 }
126
127         }
128
129         private void startCapture()
130         {
131                 try {
132                         this._capture.start();
133                 } catch (Exception e) {
134                         e.printStackTrace();
135                 }
136         }
137
138         public static void main(String[] args)
139         {
140                 try {
141                         NyarToolkitLinkTest mainwin = new NyarToolkitLinkTest();
142                         mainwin.setVisible(true);
143                         mainwin.startCapture();
144                 } catch (Exception e) {
145                         e.printStackTrace();
146                 }
147
148         }
149
150 }