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

Example for a videosource using DirectShow/DirectX

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 ExampleVideoSource_DShow.cpp
@relates VideoSource_DSHOW
@ingroup g_examples
@ingroup g_videosource
@brief Example for a videosource using DirectShow/DirectX
@author MIP
*/
#include <Base/Common/W32Compat.hh> // sprintf wrn
#include <VideoSource/VideoSource_DSHOW.hh>
#include <VideoSource/VideoSourceCapabilities.hh>
#include <Base/Image/ImageIO.hh>
#include <Base/Image/ImageConvert.hh>
#include <Image/Camera.hh>
#include <Gui/ImageCanvas.hh>
#include <Gui/ConsoleRedirectIO.hh>
#include <Gui/StringConv.hh>
#include <Base/Common/FileHandling.hh>
#include <Gui/VideoSource_Controller.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 OnTimer(wxTimerEvent& event);
DECLARE_EVENT_TABLE()
protected:
wxButton *QuitButton_, *WhiteBalanceButton_;
wxTimer Timer_;
Camera<unsigned char> pic;
const string camfile;
int framecount;
};
enum
{
ID_Quit = 1,
ID_TIMER = 1001
};
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(ID_Quit, MyFrame::OnQuit)
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 DirectShow"), 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_DSHOW;
vector<string> devices;
cam->GetAllDevices(devices);
if (devices.size() ==0) {
cerr <<"No devices found. Abort."<<endl;
exit(1);
}
cam->GetCapabilities(caps);
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;
cam->SetSize(720,480);
cam->SetColorModel(ImageBase::CM_YUYV422);
wxArrayString devStrings;
for(unsigned i=0;i<devices.size();i++){
cout<<"Found device:"<<devices[i]<<endl;
devStrings.Add(AsciiToWx(devices[i]));
}
string camera;
// ask for depth error model
wxSingleChoiceDialog wcbDlg(this,
wxT("Select camera."),
wxT("DShow Cameras"),
devStrings);
wcbDlg.SetSelection(0);
int ret = wcbDlg.ShowModal();
if(ret == wxID_OK){
camera = WxToAscii(devStrings.Item(wcbDlg.GetSelection()));
} else return;
int res = cam->OpenDevice(camera.c_str());
//int res = cam->OpenDevice(devices[1].c_str());
if (res!=0) {
cout<<"Failed to open device"<<endl;
exit(-1);
}
float shutter = cam->GetShutter();
cout <<"GetShutter: "<<shutter<<endl;
shutter++;
cout<<"Setting shutter: "<<shutter<<endl;
cam->SetShutter(shutter);
cout <<"GetShutter: "<<cam->GetShutter()<<endl;
float gain = cam->GetGain();
cout<<"Gain:"<<gain<<endl;
gain++;
cout<<"Setting Gain:"<<gain<<endl;
cam->SetGain(gain);
cout<<"Gain:"<<cam->GetGain()<<endl;
cam->InitImage(pic);
cam->PreGrab();
framecount = 0;
controller = new VideoSource_Controller(cam, this,string("DShow Controller"));
wxMenu *menuFile = new wxMenu;
menuFile->Append( ID_Quit, wxT("E&xit") );
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append( menuFile, wxT("&File") );
SetMenuBar( menuBar );
wxStatusBar* s = CreateStatusBar(2);
SetStatusText( wxT(""),1 );
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");
controller->Show(true);
Timer_.SetOwner(this, ID_TIMER);
Timer_.Start(100, true);
}
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
Timer_.Stop();
biasusleep(500000);
cam->PostGrab();
cam->CloseDevice();
exit(0);
}
void MyFrame::OnTimer(wxTimerEvent& event)
{
cam->GrabSingle(pic);
stringstream name;
name<<"image_"<<FileHandling::LeadingZeroString(4,imgNo);
ic_->Show(pic,name.str());
//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(50, true); // grab new images every 20 ms = resp 50 Hz
}
/** \endcond */