OSDN Git Service

79489c56980c754d517e63059f2d9b445f66a085
[nyartoolkit-and/nyartoolkit-and.git] / sample / qt / jp / nyatla / nyartoolkit / qt / sample / NyarToolkitLinkTest.java
1 /* 
2  * PROJECT: NyARToolkit QuickTime 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
28
29 package jp.nyatla.nyartoolkit.qt.sample;
30
31 import jp.nyatla.nyartoolkit.NyARException;
32 import jp.nyatla.nyartoolkit.qt.utils.*;
33 import java.awt.*;
34 import jp.nyatla.nyartoolkit.core.*;
35 import jp.nyatla.nyartoolkit.core.param.NyARParam;
36 import jp.nyatla.nyartoolkit.core.transmat.NyARTransMatResult;
37 import jp.nyatla.nyartoolkit.detector.NyARSingleDetectMarker;
38 /**
39  * VFM+ARToolkitテストプログラム
40  * カメラから取り込んだデータからマーカーを検出して、一致度と変換行列を表示します。
41  */
42 public class NyarToolkitLinkTest extends Frame implements QtCaptureListener
43 {
44         private final String CARCODE_FILE = "../../Data/patt.hiro";
45
46         private final String PARAM_FILE = "../../Data/camera_para.dat";
47
48         private QtCameraCapture capture;
49
50         private NyARSingleDetectMarker nya;
51
52         private QtNyARRaster_RGB raster;
53
54         private NyARTransMatResult trans_mat_result = new NyARTransMatResult();
55
56         public NyarToolkitLinkTest() throws NyARException, NyARException
57         {
58                 setTitle("QtCaptureTest");
59                 setBounds(0, 0, 320 + 64, 240 + 64);
60                 //キャプチャの準備
61                 capture = new QtCameraCapture(320, 240, 30f);
62                 capture.setCaptureListener(this);
63
64                 //NyARToolkitの準備
65                 NyARParam ar_param = new NyARParam();
66                 NyARCode ar_code = new NyARCode(16, 16);
67                 ar_param.loadARParamFromFile(PARAM_FILE);
68                 ar_param.changeScreenSize(320, 240);
69                 nya = new NyARSingleDetectMarker(ar_param, ar_code, 80.0);
70                 ar_code.loadARPattFromFile(CARCODE_FILE);
71                 //キャプチャイメージ用のラスタを準備
72                 raster = new QtNyARRaster_RGB(320, 240);
73         }
74
75         public void onUpdateBuffer(byte[] pixels)
76         {
77                 try {
78                         //キャプチャしたバッファをラスタにセット
79                         raster.setBuffer(pixels);
80
81                         //キャプチャしたイメージを表示用に加工
82                         Image img = raster.createImage();
83
84                         Graphics g = getGraphics();
85
86                         //マーカー検出
87                         boolean is_marker_exist = nya.detectMarkerLite(raster, 100);
88                         if (is_marker_exist) {
89                                 //変換行列を取得
90                                 nya.getTransmationMatrix(this.trans_mat_result);
91                         }
92                         //情報を画面に書く       
93                         g.drawImage(img, 32, 32, this);
94                         if (is_marker_exist) {
95                                 g.drawString("マーカー検出:" + nya.getConfidence(), 32, 50);
96                                 g.drawString("[m00]" + this.trans_mat_result.m00, 32, 50 + 16 * 1);
97                                 g.drawString("[m01]" + this.trans_mat_result.m01, 32, 50 + 16 * 2);
98                                 g.drawString("[m02]" + this.trans_mat_result.m02, 32, 50 + 16 * 3);
99                                 g.drawString("[m03]" + this.trans_mat_result.m03, 32, 50 + 16 * 4);
100                                 g.drawString("[m10]" + this.trans_mat_result.m10, 32, 50 + 16 * 5);
101                                 g.drawString("[m11]" + this.trans_mat_result.m11, 32, 50 + 16 * 6);
102                                 g.drawString("[m12]" + this.trans_mat_result.m12, 32, 50 + 16 * 7);
103                                 g.drawString("[m13]" + this.trans_mat_result.m13, 32, 50 + 16 * 8);
104                                 g.drawString("[m20]" + this.trans_mat_result.m20, 32, 50 + 16 * 9);
105                                 g.drawString("[m21]" + this.trans_mat_result.m21, 32, 50 + 16 * 10);
106                                 g.drawString("[m22]" + this.trans_mat_result.m22, 32, 50 + 16 * 11);
107                                 g.drawString("[m23]" + this.trans_mat_result.m23, 32, 50 + 16 * 12);
108
109                         } else {
110                                 g.drawString("マーカー未検出:", 32, 100);
111                         }
112                 } catch (Exception e) {
113                         e.printStackTrace();
114                 }
115
116         }
117
118         private void startCapture()
119         {
120                 try {
121                         capture.start();
122                 } catch (Exception e) {
123                         e.printStackTrace();
124                 }
125         }
126
127         public static void main(String[] args)
128         {
129                 try {
130                         NyarToolkitLinkTest mainwin = new NyarToolkitLinkTest();
131                         mainwin.setVisible(true);
132                         mainwin.startCapture();
133                 } catch (Exception e) {
134                         e.printStackTrace();
135                 }
136
137         }
138
139 }