OSDN Git Service

[バックアップ]NyARToolkit
[nyartoolkit-and/nyartoolkit-and.git] / sample / toys / jp / nyatla / nyartoolkit / toys / qrcode / SingleQrTest.java
1 /* このソースは実験用のソースです。
2  * 動いたり動かなかったりします。
3  * 
4  */
5 package jp.nyatla.nyartoolkit.toys.qrcode;
6
7 import javax.media.*;
8
9 import javax.media.util.BufferToImage;
10 import javax.media.format.*;
11
12 import jp.nyatla.nyartoolkit.NyARException;
13 import jp.nyatla.nyartoolkit.jmf.utils.*;
14
15 import jp.nyatla.nyartoolkit.core.*;
16
17 import java.awt.*;
18
19 import jp.nyatla.nyartoolkit.core.labeling.*;
20 import jp.nyatla.nyartoolkit.core.param.*;
21 import jp.nyatla.nyartoolkit.core.raster.*;
22 import jp.nyatla.nyartoolkit.core.rasterfilter.*;
23 import jp.nyatla.nyartoolkit.core2.rasterfilter.gs2bin.*;
24 import jp.nyatla.nyartoolkit.core2.rasterfilter.rgb2gs.*;
25 import jp.nyatla.utils.j2se.LabelingBufferdImage;
26 import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin.*;
27 import jp.nyatla.nyartoolkit.core.types.*;
28
29
30
31
32 public class SingleQrTest extends Frame implements JmfCaptureListener
33 {
34         private final String camera_file = "../../Data/camera_para.dat";
35
36         private JmfNyARRaster_RGB _raster;
37
38         private JmfCameraCapture capture;
39         private NyARParam ap;
40         public SingleQrTest() throws NyARException, NyARException
41         {
42                 setBounds(0, 0, 640 + 64, 720 + 64);
43                 // キャプチャの準備
44                 capture = new JmfCameraCapture(320, 240, 30f, JmfCameraCapture.PIXEL_FORMAT_RGB);
45                 capture.setCaptureListener(this);
46
47                 // キャプチャイメージ用のラスタを準備
48                 this._raster = new JmfNyARRaster_RGB(320, 240);
49                 
50                 // AR用カメラパラメタファイルをロード
51                 ap = new NyARParam();
52                 ap.loadARParamFromFile(camera_file);
53                 ap.changeScreenSize(320, 240);          
54                 
55                 
56         }
57
58         // そのラベルが特徴点候補か返す。
59
60         private NyARBinRaster _binraster1 = new NyARBinRaster(320, 240);
61
62         private NyARGrayscaleRaster _gsraster1 = new NyARGrayscaleRaster(320, 240);
63
64
65         private LabelingBufferdImage _bimg = new LabelingBufferdImage(320, 240);
66
67
68         public void onUpdateBuffer(Buffer i_buffer)
69         {
70                 NyARRasterFilter_QrAreaAverage gs2bin=new NyARRasterFilter_QrAreaAverage();
71
72                 try {
73                         // キャプチャしたバッファをラスタにセット
74                         _raster.setBuffer(i_buffer);
75
76                         Graphics g = getGraphics();
77                         // キャプチャ画像
78                         BufferToImage b2i = new BufferToImage((VideoFormat) i_buffer.getFormat());
79                         Image img = b2i.createImage(i_buffer);
80                         this.getGraphics().drawImage(img, 32, 32, this);
81
82                         // 画像1
83                         INyARRasterFilter_RgbToGs filter_rgb2gs = new NyARRasterFilter_RgbAve();
84 //                      INyARRasterFilter_RgbToGs filter_rgb2gs = new NyARRasterFilter_RgbMul();
85                         
86                         filter_rgb2gs.doFilter(_raster, _gsraster1);
87                         this._bimg.drawImage(this._gsraster1);
88                         this.getGraphics().drawImage(this._bimg, 32 + 320, 32, 320 + 320 + 32, 240 + 32, 0, 240, 320, 0, this);
89                         
90
91                         // 画像2
92                         gs2bin.doFilter(_gsraster1, _binraster1);
93                         this._bimg.drawImage(_binraster1);
94                         this.getGraphics().drawImage(this._bimg, 32, 32 + 240, 320 + 32, 240 + 32 + 240, 0, 240, 320, 0, this);
95
96                         // 画像3
97                         NyARLabelingImage limage = new NyARLabelingImage(320, 240);
98                         NyARLabeling_ARToolKit labeling = new NyARLabeling_ARToolKit();
99                         labeling.attachDestination(limage);
100                         labeling.labeling(_binraster1);
101                         this._bimg.drawImage(this._gsraster1);
102
103                         NyARSquareStack stack = new NyARSquareStack(100);
104                         NyARQrCodeDetector detect = new NyARQrCodeDetector(ap.getDistortionFactor(), new NyARIntSize(320,240));
105 //                      detect.bimg=this._bimg;
106
107                         detect.detectMarker(_binraster1, stack);
108                         for (int i = 0; i < stack.getLength(); i++) {
109                                 NyARSquare[] square_ptr = (NyARSquare[]) stack.getArray();
110                                 int d=square_ptr[i].direction;
111                                 int[] xp=new int[4]; 
112                                 int[] yp=new int[4]; 
113                                 for(int i2=0;i2<4;i2++){
114                                         xp[i2]=square_ptr[i].imvertex[(i2+d)%4].x;
115                                         yp[i2]=square_ptr[i].imvertex[(i2+d)%4].y;
116                                 }
117                                 Graphics g2=this._bimg.getGraphics();
118                                 g2.setColor(Color.RED);
119                                 g2.drawPolygon(xp, yp,3);
120                                 g2.setColor(Color.CYAN);
121                                 g2.drawRect(square_ptr[i].imvertex[d].x, square_ptr[i].imvertex[d].y,5,5);                              
122                         }
123                         this.getGraphics().drawImage(this._bimg, 32 + 320, 32 + 240, 320 + 32 + 320, 240 + 32 + 240, 0, 240, 320, 0, this);
124
125                         // 画像3
126                         // threshold.debugDrawHistgramMap(_workraster, _workraster2);
127                         // this._bimg2.setImage(this._workraster2);
128                         // this.getGraphics().drawImage(this._bimg2, 32+320, 32+240,320+32+320,240+32+240,0,240,320,0, this);
129
130                         // 画像4
131                         // NyARRasterThresholdAnalyzer_SlidePTile threshold=new NyARRasterThresholdAnalyzer_SlidePTile(15);
132                         // threshold.analyzeRaster(_gsraster1);
133                         // filter_gs2bin=new NyARRasterFilter_AreaAverage();
134                         // filter_gs2bin.doFilter(_gsraster1, _binraster1);
135                         // this._bimg.drawImage(_binraster1);
136
137                         // NyARRasterDetector_QrCodeEdge detector=new NyARRasterDetector_QrCodeEdge(10000);
138                         // detector.analyzeRaster(_binraster1);
139
140                         // this._bimg.overlayData(detector.geResult());
141
142                         // this.getGraphics().drawImage(this._bimg, 32, 32+480,320+32,480+32+240,0,240,320,0, this);
143                         // 画像5
144
145                         /*
146                          * threshold2.debugDrawHistgramMap(_workraster, _workraster2); this._bimg2.drawImage(this._workraster2); this.getGraphics().drawImage(this._bimg2,
147                          * 32+320, 32+480,320+32+320,480+32+240,0,240,320,0, this);
148                          */
149
150                         // this.getGraphics().drawImage(this._bimg, 32, 32, this);
151
152                 } catch (Exception e) {
153                         e.printStackTrace();
154                 }
155
156         }
157
158         private INyARLabeling labelingFactory(int i_idx)
159         {
160                 // switch(i_idx){
161                 // case 0:{NyARLabeling_ARToolKit l=new NyARLabeling_ARToolKit();l.setThresh(4);return l;}
162                 // case 1:{return new NyLineLabeling();}
163                 // }
164                 return null;
165
166         }
167
168         private void startCapture()
169         {
170                 try {
171                         capture.start();
172                 } catch (Exception e) {
173                         e.printStackTrace();
174                 }
175         }
176
177         public static void main(String[] args)
178         {
179                 try {
180                         SingleQrTest mainwin = new SingleQrTest();
181                         mainwin.setVisible(true);
182                         mainwin.startCapture();
183                 } catch (Exception e) {
184                         e.printStackTrace();
185                 }
186
187         }
188
189 }