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.NyARRleLabelFragmentInfo;
42 import jp.nyatla.nyartoolkit.core.labeling.rlelabeling.NyARRleLabelFragmentInfoPtrStack;
43 import jp.nyatla.nyartoolkit.core.param.NyARParam;
44 import jp.nyatla.nyartoolkit.core.raster.*;
45 import jp.nyatla.nyartoolkit.core.rasterfilter.rgb2gs.NyARRasterFilter_Rgb2Gs_RgbAve192;
46 import jp.nyatla.nyartoolkit.core.squaredetect.NyARSquareContourDetector_Rle;
47 import jp.nyatla.nyartoolkit.core.types.*;
50 * VFM+ARToolkitテストプログラム
51 * カメラから取り込んだデータからマーカーを検出して、一致度と変換行列を表示します。
53 public class LabelingViewer extends Frame implements JmfCaptureListener
55 class SquareDetector extends NyARSquareContourDetector_Rle
57 public SquareDetector(NyARIntSize i_size) throws NyARException
61 protected void onSquareDetect(NyARIntCoordinates i_coord,int[] i_vertex_index) throws NyARException
66 private static final long serialVersionUID = 6471434231970804953L;
68 private final String PARAM_FILE = "../../Data/camera_para.dat";
70 private JmfCaptureDevice _capture;
73 private JmfNyARRaster_RGB _raster;
75 public LabelingViewer() throws NyARException
77 setTitle("JmfCaptureTest");
78 setBounds(0, 0, 320 + 64, 240 + 64);
80 JmfCaptureDeviceList devlist=new JmfCaptureDeviceList();
81 this._capture=devlist.getDevice(0);
82 //JmfNyARRaster_RGBはYUVよりもRGBで高速に動作します。
83 if(!this._capture.setCaptureFormat(JmfCaptureDevice.PIXEL_FORMAT_RGB,320, 240,15f)){
84 if(!this._capture.setCaptureFormat(JmfCaptureDevice.PIXEL_FORMAT_YUV,320, 240,15f)){
85 throw new NyARException("キャプチャフォーマットが見つかりません");
88 this._capture.setOnCapture(this);
89 this.addWindowListener(new WindowAdapter() {
90 public void windowClosing(WindowEvent e)
96 NyARParam ar_param = new NyARParam();
97 ar_param.loadARParamFromFile(PARAM_FILE);
98 ar_param.changeScreenSize(320, 240);
99 this._raster = new JmfNyARRaster_RGB(this._capture.getCaptureFormat());
100 this._detect=new SquareDetector(ar_param.getScreenSize());
101 this._filter = new NyARRasterFilter_Rgb2Gs_RgbAve192(_raster.getBufferType());
105 private NyARSquareContourDetector_Rle _detect;
106 private NyARGrayscaleRaster _bi=new NyARGrayscaleRaster(320,240);
107 private NyARRasterFilter_Rgb2Gs_RgbAve192 _filter;
111 public void onUpdateBuffer(Buffer i_buffer)
114 NyARGrayscaleRaster gs = this._bi;
115 //キャプチャしたバッファをラスタにセット
116 this._raster.setBuffer(i_buffer);
119 BufferToImage b2i = new BufferToImage((VideoFormat) i_buffer.getFormat());
120 Image img = b2i.createImage(i_buffer);
121 this._filter.doFilter(this._raster,gs);
123 Graphics g = getGraphics();
125 NyARParam param=new NyARParam();
126 param.loadARParamFromFile(PARAM_FILE);
127 param.changeScreenSize(320,240);
129 NyARIntRect rect=new NyARIntRect();
130 rect.x=100;rect.y=100;rect.w=220;rect.h=140;
131 this._detect.detectMarker(gs,rect,110);
135 NyARRleLabelFragmentInfoPtrStack ls=(NyARRleLabelFragmentInfoPtrStack)this._detect._probe()[0];
136 for(int i=0;i<ls.getLength();i++){
137 NyARRleLabelFragmentInfo label=ls.getItem(i);
138 // if(label.area==0){break;}
139 Graphics g2=img.getGraphics();
140 g2.setColor(Color.RED);
141 g2.drawRect(label.clip_l,label.clip_t,label.clip_r-label.clip_l,label.clip_b-label.clip_t);
145 g.drawImage(img,50,50,null);
153 private void startCapture()
156 this._capture.start();
157 } catch (Exception e) {
162 public static void main(String[] args)
165 LabelingViewer mainwin = new LabelingViewer();
166 mainwin.setVisible(true);
167 mainwin.startCapture();
168 } catch (Exception e) {