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.utils.test;
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.NyARRleLabelFragmentInfoPtrStack;
42 import jp.nyatla.nyartoolkit.core.param.NyARParam;
43 import jp.nyatla.nyartoolkit.core.raster.*;
44 import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2gs.NyARRasterFilter_Rgb2Gs_RgbAve;
45 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquareContourDetector_Rle;
46 import jp.nyatla.nyartoolkit.core.types.*;
49 * VFM+ARToolkitテストプログラム
50 * カメラから取り込んだデータからマーカーを検出して、一致度と変換行列を表示します。
52 public class LabelingViewer extends Frame implements JmfCaptureListener
54 class SquareDetector extends NyARSquareContourDetector_Rle
56 public SquareDetector(NyARIntSize i_size) throws NyARException
60 protected void onSquareDetect(NyARIntPoint2d[] i_coord,int i_coor_num,int[] i_vertex_index,Object i_param) throws NyARException
65 private static final long serialVersionUID = 6471434231970804953L;
67 private final String PARAM_FILE = "../../Data/camera_para.dat";
69 private JmfCaptureDevice _capture;
72 private JmfNyARRaster_RGB _raster;
74 public LabelingViewer() throws NyARException
76 setTitle("JmfCaptureTest");
77 setBounds(0, 0, 320 + 64, 240 + 64);
79 JmfCaptureDeviceList devlist=new JmfCaptureDeviceList();
80 this._capture=devlist.getDevice(0);
81 //JmfNyARRaster_RGBはYUVよりもRGBで高速に動作します。
82 if(!this._capture.setCaptureFormat(JmfCaptureDevice.PIXEL_FORMAT_RGB,320, 240,15f)){
83 if(!this._capture.setCaptureFormat(JmfCaptureDevice.PIXEL_FORMAT_YUV,320, 240,15f)){
84 throw new NyARException("キャプチャフォーマットが見つかりません");
87 this._capture.setOnCapture(this);
88 this.addWindowListener(new WindowAdapter() {
89 public void windowClosing(WindowEvent e)
95 NyARParam ar_param = new NyARParam();
96 ar_param.loadARParamFromFile(PARAM_FILE);
97 ar_param.changeScreenSize(320, 240);
98 this._raster = new JmfNyARRaster_RGB(320, 240,this._capture.getCaptureFormat());
99 this._detect=new SquareDetector(ar_param.getScreenSize());
100 this._filter = new NyARRasterFilter_Rgb2Gs_RgbAve(_raster.getBufferType());
104 private NyARSquareContourDetector_Rle _detect;
105 private NyARGrayscaleRaster _bi=new NyARGrayscaleRaster(320,240);
106 private NyARRasterFilter_Rgb2Gs_RgbAve _filter;
110 public void onUpdateBuffer(Buffer i_buffer)
113 NyARGrayscaleRaster gs = this._bi;
114 //キャプチャしたバッファをラスタにセット
115 this._raster.setBuffer(i_buffer);
118 BufferToImage b2i = new BufferToImage((VideoFormat) i_buffer.getFormat());
119 Image img = b2i.createImage(i_buffer);
120 this._filter.doFilter(this._raster,gs);
122 Graphics g = getGraphics();
124 NyARParam param=new NyARParam();
125 param.loadARParamFromFile(PARAM_FILE);
126 param.changeScreenSize(320,240);
128 NyARIntRect rect=new NyARIntRect();
129 rect.x=100;rect.y=100;rect.w=220;rect.h=140;
130 this._detect.detectMarker(gs,rect,110,null);
134 NyARRleLabelFragmentInfoPtrStack ls=(NyARRleLabelFragmentInfoPtrStack)this._detect._probe()[0];
135 for(int i=0;i<ls.getLength();i++){
136 NyARRleLabelFragmentInfoPtrStack.RleLabelFragmentInfo label=ls.getItem(i);
137 // if(label.area==0){break;}
138 Graphics g2=img.getGraphics();
139 g2.setColor(Color.RED);
140 g2.drawRect(label.clip_l,label.clip_t,label.clip_r-label.clip_l,label.clip_b-label.clip_t);
144 g.drawImage(img,50,50,null);
152 private void startCapture()
155 this._capture.start();
156 } catch (Exception e) {
161 public static void main(String[] args)
164 LabelingViewer mainwin = new LabelingViewer();
165 mainwin.setVisible(true);
166 mainwin.startCapture();
167 } catch (Exception e) {