OSDN Git Service

[TAG]NyARToolkit/2.5.0
[nyartoolkit-and/nyartoolkit-and.git] / tags / 2.5.0 / 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 import java.awt.event.WindowAdapter;
39 import java.awt.event.WindowEvent;
40
41 import jp.nyatla.nyartoolkit.core.*;
42 import jp.nyatla.nyartoolkit.core.param.NyARParam;
43 import jp.nyatla.nyartoolkit.core.transmat.*;
44 import jp.nyatla.nyartoolkit.detector.NyARSingleDetectMarker;
45
46 /**
47  * VFM+ARToolkitテストプログラム
48  * カメラから取り込んだデータからマーカーを検出して、一致度と変換行列を表示します。
49  */
50 public class NyarToolkitLinkTest extends Frame implements JmfCaptureListener
51 {
52         private static final long serialVersionUID = 6471434231970804953L;
53
54         private final String CARCODE_FILE = "../../Data/patt.hiro";
55
56         private final String PARAM_FILE = "../../Data/camera_para.dat";
57
58         private JmfCaptureDevice _capture;
59
60         private NyARSingleDetectMarker _nya;
61
62         private JmfNyARRaster_RGB _raster;
63
64         private NyARTransMatResult _trans_mat_result = new NyARTransMatResult();
65
66         public NyarToolkitLinkTest() throws NyARException
67         {
68                 setTitle("JmfCaptureTest");
69                 setBounds(0, 0, 320 + 64, 240 + 64);
70                 //キャプチャの準備
71                 JmfCaptureDeviceList devlist=new JmfCaptureDeviceList();
72                 this._capture=devlist.getDevice(0);
73                 //JmfNyARRaster_RGBはYUVよりもRGBで高速に動作します。
74                 if(!this._capture.setCaptureFormat(JmfCaptureDevice.PIXEL_FORMAT_RGB,320, 240,15f)){
75                         if(!this._capture.setCaptureFormat(JmfCaptureDevice.PIXEL_FORMAT_YUV,320, 240,15f)){
76                                 throw new NyARException("キャプチャフォーマットが見つかりません");
77                         }               
78                 }
79                 this._capture.setOnCapture(this);
80                 this.addWindowListener(new WindowAdapter() {
81                         public void windowClosing(WindowEvent e)
82                         {
83                                 System.exit(0);
84                         }
85                 });
86                 //NyARToolkitの準備
87                 NyARParam ar_param = new NyARParam();
88                 NyARCode ar_code = new NyARCode(16, 16);
89                 ar_param.loadARParamFromFile(PARAM_FILE);
90                 ar_param.changeScreenSize(320, 240);
91                 this._raster = new JmfNyARRaster_RGB(320, 240,this._capture.getCaptureFormat());
92                 this._nya = new NyARSingleDetectMarker(ar_param, ar_code, 80.0,this._raster.getBufferType());
93                 ar_code.loadARPattFromFile(CARCODE_FILE);
94                 //キャプチャイメージ用のラスタを準備
95                 return;
96         }
97
98         public void onUpdateBuffer(Buffer i_buffer)
99         {
100                 try {
101                         //キャプチャしたバッファをラスタにセット
102                         this._raster.setBuffer(i_buffer);
103
104                         //キャプチャしたイメージを表示用に加工
105                         BufferToImage b2i = new BufferToImage((VideoFormat) i_buffer.getFormat());
106                         Image img = b2i.createImage(i_buffer);
107
108                         Graphics g = getGraphics();
109
110                         //マーカー検出
111                         boolean is_marker_exist = this._nya.detectMarkerLite(this._raster, 110);
112                         if (is_marker_exist) {
113                                 //変換行列を取得
114                                 this._nya.getTransmationMatrix(this._trans_mat_result);
115                         }
116                         //情報を画面に書く
117                         Graphics image_g=img.getGraphics();
118                         image_g.setColor(Color.red);
119                         if (is_marker_exist) {
120                                 image_g.drawString("マーカー検出:" + this._nya.getConfidence(), 4, 20);
121                                 image_g.drawString("[m00]" +this._trans_mat_result.m00, 4, 20 + 16*1);
122                                 image_g.drawString("[m01]" +this._trans_mat_result.m01, 4, 20 + 16*2);
123                                 image_g.drawString("[m02]" +this._trans_mat_result.m02, 4, 20 + 16*3);
124                                 image_g.drawString("[m03]" +this._trans_mat_result.m03, 4, 20 + 16*4);
125                                 image_g.drawString("[m10]" +this._trans_mat_result.m10, 4, 20 + 16*5);
126                                 image_g.drawString("[m11]" +this._trans_mat_result.m11, 4, 20 + 16*6);
127                                 image_g.drawString("[m12]" +this._trans_mat_result.m12, 4, 20 + 16*7);
128                                 image_g.drawString("[m13]" +this._trans_mat_result.m13, 4, 20 + 16*8);
129                                 image_g.drawString("[m20]" +this._trans_mat_result.m20, 4, 20 + 16*9);
130                                 image_g.drawString("[m21]" +this._trans_mat_result.m21, 4, 20 + 16*10);
131                                 image_g.drawString("[m22]" +this._trans_mat_result.m22, 4, 20 + 16*11);
132                                 image_g.drawString("[m23]" +this._trans_mat_result.m23, 4, 20 + 16*12);
133                         } else {
134                                 g.drawString("マーカー未検出:", 32, 100);
135                         }
136                         g.drawImage(img, 32, 32, this);
137                 } catch (Exception e) {
138                         e.printStackTrace();
139                 }
140
141         }
142
143         private void startCapture()
144         {
145                 try {
146                         this._capture.start();
147                 } catch (Exception e) {
148                         e.printStackTrace();
149                 }
150         }
151
152         public static void main(String[] args)
153         {
154                 try {
155                         NyarToolkitLinkTest mainwin = new NyarToolkitLinkTest();
156                         mainwin.setVisible(true);
157                         mainwin.startCapture();
158                 } catch (Exception e) {
159                         e.printStackTrace();
160                 }
161
162         }
163
164 }