OSDN Git Service

[リリース]NyARToolkit 0.5
[nyartoolkit-and/nyartoolkit-and.git] / sample / jmf / NyarToolkitLinkTest.java
1 /**
2  * VFM+ARToolkitテストプログラム
3  * カメラから取り込んだデータからマーカーを検出して、一致度と変換行列を表示します。
4  * (c)2008 R.iizuka
5  * airmail(at)ebony.plala.or.jp
6  * http://nyatla.jp/
7  */
8 import javax.media.*;
9
10 import javax.media.util.BufferToImage;
11 import javax.media.format.*;
12
13 import jp.nyatla.nyartoolkit.NyARException;
14 import jp.nyatla.nyartoolkit.jmf.*;
15
16 import java.awt.*;
17
18 import jp.nyatla.nyartoolkit.core.*;
19 import jp.nyatla.nyartoolkit.detector.*;
20 import jp.nyatla.nyartoolkit.core.raster.*;
21
22
23
24
25
26 public class NyarToolkitLinkTest extends Frame implements JmfCaptureListener
27 {
28     private final String CARCODE_FILE ="../../Data/patt.hiro";
29     private final String PARAM_FILE   ="../../Data/camera_para.dat";
30     private JmfCameraCapture capture;
31     NyARSingleDetectMarker nya;
32
33     public NyarToolkitLinkTest() throws NyARException,NyARException
34     {
35         setTitle("JmfCaptureTest");
36         setBounds(0,0,320+64,240+64);     
37         //キャプチャの準備
38         capture=new JmfCameraCapture(320,240,30f,JmfCameraCapture.PIXCEL_FORMAT_RGB);
39         capture.setCaptureListener(this);
40         
41         //NyARToolkitの準備
42         NyARParam ar_param=new NyARParam();
43         NyARCode ar_code  =new NyARCode(16,16);
44         ar_param.loadFromARFile(PARAM_FILE);
45         ar_param.changeSize(320,240);
46         nya=new NyARSingleDetectMarker(ar_param,ar_code,80.0);
47         ar_code.LoadFromARFile(CARCODE_FILE);
48     }
49
50
51
52     public void onUpdateBuffer(Buffer i_buffer)
53     {
54         try{
55             //キャプチャしたイメージを加工
56             BufferToImage b2i=new BufferToImage((VideoFormat)i_buffer.getFormat());
57             Image img=b2i.createImage(i_buffer);
58             Graphics g = getGraphics();
59             NyARRaster_RGB ra=NyARRaster_RGB.wrap((byte[])i_buffer.getData(), 320, 240);
60             //i_buffer.
61             boolean is_marker_exist=nya.detectMarkerLite(ra,100);
62             
63             double[][] atm=null;
64             if(is_marker_exist){
65                 //変換行列を取得
66                 atm=nya.getTransmationMatrix().getArray();
67             }
68             //情報を画面に書く       
69             g.drawImage(img, 32, 32,this);
70             if(is_marker_exist){
71                 g.drawString("マーカー検出:"+nya.getConfidence(),32,50);
72                 for(int i=0;i<3;i++){
73                     for(int i2=0;i2<4;i2++){
74                         g.drawString("["+i+"]["+i2+"]"+atm[i][i2],32,50+(1+i2*3+i)*16);
75                     }
76                     
77                 }
78             }else{
79                 g.drawString("マーカー未検出:",32,100);
80             }
81         }catch(Exception e){
82             e.printStackTrace();
83         }
84        
85         
86         
87         
88     }
89     private void startCapture()
90     {
91         try{
92             capture.start();
93         }catch(Exception e){
94             e.printStackTrace();
95         }
96     }
97     public static void main(String[] args) {
98         try{
99             NyarToolkitLinkTest mainwin = new NyarToolkitLinkTest();
100             mainwin.setVisible(true);
101             mainwin.startCapture();
102         }catch(Exception e){
103             e.printStackTrace();
104         }
105         
106     }
107
108 }