OSDN Git Service

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