2 * PROJECT: NyARToolkit JMF sample program.
3 * --------------------------------------------------------------------------------
5 * Copyright (c) 2008 nyatla
6 * airmail(at)ebony.plala.or.jp
7 * http://nyatla.jp/nyartoolkit/
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.
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
27 package jp.nyatla.nyartoolkit.jmf.sample;
31 import javax.media.util.BufferToImage;
32 import javax.media.format.*;
34 import jp.nyatla.nyartoolkit.NyARException;
35 import jp.nyatla.nyartoolkit.jmf.utils.*;
38 import java.awt.event.WindowAdapter;
39 import java.awt.event.WindowEvent;
41 import jp.nyatla.nyartoolkit.core.labeling.rlelabeling.RleLabelFragmentInfoStack;
42 import jp.nyatla.nyartoolkit.core.param.NyARParam;
43 import jp.nyatla.nyartoolkit.core.raster.NyARBinRaster;
44 import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2bin.NyARRasterFilter_ARToolkitThreshold;
45 import jp.nyatla.nyartoolkit.core.squaredetect.INyARSquareContourDetector;
46 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquareContourDetector_Rle;
49 * VFM+ARToolkitテストプログラム
50 * カメラから取り込んだデータからマーカーを検出して、一致度と変換行列を表示します。
52 public class LabelingViewer extends Frame implements JmfCaptureListener
54 private static final long serialVersionUID = 6471434231970804953L;
56 private final String PARAM_FILE = "../../Data/camera_para.dat";
58 private JmfCaptureDevice _capture;
61 private JmfNyARRaster_RGB _raster;
63 public LabelingViewer() throws NyARException
65 setTitle("JmfCaptureTest");
66 setBounds(0, 0, 320 + 64, 240 + 64);
68 JmfCaptureDeviceList devlist=new JmfCaptureDeviceList();
69 this._capture=devlist.getDevice(0);
70 //JmfNyARRaster_RGBはYUVよりもRGBで高速に動作します。
71 if(!this._capture.setCaptureFormat(JmfCaptureDevice.PIXEL_FORMAT_RGB,320, 240,15f)){
72 if(!this._capture.setCaptureFormat(JmfCaptureDevice.PIXEL_FORMAT_YUV,320, 240,15f)){
73 throw new NyARException("キャプチャフォーマットが見つかりません");
76 this._capture.setOnCapture(this);
77 this.addWindowListener(new WindowAdapter() {
78 public void windowClosing(WindowEvent e)
84 NyARParam ar_param = new NyARParam();
85 ar_param.loadARParamFromFile(PARAM_FILE);
86 ar_param.changeScreenSize(320, 240);
87 this._raster = new JmfNyARRaster_RGB(320, 240,this._capture.getCaptureFormat());
88 this._detect=new NyARSquareContourDetector_Rle(ar_param.getDistortionFactor(),ar_param.getScreenSize());
89 this._filter = new NyARRasterFilter_ARToolkitThreshold(110, _raster.getBufferType());
93 private NyARSquareContourDetector_Rle _detect;
94 private NyARBinRaster _bi=new NyARBinRaster(320,240);
95 private NyARRasterFilter_ARToolkitThreshold _filter;
97 * detectMarkerのコールバック関数
99 private class DetectSquareCB implements INyARSquareContourDetector.DetectMarkerCallback
101 public DetectSquareCB()
107 * 発見した矩形のパターンを検査して、方位を考慮した頂点データを確保します。
109 public void onSquareDetect(INyARSquareContourDetector i_sender,int[] i_coordx,int[] i_coordy,int i_coor_num,int[] i_vertex_index) throws NyARException
114 public void onUpdateBuffer(Buffer i_buffer)
117 NyARBinRaster bin = this._bi;
118 //キャプチャしたバッファをラスタにセット
119 this._raster.setBuffer(i_buffer);
122 BufferToImage b2i = new BufferToImage((VideoFormat) i_buffer.getFormat());
123 Image img = b2i.createImage(i_buffer);
124 this._filter.doFilter(this._raster,bin);
126 Graphics g = getGraphics();
128 NyARParam param=new NyARParam();
129 param.loadARParamFromFile(PARAM_FILE);
130 param.changeScreenSize(320,240);
132 this._detect.detectMarkerCB(bin,new DetectSquareCB());
136 RleLabelFragmentInfoStack ls=this._detect._getFragmentStack();
137 for(int i=0;i<ls.getLength();i++){
138 RleLabelFragmentInfoStack.RleLabelFragmentInfo label=ls.getItem(i);
139 // if(label.area==0){break;}
140 Graphics g2=img.getGraphics();
141 g2.setColor(Color.RED);
142 g2.drawRect(label.clip_l,label.clip_t,label.clip_r-label.clip_l,label.clip_b-label.clip_t);
146 g.drawImage(img,50,50,null);
154 private void startCapture()
157 this._capture.start();
158 } catch (Exception e) {
163 public static void main(String[] args)
166 LabelingViewer mainwin = new LabelingViewer();
167 mainwin.setVisible(true);
168 mainwin.startCapture();
169 } catch (Exception e) {