Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleVideoSource_Swissranger.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 
35 
36 #include <Gui/ImageCanvas.hh>
37 #include <Image/Camera.hh>
38 #include <Gui/StringConv.hh>
39 #include <VideoSource/VideoSource_SwissRanger.hh>
40 #include <VideoSource/VideoSourceCapabilities.hh>
41 #include <unistd.h>
42 #include <Base/Image/ImageConvert.hh>
43 #include <Base/Image/ImageIO.hh>
44 #include <Gui/StringConv.hh>
45 
46 
47 
48 using namespace BIAS;
49 using namespace std;
50 
51 bool done = false;
52 bool save = false;
53 int imgNo = 0;
54 
55 /** \cond HIDDEN_SYMBOLS */
56 class MyApp : public wxApp
57 {
58  virtual bool OnInit();
59 };
60 
61 class MyFrame: public wxFrame
62 {
63 public:
64 
65  MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
66 
67  void OnQuit(wxCommandEvent& event);
68  void OnReconnect(wxCommandEvent& event);
69  void OnTimer(wxTimerEvent& event);
70 
71  DECLARE_EVENT_TABLE()
72 protected:
73  wxButton *QuitButton_, *WhiteBalanceButton_;
74  ImageCanvas *ic_;
75  wxTimer Timer_;
77  Camera<unsigned char> pic;
78  string device;
79  int framecount;
80 };
81 
82 
83 enum
84 {
85  ID_Quit = 1,
87  ID_Reconnect,
88  ID_TIMER = 1001
89 };
90 
91 
92 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
93  EVT_MENU(ID_Quit, MyFrame::OnQuit)
94  EVT_MENU(ID_Reconnect, MyFrame::OnReconnect)
95  EVT_BUTTON(ID_BUTTON_QUIT, MyFrame::OnQuit)
96  EVT_TIMER(ID_TIMER, MyFrame::OnTimer)
97 END_EVENT_TABLE()
98 
99 IMPLEMENT_APP(MyApp)
100 
101 bool MyApp::OnInit()
102 {
103  MyFrame *frame =
104  new MyFrame( wxT("Example Video4Linux"), wxPoint(50,50), wxSize(800,600) );
105  SetTopWindow(frame);
106  frame->Show();
107 
108 
109  return TRUE;
110 }
111 
112 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
113  : wxFrame((wxFrame *)NULL, -1, title, pos, size)
114 {
115 
116  cam = new VideoSource_SwissRanger;
117 
118  int res = cam->OpenDevice();
119  if (res<0) {
120  cout<<"Failed to open swissranger device!"<<endl;
121  exit(-1);
122  }
123 
124  cam->InitImage(pic);
125  cam->PreGrab();
126 
127  framecount = 0;
128 
129  wxMenu *menuFile = new wxMenu;
130  menuFile->Append( ID_Quit, wxT("E&xit") );
131  menuFile->Append( ID_Reconnect, wxT("Reconnect") );
132 
133  wxMenuBar *menuBar = new wxMenuBar;
134  menuBar->Append( menuFile, wxT("&File") );
135 
136  SetMenuBar( menuBar );
137  wxStatusBar* s = CreateStatusBar();
138  SetStatusText( wxT("") );
139 
140  wxSizer *Sizer = new wxBoxSizer( wxHORIZONTAL );
141 
142  ic_ = new ImageCanvas(this,s,0,-1,wxDefaultPosition,wxSize(640,480));
143 
144  Sizer->Add(ic_,1,wxEXPAND);
145  SetSizer( Sizer );
146  Layout();
147 
148  ic_->Show(pic,"foo");
149  Timer_.SetOwner(this, ID_TIMER);
150  Timer_.Start(100, true);
151 }
152 
153 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
154 {
155  Timer_.Stop();
156  biasusleep(500000);
157  cam->PostGrab();
158  cam->CloseDevice();
159  Close(TRUE);
160 }
161 
162 void MyFrame::OnReconnect(wxCommandEvent& WXUNUSED(event))
163 {
164  Timer_.Stop();
165  biasusleep(500000);
166  cam->PostGrab();
167  cam->CloseDevice();
168  delete cam;
169 
170  cam = new VideoSource_SwissRanger;
171  cam->OpenDevice();
172  cam->InitImage(pic);
173  cam->PreGrab();
174  cam->SetWaitForNew(false);
175  Timer_.Start(100,true);
176 }
177 
178 
179 
180 void MyFrame::OnTimer(wxTimerEvent& event)
181 {
182  cam->GrabSingle(pic);
183  char name[255];
184  sprintf(name,"image_%04i",imgNo);
185  SetStatusText(AsciiToWx(name));
186  BIAS::UUID uid;
187 
188  // evil, evil, evil: using false corrupts the videosource !!!
189  uid.AssignUUID(true);
190 
191  ic_->Show(pic,name);
192  // Image<unsigned char> rgb;
193  // ImageConvert::ToRGB(pic,rgb);
194  // BIAS::ImageIO::Save(name,rgb,ImageIO::FF_jpg);
195  // cout<<"Written image:"<<name<<" to disk"<<endl;
196  Refresh();
197  imgNo++;
198  Timer_.Start(20, true); // grab new images every 20 ms = resp 50 Hz
199 }
200 /** \endcond */
201 
202 
203 
204 
205 
206 
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
Support for SwissRanger usb cam.
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
virtual int OpenDevice()
try to open first unused PMD-camera
interface class for producing/storing Universally Unique IDentifiers
Definition: UUID.hh:98