OSDN Git Service

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