OSDN Git Service

git-svn-id: http://svn.sourceforge.jp/svnroot/nyartoolkit/NyARToolkit/trunk@711 7cac0...
[nyartoolkit-and/nyartoolkit-and.git] / utils / jmf / test / jp / nyatla / nyartoolkit / jmf / utils / test / 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.utils.test;
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.NyARRleLabelFragmentInfoPtrStack;
42 import jp.nyatla.nyartoolkit.core.param.NyARParam;
43 import jp.nyatla.nyartoolkit.core.raster.*;
44 import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2gs.NyARRasterFilter_Rgb2Gs_RgbAve;
45 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquareContourDetector_Rle;
46 import jp.nyatla.nyartoolkit.core.types.*;
47
48 /**
49  * VFM+ARToolkitテストプログラム
50  * カメラから取り込んだデータからマーカーを検出して、一致度と変換行列を表示します。
51  */
52 public class LabelingViewer extends Frame implements JmfCaptureListener
53 {
54         class SquareDetector extends NyARSquareContourDetector_Rle
55         {
56                 public SquareDetector(NyARIntSize i_size) throws NyARException
57                 {
58                         super(i_size);
59                 }
60                 protected void onSquareDetect(NyARIntPoint2d[] i_coord,int i_coor_num,int[] i_vertex_index,Object i_param) throws NyARException
61                 {
62                         
63                 }
64         }
65         private static final long serialVersionUID = 6471434231970804953L;
66
67         private final String PARAM_FILE = "../../Data/camera_para.dat";
68
69         private JmfCaptureDevice _capture;
70
71
72         private JmfNyARRaster_RGB _raster;
73
74         public LabelingViewer() throws NyARException
75         {
76                 setTitle("JmfCaptureTest");
77                 setBounds(0, 0, 320 + 64, 240 + 64);
78                 //キャプチャの準備
79                 JmfCaptureDeviceList devlist=new JmfCaptureDeviceList();
80                 this._capture=devlist.getDevice(0);
81                 //JmfNyARRaster_RGBはYUVよりもRGBで高速に動作します。
82                 if(!this._capture.setCaptureFormat(JmfCaptureDevice.PIXEL_FORMAT_RGB,320, 240,15f)){
83                         if(!this._capture.setCaptureFormat(JmfCaptureDevice.PIXEL_FORMAT_YUV,320, 240,15f)){
84                                 throw new NyARException("キャプチャフォーマットが見つかりません");
85                         }               
86                 }
87                 this._capture.setOnCapture(this);
88                 this.addWindowListener(new WindowAdapter() {
89                         public void windowClosing(WindowEvent e)
90                         {
91                                 System.exit(0);
92                         }
93                 });
94                 //NyARToolkitの準備
95                 NyARParam ar_param = new NyARParam();
96                 ar_param.loadARParamFromFile(PARAM_FILE);
97                 ar_param.changeScreenSize(320, 240);
98                 this._raster = new JmfNyARRaster_RGB(320, 240,this._capture.getCaptureFormat());
99                 this._detect=new SquareDetector(ar_param.getScreenSize());
100                 this._filter    = new NyARRasterFilter_Rgb2Gs_RgbAve(_raster.getBufferType());
101                 //キャプチャイメージ用のラスタを準備
102                 return;
103         }
104         private NyARSquareContourDetector_Rle _detect;
105         private NyARGrayscaleRaster _bi=new NyARGrayscaleRaster(320,240);
106         private NyARRasterFilter_Rgb2Gs_RgbAve _filter;
107
108
109         
110         public void onUpdateBuffer(Buffer i_buffer)
111         {
112                 try {
113                         NyARGrayscaleRaster gs = this._bi;
114                         //キャプチャしたバッファをラスタにセット
115                         this._raster.setBuffer(i_buffer);
116
117                         //キャプチャしたイメージを表示用に加工
118                         BufferToImage b2i = new BufferToImage((VideoFormat) i_buffer.getFormat());
119                         Image img = b2i.createImage(i_buffer);
120                         this._filter.doFilter(this._raster,gs);
121
122                         Graphics g = getGraphics();
123                         
124                         NyARParam param=new NyARParam();
125                         param.loadARParamFromFile(PARAM_FILE);
126                         param.changeScreenSize(320,240);
127                         try{
128                                 NyARIntRect rect=new NyARIntRect();
129                                 rect.x=100;rect.y=100;rect.w=220;rect.h=140;
130                                 this._detect.detectMarker(gs,rect,110,null);
131                         }catch(Exception e){
132                                 e.printStackTrace();
133                         }
134                         NyARRleLabelFragmentInfoPtrStack ls=(NyARRleLabelFragmentInfoPtrStack)this._detect._probe()[0];
135                         for(int i=0;i<ls.getLength();i++){
136                                 NyARRleLabelFragmentInfoPtrStack.RleLabelFragmentInfo label=ls.getItem(i);
137 //                              if(label.area==0){break;}
138                                 Graphics g2=img.getGraphics();
139                                 g2.setColor(Color.RED);
140                                 g2.drawRect(label.clip_l,label.clip_t,label.clip_r-label.clip_l,label.clip_b-label.clip_t);
141                         }               
142                         
143                         
144                         g.drawImage(img,50,50,null);
145                 }catch(Exception e)
146                 {
147                         e.printStackTrace();
148                 }
149
150         }
151
152         private void startCapture()
153         {
154                 try {
155                         this._capture.start();
156                 } catch (Exception e) {
157                         e.printStackTrace();
158                 }
159         }
160
161         public static void main(String[] args)
162         {
163                 try {
164                         LabelingViewer mainwin = new LabelingViewer();
165                         mainwin.setVisible(true);
166                         mainwin.startCapture();
167                 } catch (Exception e) {
168                         e.printStackTrace();
169                 }
170
171         }
172
173 }