Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleV4L.cpp

Example for a v4l videosource with wx gui , VideoSourceCapabilities

Author
MIP
/*
This file is part of the BIAS library (Basic ImageAlgorithmS).
Copyright (C) 2003-2009 (see file CONTACT for details)
Multimediale Systeme der Informationsverarbeitung
Institut fuer Informatik
Christian-Albrechts-Universitaet Kiel
BIAS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
BIAS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with BIAS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/** @example ExampleV4L.cpp
@relates VideoSource_V4L, VideoSourceCapabilities
@ingroup g_examples
@ingroup g_videosource
@brief Example for a v4l videosource with wx gui
@author MIP
*/
#include <Gui/ImageCanvas.hh>
#include <Gui/StringConv.hh>
#include <VideoSource/VideoSource_V4L.hh>
#include <VideoSource/VideoSourceCapabilities.hh>
#include <unistd.h>
#include <Base/Image/ImageConvert.hh>
#include <Base/Image/ImageIO.hh>
#include <Gui/StringConv.hh>
#include <Image/Camera.hh>
using namespace BIAS;
using namespace std;
bool done = false;
bool save = false;
int imgNo = 0;
/** \cond HIDDEN_SYMBOLS */
class MyApp : public wxApp
{
virtual bool OnInit();
};
class MyFrame: public wxFrame
{
public:
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
void OnQuit(wxCommandEvent& event);
void OnReconnect(wxCommandEvent& event);
void OnTimer(wxTimerEvent& event);
DECLARE_EVENT_TABLE()
protected:
wxButton *QuitButton_, *WhiteBalanceButton_;
wxTimer Timer_;
Camera<unsigned char> pic;
string device;
int framecount;
};
enum
{
ID_Quit = 1,
ID_Reconnect,
ID_TIMER = 1001
};
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(ID_Quit, MyFrame::OnQuit)
EVT_MENU(ID_Reconnect, MyFrame::OnReconnect)
EVT_BUTTON(ID_BUTTON_QUIT, MyFrame::OnQuit)
EVT_TIMER(ID_TIMER, MyFrame::OnTimer)
END_EVENT_TABLE()
IMPLEMENT_APP(MyApp)
bool MyApp::OnInit()
{
MyFrame *frame =
new MyFrame( wxT("Example Video4Linux"), wxPoint(50,50), wxSize(800,600) );
SetTopWindow(frame);
frame->Show();
return TRUE;
}
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame((wxFrame *)NULL, -1, title, pos, size)
{
cam = new VideoSource_V4L;
vector<string> devices;
cam->GetAllDevices(devices);
if (devices.size()==0) {
cout <<"No V4L devices found"<<endl;
exit(1);
}
for ( unsigned d=0; d<devices.size(); d++ )
cout <<"Foud V4L device: "<<devices[d]<<endl;
device = devices[0];
cout <<"Using: "<<device<<endl;
if ( cam->GetCapabilities(device.c_str(),caps) == 0 ) {
caps.Show();
vector<VideoSourceCapabilities::ResolutionEntry> re;
for (unsigned int i=0;i<re.size();i++){
cout <<" Best for "<<re[i].width<<"x"<<re[i].height<<" is "
<<re[i].colormodel<<" at fps: "<<re[i].fps<<endl;
}
}
else {
cerr<<"failed to open "<<device<<endl;
exit(1);
}
cam->SetSize(320,240);
cam->SetColorModel(ImageBase::CM_YUYV422);
int res = cam->OpenDevice(device.c_str());
if (res!=0) {
cout<<"Failed to open device "<<device<<endl;
exit(-1);
}
cam->InitImage(pic);
cam->PreGrab();
cam->SetWaitForNew(false);
framecount = 0;
wxMenu *menuFile = new wxMenu;
menuFile->Append( ID_Quit, wxT("E&xit") );
menuFile->Append( ID_Reconnect, wxT("Reconnect") );
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append( menuFile, wxT("&File") );
SetMenuBar( menuBar );
wxStatusBar* s = CreateStatusBar();
SetStatusText( wxT("") );
wxSizer *Sizer = new wxBoxSizer( wxHORIZONTAL );
ic_ = new ImageCanvas(this,s,0,-1,wxDefaultPosition,wxSize(640,480));
Sizer->Add(ic_,1,wxEXPAND);
SetSizer( Sizer );
Layout();
ic_->Show(pic,"foo");
Timer_.SetOwner(this, ID_TIMER);
Timer_.Start(100, true);
}
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
Timer_.Stop();
usleep(500000);
cam->PostGrab();
cam->CloseDevice();
Close(TRUE);
}
void MyFrame::OnReconnect(wxCommandEvent& WXUNUSED(event))
{
Timer_.Stop();
usleep(500000);
// cam->PostGrab();
cam->CloseDevice();
delete cam;
cam = new VideoSource_V4L;
vector<string> devs;
cam->GetAllDevices(devs);
cam->GetCapabilities(devs[0].c_str(),caps);
cam->OpenDevice(device.c_str());
cam->InitImage(pic);
cam->PreGrab();
cam->SetWaitForNew(false);
Timer_.Start(100,true);
}
void MyFrame::OnTimer(wxTimerEvent& event)
{
cam->GrabSingle(pic);
char name[255];
sprintf(name,"image_%04i",imgNo);
SetStatusText(AsciiToWx(name));
// evil, evil, evil: using false corrupts the videosource !!!
uid.AssignUUID(true);
ic_->Show(pic,name);
// Image<unsigned char> rgb;
// ImageConvert::ToRGB(pic,rgb);
// BIAS::ImageIO::Save(name,rgb,ImageIO::FF_jpg);
// cout<<"Written image:"<<name<<" to disk"<<endl;
Refresh();
imgNo++;
Timer_.Start(20, true); // grab new images every 20 ms = resp 50 Hz
}
/** \endcond */