OSDN Git Service

[backup]NyARToolkit
[nyartoolkit-and/nyartoolkit-and.git] / sample / jmf / jp / nyatla / nyartoolkit / jmf / sample / LabelingViewer.java
1 /* 
2  * PROJECT: NyARToolkit JMF 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 package jp.nyatla.nyartoolkit.jmf.sample;
28
29 import javax.media.*;
30
31 import javax.media.util.BufferToImage;
32 import javax.media.format.*;
33
34 import jp.nyatla.nyartoolkit.NyARException;
35 import jp.nyatla.nyartoolkit.jmf.utils.*;
36
37 import java.awt.*;
38 import java.awt.event.WindowAdapter;
39 import java.awt.event.WindowEvent;
40
41 import jp.nyatla.nyartoolkit.core.labeling.rlelabeling.RleLabelFragmentInfoStack;
42 import jp.nyatla.nyartoolkit.core.param.NyARParam;
43 import jp.nyatla.nyartoolkit.core.raster.NyARBinRaster;
44 import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin.NyARRasterFilter_ARToolkitThreshold;
45 import jp.nyatla.nyartoolkit.core.squaredetect.INyARSquareContourDetector;
46 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquareContourDetector_Rle;
47
48 /**
49  * VFM+ARToolkitテストプログラム
50  * カメラから取り込んだデータからマーカーを検出して、一致度と変換行列を表示します。
51  */
52 public class LabelingViewer extends Frame implements JmfCaptureListener
53 {
54         private static final long serialVersionUID = 6471434231970804953L;
55
56         private final String PARAM_FILE = "../../Data/camera_para.dat";
57
58         private JmfCaptureDevice _capture;
59
60
61         private JmfNyARRaster_RGB _raster;
62
63         public LabelingViewer() throws NyARException
64         {
65                 setTitle("JmfCaptureTest");
66                 setBounds(0, 0, 320 + 64, 240 + 64);
67                 //キャプチャの準備
68                 JmfCaptureDeviceList devlist=new JmfCaptureDeviceList();
69                 this._capture=devlist.getDevice(0);
70                 //JmfNyARRaster_RGBはYUVよりもRGBで高速に動作します。
71                 if(!this._capture.setCaptureFormat(JmfCaptureDevice.PIXEL_FORMAT_RGB,320, 240,15f)){
72                         if(!this._capture.setCaptureFormat(JmfCaptureDevice.PIXEL_FORMAT_YUV,320, 240,15f)){
73                                 throw new NyARException("キャプチャフォーマットが見つかりません");
74                         }               
75                 }
76                 this._capture.setOnCapture(this);
77                 this.addWindowListener(new WindowAdapter() {
78                         public void windowClosing(WindowEvent e)
79                         {
80                                 System.exit(0);
81                         }
82                 });
83                 //NyARToolkitの準備
84                 NyARParam ar_param = new NyARParam();
85                 ar_param.loadARParamFromFile(PARAM_FILE);
86                 ar_param.changeScreenSize(320, 240);
87                 this._raster = new JmfNyARRaster_RGB(320, 240,this._capture.getCaptureFormat());
88                 this._detect=new NyARSquareContourDetector_Rle(ar_param.getDistortionFactor(),ar_param.getScreenSize());
89                 this._filter    = new NyARRasterFilter_ARToolkitThreshold(110, _raster.getBufferType());
90                 //キャプチャイメージ用のラスタを準備
91                 return;
92         }
93         private NyARSquareContourDetector_Rle _detect;
94         private NyARBinRaster _bi=new NyARBinRaster(320,240);
95         private NyARRasterFilter_ARToolkitThreshold _filter;
96         /**
97          * detectMarkerのコールバック関数
98          */
99         private class DetectSquareCB implements INyARSquareContourDetector.DetectMarkerCallback
100         {
101                 public DetectSquareCB()
102                 {
103                         return;
104                 }
105                 /**
106                  * 矩形が見付かるたびに呼び出されます。
107                  * 発見した矩形のパターンを検査して、方位を考慮した頂点データを確保します。
108                  */
109                 public void onSquareDetect(INyARSquareContourDetector i_sender,int[] i_coordx,int[] i_coordy,int i_coor_num,int[] i_vertex_index) throws NyARException
110                 {                       
111                 }
112         }
113         
114         public void onUpdateBuffer(Buffer i_buffer)
115         {
116                 try {
117                         NyARBinRaster bin = this._bi;
118                         //キャプチャしたバッファをラスタにセット
119                         this._raster.setBuffer(i_buffer);
120
121                         //キャプチャしたイメージを表示用に加工
122                         BufferToImage b2i = new BufferToImage((VideoFormat) i_buffer.getFormat());
123                         Image img = b2i.createImage(i_buffer);
124                         this._filter.doFilter(this._raster,bin);
125
126                         Graphics g = getGraphics();
127                         
128                         NyARParam param=new NyARParam();
129                         param.loadARParamFromFile(PARAM_FILE);
130                         param.changeScreenSize(320,240);
131                         try{
132                                 this._detect.detectMarkerCB(bin,new DetectSquareCB());
133                         }catch(Exception e){
134                                 e.printStackTrace();
135                         }
136                         RleLabelFragmentInfoStack ls=this._detect._getFragmentStack();
137                         for(int i=0;i<ls.getLength();i++){
138                                 RleLabelFragmentInfoStack.RleLabelFragmentInfo label=ls.getItem(i);
139 //                              if(label.area==0){break;}
140                                 Graphics g2=img.getGraphics();
141                                 g2.setColor(Color.RED);
142                                 g2.drawRect(label.clip_l,label.clip_t,label.clip_r-label.clip_l,label.clip_b-label.clip_t);
143                         }               
144                         
145                         
146                         g.drawImage(img,50,50,null);
147                 }catch(Exception e)
148                 {
149                         e.printStackTrace();
150                 }
151
152         }
153
154         private void startCapture()
155         {
156                 try {
157                         this._capture.start();
158                 } catch (Exception e) {
159                         e.printStackTrace();
160                 }
161         }
162
163         public static void main(String[] args)
164         {
165                 try {
166                         LabelingViewer mainwin = new LabelingViewer();
167                         mainwin.setVisible(true);
168                         mainwin.startCapture();
169                 } catch (Exception e) {
170                         e.printStackTrace();
171                 }
172
173         }
174
175 }