#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;
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_;
string device;
int framecount;
};
enum
{
ID_Reconnect,
};
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(ID_Reconnect, MyFrame::OnReconnect)
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)
{
vector<string> 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 ) {
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->CloseDevice();
delete cam;
vector<string> 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);
ic_->Show(pic,name);
Refresh();
imgNo++;
Timer_.Start(20, true);
}