The following code will open a device located at "/dev/video0" and read a frame into memory.
Here is the code, which will be taken piece by piece in a moment.
1 #include <iostream>
2 using std::cerr;
3 using std::endl;
4 #include <string>
5 using std::string;
6
7 #include <libvcvideo/videoDevice.h>
8
9 int main (int argc, char ** argv) {
10 vc::videoDevice device ("/dev/video0");
11 vc::vdFrame frame;
12
13 try {
14 device.init();
15 }
16 catch(string s) {
17 cerr << "Device initialization failed: " << s << endl;
18 exit(1);
19 }
20
21 try {
22 test.getFrame(frame);
23 }
24 catch(string s) {
25 cout << "Failed to get frame: " << s << endl;
26 exit(1);
27 }
28 }
At line 10 we start using the library. Everything is kept in the vc namespace, which stands for VelvetCache.org and is why the library is named as it is.
The two things in the vc namespace we are concerned with right now are the videoDevice object and the vdFrame struct. We create one of each, and we pass the videoDevice an absolute path to our device, in this case "/dev/video0". The videoDevice class provides a static member for finding video devices on the fly, called enumerateDevices, but we leave it's use as an exercise for the reader.
10 vc::videoDevice device ("/dev/video0"); 11 vc::vdFrame frame;
Simply creating our videoDevice object doesn't really do anything, so we need to initialize it, like on line 14. libvcvideo uses exceptions for almost all of it's error handling, and it always throws plain old std::string objects. Thus we need to wrap all our volatile calls in try/catch blocks. You can check the API to see what methods throw exceptions and why.
14 device.init();
If we get through initialization the device will be opened at it's largest capture dimensions by default. To get a frame all we have to do is ask using getFrame passing along our vdFrame struct, like on line 22. There is no need to prepare the frame in any way, the object will handle that in the call.
22 test.getFrame(frame);
Thats it. The frame struct now holds data from your video device in 3 bytes per pixel, with BGR byte ordering. You can now dump this to a file, show it to the user, or do whatever with it that you please.
1.5.3