OSDN Git Service

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