Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
tutorial-grabber-v4l2.cpp
1 
2 #include <visp3/sensor/vpV4l2Grabber.h>
3 #include <visp3/gui/vpDisplayX.h>
4 #include <visp3/core/vpImage.h>
5 
12 int main(int argc, const char* argv[])
13 {
14 #ifdef VISP_HAVE_V4L2
15  try {
16  unsigned int opt_device = 0;
17  unsigned int opt_scale = 2; // Default value is 2 in the constructor. Turn it to 1 to avoid subsampling
18 
19  for (int i=0; i<argc; i++) {
20  if (std::string(argv[i]) == "--device")
21  opt_device = (unsigned int)atoi(argv[i+1]);
22  else if (std::string(argv[i]) == "--scale")
23  opt_scale = (unsigned int)atoi(argv[i+1]);
24  else if (std::string(argv[i]) == "--help") {
25  std::cout << "Usage: " << argv[0] << " [--device <camera device>] [--scale <subsampling factor>] [--help]" << std::endl;
26  return 0;
27  }
28  }
29 
31 
32  vpV4l2Grabber g;
33  std::ostringstream device;
34  device << "/dev/video" << opt_device;
35  g.setDevice(device.str());
36  g.setScale(opt_scale);
37  g.open(I);
38 
39  std::cout << "Image size: " << I.getWidth() << " " << I.getHeight() << std::endl;
40 
41 #ifdef VISP_HAVE_X11
42  vpDisplayX d(I);
43 #else
44  std::cout << "No image viewer is available..." << std::endl;
45 #endif
46 
47  while(1) {
48  g.acquire(I);
50  vpDisplay::displayText(I, 10, 10, "A click to quit", vpColor::red);
52  if (vpDisplay::getClick(I, false)) break;
53  }
54  }
55  catch(vpException &e) {
56  std::cout << "Catch an exception: " << e << std::endl;
57  }
58 #else
59  (void)argc;
60  (void)argv;
61 #endif
62 }
void acquire(vpImage< unsigned char > &I)
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
void open(vpImage< unsigned char > &I)
unsigned int getWidth() const
Definition: vpImage.h:226
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:153
void setDevice(const std::string &devname)
error that can be emited by ViSP classes.
Definition: vpException.h:73
static void flush(const vpImage< unsigned char > &I)
static const vpColor red
Definition: vpColor.h:163
static void display(const vpImage< unsigned char > &I)
void setScale(unsigned scale=vpV4l2Grabber::DEFAULT_SCALE)
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
unsigned int getHeight() const
Definition: vpImage.h:175