Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleVideoSource_DShow.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 
26 /** @example ExampleVideoSource_DShow.cpp
27  @relates VideoSource_DSHOW
28  @ingroup g_examples
29  @ingroup g_videosource
30  @brief Example for a videosource using DirectShow/DirectX
31  @author MIP
32 */
33 #include <Base/Common/W32Compat.hh> // sprintf wrn
34 #include <VideoSource/VideoSource_DSHOW.hh>
35 #include <VideoSource/VideoSourceCapabilities.hh>
36 
37 #include <Base/Image/ImageIO.hh>
38 #include <Base/Image/ImageConvert.hh>
39 #include <Image/Camera.hh>
40 #include <Gui/ImageCanvas.hh>
41 #include <Gui/ConsoleRedirectIO.hh>
42 #include <Gui/StringConv.hh>
43 #include <Base/Common/FileHandling.hh>
44 #include <Gui/VideoSource_Controller.hh>
45 
46 using namespace BIAS;
47 using namespace std;
48 
49 bool done = false;
50 bool save = false;
51 int imgNo = 0;
52 
53 /** \cond HIDDEN_SYMBOLS */
54 class MyApp : public wxApp
55 {
56  virtual bool OnInit();
57 };
58 
59 class MyFrame: public wxFrame
60 {
61 public:
62 
63  MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
64 
65  void OnQuit(wxCommandEvent& event);
66  void OnTimer(wxTimerEvent& event);
67 
68  DECLARE_EVENT_TABLE()
69 protected:
70  wxButton *QuitButton_, *WhiteBalanceButton_;
71  ImageCanvas *ic_;
72  wxTimer Timer_;
73  VideoSource_DSHOW *cam;
74  VideoSource_Controller *controller;
75  Camera<unsigned char> pic;
76  const string camfile;
77  int framecount;
78 };
79 
80 
81 enum
82 {
83  ID_Quit = 1,
85  ID_TIMER = 1001
86 };
87 
88 
89 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
90  EVT_MENU(ID_Quit, MyFrame::OnQuit)
91  EVT_BUTTON(ID_BUTTON_QUIT, MyFrame::OnQuit)
92  EVT_TIMER(ID_TIMER, MyFrame::OnTimer)
93 END_EVENT_TABLE()
94 
95 IMPLEMENT_APP(MyApp)
96 
97 bool MyApp::OnInit()
98 {
100  MyFrame *frame =
101  new MyFrame( wxT("Example DirectShow"), wxPoint(50,50), wxSize(800,600) );
102  SetTopWindow(frame);
103  frame->Show();
104 
105  return TRUE;
106 }
107 
108 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
109  : wxFrame((wxFrame *)NULL, -1, title, pos, size)
110 {
111  cam = new VideoSource_DSHOW;
112  vector<string> devices;
113  cam->GetAllDevices(devices);
114  if (devices.size() ==0) {
115  cerr <<"No devices found. Abort."<<endl;
116  exit(1);
117  }
118 
120  cam->GetCapabilities(caps);
121  caps.Show();
122  vector<VideoSourceCapabilities::ResolutionEntry> re;
123  caps.GetBestForEachResolution(re);
124  for (unsigned int i=0;i<re.size();i++)
125  cout <<" Best for "<<re[i].width<<"x"<<re[i].height<<" is "
126  <<re[i].colormodel<<" at fps: "<<re[i].fps<<endl;
127 
128  cam->SetSize(720,480);
129  cam->SetColorModel(ImageBase::CM_YUYV422);
130 
131  wxArrayString devStrings;
132  for(unsigned i=0;i<devices.size();i++){
133  cout<<"Found device:"<<devices[i]<<endl;
134  devStrings.Add(AsciiToWx(devices[i]));
135  }
136  string camera;
137  // ask for depth error model
138  wxSingleChoiceDialog wcbDlg(this,
139  wxT("Select camera."),
140  wxT("DShow Cameras"),
141  devStrings);
142  wcbDlg.SetSelection(0);
143  int ret = wcbDlg.ShowModal();
144  if(ret == wxID_OK){
145  camera = WxToAscii(devStrings.Item(wcbDlg.GetSelection()));
146  } else return;
147 
148  int res = cam->OpenDevice(camera.c_str());
149  //int res = cam->OpenDevice(devices[1].c_str());
150  if (res!=0) {
151  cout<<"Failed to open device"<<endl;
152  exit(-1);
153  }
154  float shutter = cam->GetShutter();
155  cout <<"GetShutter: "<<shutter<<endl;
156  shutter++;
157  cout<<"Setting shutter: "<<shutter<<endl;
158  cam->SetShutter(shutter);
159  cout <<"GetShutter: "<<cam->GetShutter()<<endl;
160 
161  float gain = cam->GetGain();
162  cout<<"Gain:"<<gain<<endl;
163  gain++;
164  cout<<"Setting Gain:"<<gain<<endl;
165  cam->SetGain(gain);
166  cout<<"Gain:"<<cam->GetGain()<<endl;
167  cam->InitImage(pic);
168 
169  cam->PreGrab();
170  framecount = 0;
171 
172  controller = new VideoSource_Controller(cam, this,string("DShow Controller"));
173 
174  wxMenu *menuFile = new wxMenu;
175  menuFile->Append( ID_Quit, wxT("E&xit") );
176 
177  wxMenuBar *menuBar = new wxMenuBar;
178  menuBar->Append( menuFile, wxT("&File") );
179 
180  SetMenuBar( menuBar );
181  wxStatusBar* s = CreateStatusBar(2);
182  SetStatusText( wxT(""),1 );
183 
184  wxSizer *Sizer = new wxBoxSizer( wxHORIZONTAL );
185 
186 
187  ic_ = new ImageCanvas(this,s,0,-1,wxDefaultPosition,wxSize(640,480));
188  Sizer->Add(ic_,1,wxEXPAND);
189 
190  SetSizer( Sizer );
191  Layout();
192 
193  ic_->Show(pic,"foo");
194 
195  controller->Show(true);
196  Timer_.SetOwner(this, ID_TIMER);
197  Timer_.Start(100, true);
198 }
199 
200 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
201 {
202  Timer_.Stop();
203  biasusleep(500000);
204  cam->PostGrab();
205  cam->CloseDevice();
206  exit(0);
207 
208 }
209 
210 
211 void MyFrame::OnTimer(wxTimerEvent& event)
212 {
213  cam->GrabSingle(pic);
214  stringstream name;
215  name<<"image_"<<FileHandling::LeadingZeroString(4,imgNo);
216 
217  ic_->Show(pic,name.str());
218  //Image<unsigned char> rgb;
219  //ImageConvert::ToRGB(pic,rgb);
220  // BIAS::ImageIO::Save(name,rgb,ImageIO::FF_jpg);
221  // cout<<"Written image:"<<name<<" to disk"<<endl;
222  Refresh();
223  imgNo++;
224  Timer_.Start(50, true); // grab new images every 20 ms = resp 50 Hz
225 }
226 /** \endcond */
227 
228 
229 
230 
YUYV422, 2 channels, full luminance Y, subsampled half U,V.
Definition: ImageBase.hh:133
void BIASGui_EXPORT ConsoleRedirectIO(std::string name=std::string(""))
Extra Console for WIN32 WinMain gui applications.
wxString AsciiToWx(const char *thestring)
Converts a C string to a wxString.
Definition: StringConv.hh:32
extends the Image by MetaData support (e.g.
Definition: Camera.hh:74
display image in wx application, provides zoom and investigation functionality
Definition: ImageCanvas.hh:38
This class extends VideoSource for the use of DirectShow devices you need Microsoft Windows SDK versi...
static std::string LeadingZeroString(const int &n, const unsigned int &digits=DEFAULT_LEADING_ZEROS)
Create a string with leading zeroes from number.
Checks for VideoSource capabilities.
int GetAllDevices(std::vector< std::string > &devices)
void GetBestForEachResolution(std::vector< ResolutionEntry > &res)