OSDN Git Service

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