OSDN Git Service

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