OSDN Git Service

[バックアップ]nyartoolkit
[nyartoolkit-and/nyartoolkit-and.git] / sample / qt / jp / nyatla / nyartoolkit / qt / 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.qt.sample;
9
10 import jp.nyatla.nyartoolkit.NyARException;
11 import jp.nyatla.nyartoolkit.qt.utils.*;
12
13 import java.awt.*;
14
15 import jp.nyatla.nyartoolkit.core.*;
16 import jp.nyatla.nyartoolkit.detector.*;
17
18
19 public class NyarToolkitLinkTest extends Frame implements QtCaptureListener
20 {
21     private final String CARCODE_FILE ="../../Data/patt.hiro";
22     private final String PARAM_FILE   ="../../Data/camera_para.dat";
23     private QtCameraCapture capture;
24     private NyARSingleDetectMarker nya;
25     private QtNyARRaster_RGB raster;
26     private NyARTransMatResult trans_mat_result=new NyARTransMatResult();
27     
28     public NyarToolkitLinkTest() throws NyARException,NyARException
29     {
30         setTitle("QtCaptureTest");
31         setBounds(0,0,320+64,240+64);     
32         //キャプチャの準備
33         capture=new QtCameraCapture(320,240,30f);
34         capture.setCaptureListener(this);
35         
36         //NyARToolkitの準備
37         NyARParam ar_param=new NyARParam();
38         NyARCode ar_code  =new NyARCode(16,16);
39         ar_param.loadFromARFile(PARAM_FILE);
40         ar_param.changeSize(320,240);
41         nya=new NyARSingleDetectMarker(ar_param,ar_code,80.0);
42         ar_code.loadFromARFile(CARCODE_FILE);
43         //キャプチャイメージ用のラスタを準備
44         raster=new QtNyARRaster_RGB(320,240);
45     }
46
47
48     
49     public void onUpdateBuffer(byte[] pixels)
50     {
51         try{
52             //キャプチャしたバッファをラスタにセット
53             raster.setBuffer(pixels);
54
55             //キャプチャしたイメージを表示用に加工
56             Image img= raster.createImage();
57
58             Graphics g = getGraphics();            
59             double[][] atm=null;
60
61             //マーカー検出
62             boolean is_marker_exist=nya.detectMarkerLite(raster,100);
63             if(is_marker_exist){
64                 //変換行列を取得
65                 nya.getTransmationMatrix(this.trans_mat_result);
66                 atm=this.trans_mat_result.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 }