Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleV4L.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 ExampleV4L.cpp
27  @relates VideoSource_V4L, VideoSourceCapabilities
28  @ingroup g_examples
29  @ingroup g_videosource
30  @brief Example for a v4l videosource with wx gui
31  @author MIP
32 */
33 
34 #include <Gui/ImageCanvas.hh>
35 #include <Gui/StringConv.hh>
36 
37 #include <VideoSource/VideoSource_V4L.hh>
38 #include <VideoSource/VideoSourceCapabilities.hh>
39 
40 
41 #include <unistd.h>
42 #include <Base/Image/ImageConvert.hh>
43 #include <Base/Image/ImageIO.hh>
44 #include <Gui/StringConv.hh>
45 #include <Image/Camera.hh>
46 
47 
48 
49 using namespace BIAS;
50 using namespace std;
51 
52 bool done = false;
53 bool save = false;
54 int imgNo = 0;
55 
56 /** \cond HIDDEN_SYMBOLS */
57 class MyApp : public wxApp
58 {
59  virtual bool OnInit();
60 };
61 
62 class MyFrame: public wxFrame
63 {
64 public:
65 
66  MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
67 
68  void OnQuit(wxCommandEvent& event);
69  void OnReconnect(wxCommandEvent& event);
70  void OnTimer(wxTimerEvent& event);
71 
72  DECLARE_EVENT_TABLE()
73 protected:
74  wxButton *QuitButton_, *WhiteBalanceButton_;
75  ImageCanvas *ic_;
76  wxTimer Timer_;
77  VideoSource_V4L *cam;
78  Camera<unsigned char> pic;
79  string device;
80  int framecount;
81 };
82 
83 
84 enum
85 {
86  ID_Quit = 1,
88  ID_Reconnect,
89  ID_TIMER = 1001
90 };
91 
92 
93 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
94  EVT_MENU(ID_Quit, MyFrame::OnQuit)
95  EVT_MENU(ID_Reconnect, MyFrame::OnReconnect)
96  EVT_BUTTON(ID_BUTTON_QUIT, MyFrame::OnQuit)
97  EVT_TIMER(ID_TIMER, MyFrame::OnTimer)
98 END_EVENT_TABLE()
99 
100 IMPLEMENT_APP(MyApp)
101 
102 bool MyApp::OnInit()
103 {
104  MyFrame *frame =
105  new MyFrame( wxT("Example Video4Linux"), wxPoint(50,50), wxSize(800,600) );
106  SetTopWindow(frame);
107  frame->Show();
108 
109 
110  return TRUE;
111 }
112 
113 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
114  : wxFrame((wxFrame *)NULL, -1, title, pos, size)
115 {
116 
117  cam = new VideoSource_V4L;
118  vector<string> devices;
119  cam->GetAllDevices(devices);
120 
121  if (devices.size()==0) {
122  cout <<"No V4L devices found"<<endl;
123  exit(1);
124  }
125 
126  for ( unsigned d=0; d<devices.size(); d++ )
127  cout <<"Foud V4L device: "<<devices[d]<<endl;
128 
129  device = devices[0];
130  cout <<"Using: "<<device<<endl;
132  if ( cam->GetCapabilities(device.c_str(),caps) == 0 ) {
133  caps.Show();
134  vector<VideoSourceCapabilities::ResolutionEntry> re;
135  caps.GetBestForEachResolution(re);
136 
137  for (unsigned int i=0;i<re.size();i++){
138  cout <<" Best for "<<re[i].width<<"x"<<re[i].height<<" is "
139  <<re[i].colormodel<<" at fps: "<<re[i].fps<<endl;
140  }
141  }
142  else {
143  cerr<<"failed to open "<<device<<endl;
144  exit(1);
145  }
146 
147  cam->SetSize(320,240);
148  cam->SetColorModel(ImageBase::CM_YUYV422);
149  int res = cam->OpenDevice(device.c_str());
150  if (res!=0) {
151  cout<<"Failed to open device "<<device<<endl;
152  exit(-1);
153  }
154  cam->InitImage(pic);
155 
156  cam->PreGrab();
157  cam->SetWaitForNew(false);
158 
159  framecount = 0;
160 
161 
162  wxMenu *menuFile = new wxMenu;
163  menuFile->Append( ID_Quit, wxT("E&xit") );
164  menuFile->Append( ID_Reconnect, wxT("Reconnect") );
165 
166  wxMenuBar *menuBar = new wxMenuBar;
167  menuBar->Append( menuFile, wxT("&File") );
168 
169  SetMenuBar( menuBar );
170  wxStatusBar* s = CreateStatusBar();
171  SetStatusText( wxT("") );
172 
173  wxSizer *Sizer = new wxBoxSizer( wxHORIZONTAL );
174 
175  ic_ = new ImageCanvas(this,s,0,-1,wxDefaultPosition,wxSize(640,480));
176 
177  Sizer->Add(ic_,1,wxEXPAND);
178  SetSizer( Sizer );
179  Layout();
180 
181  ic_->Show(pic,"foo");
182  Timer_.SetOwner(this, ID_TIMER);
183 
184 
185 
186  Timer_.Start(100, true);
187 }
188 
189 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
190 {
191  Timer_.Stop();
192  usleep(500000);
193  cam->PostGrab();
194  cam->CloseDevice();
195  Close(TRUE);
196 }
197 
198 void MyFrame::OnReconnect(wxCommandEvent& WXUNUSED(event))
199 {
200  Timer_.Stop();
201  usleep(500000);
202  // cam->PostGrab();
203  cam->CloseDevice();
204 
205  delete cam;
206 
207  cam = new VideoSource_V4L;
208  vector<string> devs;
209  cam->GetAllDevices(devs);
211 
212  cam->GetCapabilities(devs[0].c_str(),caps);
213  cam->OpenDevice(device.c_str());
214  cam->InitImage(pic);
215  cam->PreGrab();
216  cam->SetWaitForNew(false);
217  Timer_.Start(100,true);
218 }
219 
220 
221 
222 void MyFrame::OnTimer(wxTimerEvent& event)
223 {
224  cam->GrabSingle(pic);
225  char name[255];
226  sprintf(name,"image_%04i",imgNo);
227  SetStatusText(AsciiToWx(name));
228  BIAS::UUID uid;
229 
230  // evil, evil, evil: using false corrupts the videosource !!!
231  uid.AssignUUID(true);
232 
233  ic_->Show(pic,name);
234  // Image<unsigned char> rgb;
235  // ImageConvert::ToRGB(pic,rgb);
236  // BIAS::ImageIO::Save(name,rgb,ImageIO::FF_jpg);
237  // cout<<"Written image:"<<name<<" to disk"<<endl;
238  Refresh();
239  imgNo++;
240  Timer_.Start(20, true); // grab new images every 20 ms = resp 50 Hz
241 }
242 /** \endcond */
243 
244 
245 
246 
247 
248 
YUYV422, 2 channels, full luminance Y, subsampled half U,V.
Definition: ImageBase.hh:133
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
void AssignUUID(const bool &consecutively=DEFAULT_UUID_CONSECUTIVELY)
function to assign a fresh, unique ID.
Definition: UUID.cpp:264
int GetAllDevices(std::vector< std::string > &devices)
Checks for VideoSource capabilities.
This class extends VideoSource for the use of Video4Linux supported devices like framegrabber, USB cameras.
interface class for producing/storing Universally Unique IDentifiers
Definition: UUID.hh:98
void GetBestForEachResolution(std::vector< ResolutionEntry > &res)