5 package jp.nyatla.nyartoolkit.sandbox.qrcode;
9 import javax.media.util.BufferToImage;
10 import javax.media.format.*;
12 import jp.nyatla.nyartoolkit.NyARException;
13 import jp.nyatla.nyartoolkit.jmf.utils.*;
14 import jp.nyatla.nyartoolkit.utils.j2se.LabelingBufferdImage;
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.*;
34 public class SingleQrTest extends Frame implements JmfCaptureListener
36 private final String camera_file = "../../Data/camera_para.dat";
38 private JmfNyARRaster_RGB _raster;
40 private JmfCaptureDevice capture;
42 public SingleQrTest() throws NyARException, NyARException
44 setBounds(0, 0, 640 + 64, 720 + 64);
46 JmfCaptureDeviceList list=new JmfCaptureDeviceList();
47 capture=list.getDevice(0);
48 capture.setCaptureFormat(320,240,30.0f);
49 capture.setOnCapture(this);
52 this._raster = new JmfNyARRaster_RGB(320, 240,capture.getCaptureFormat());
56 ap.loadARParamFromFile(camera_file);
57 ap.changeScreenSize(320, 240);
62 private NyARBinRaster _binraster1 = new NyARBinRaster(320, 240);
64 private NyARGrayscaleRaster _gsraster1 = new NyARGrayscaleRaster(320, 240);
65 private INyARRasterThresholdAnalyzer _tha=new NyARRasterThresholdAnalyzer_DiffHistgram();
67 private LabelingBufferdImage _bimg = new LabelingBufferdImage(320, 240);
70 public void onUpdateBuffer(Buffer i_buffer)
74 // キャプチャしたバッファをラスタにセット
75 _raster.setBuffer(i_buffer);
77 Graphics g = getGraphics();
79 BufferToImage b2i = new BufferToImage((VideoFormat) i_buffer.getFormat());
80 Image img = b2i.createImage(i_buffer);
81 this.getGraphics().drawImage(img, 32, 32, this);
84 INyARRasterFilter_Rgb2Gs filter_rgb2gs = new NyARRasterFilter_AveAdd();
85 // INyARRasterFilter_RgbToGs filter_rgb2gs = new NyARRasterFilter_RgbMul();
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());
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);
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);
105 NyARSquareStack stack = new NyARSquareStack(100);
106 NyARQrCodeDetector detect = new NyARQrCodeDetector(ap.getDistortionFactor(), new NyARIntSize(320,240));
107 // detect.bimg=this._bimg;
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;
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;
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);
125 this.getGraphics().drawImage(this._bimg, 32 + 320, 32 + 240, 320 + 32 + 320, 240 + 32 + 240, 0, 240, 320, 0, this);
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);
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);
139 // NyARRasterDetector_QrCodeEdge detector=new NyARRasterDetector_QrCodeEdge(10000);
140 // detector.analyzeRaster(_binraster1);
142 // this._bimg.overlayData(detector.geResult());
144 // this.getGraphics().drawImage(this._bimg, 32, 32+480,320+32,480+32+240,0,240,320,0, this);
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);
152 // this.getGraphics().drawImage(this._bimg, 32, 32, this);
154 } catch (Exception e) {
160 private void startCapture()
164 } catch (Exception e) {
169 public static void main(String[] args)
172 SingleQrTest mainwin = new SingleQrTest();
173 mainwin.setVisible(true);
174 mainwin.startCapture();
175 } catch (Exception e) {