This example shows how we would make a new device for a camera.We make a device that reads images from file. The only step missing here is to integrate the device into the YARP library.
Here's the header file, dev/FileFrameGrabber.h
#include <stdio.h>
private:
std::string pattern, lastLoad;
int first, last, at;
int h, w;
bool triedFirst = false;
char buf[1000];
sprintf(buf,pattern.c_str(),at);
if (at==first) {
if (triedFirst) {
return false;
}
triedFirst = true;
}
if (last==-1) {
at = first;
} else {
at++;
if (at>last) {
at = first;
}
}
sprintf(buf,pattern.c_str(),at);
}
lastLoad = buf;
return true;
}
public:
FileFrameGrabber() {
pattern = "%d.ppm";
first = last = -1;
at = -1;
h = w = 0;
}
bool open(
const char *pattern,
int first,
int last) {
this->pattern = pattern;
this->first = first;
this->last = last;
at = first;
return findImage(dummy);
}
std::string pattern =
return open(pattern.c_str(),first,last);
}
return true;
}
bool ok = findImage(image);
if (ok) {
printf("showing image %s\n", lastLoad.c_str());
at++;
if (last!=-1 && at>last) {
at = first;
}
}
return ok;
}
return h;
}
virtual int width()
const {
return w;
}
};
And here's how we can use it. In fact, we just grab the first image from the device.
#include <stdio.h>
#include <stdlib.h>
#include "FileFrameGrabber.h"
int main(
int argc,
char *argv[]) {
"grabber",
"FileFrameGrabber");
Drivers::factory().add(file_grabber_factory);
if (argc==1) {
config.
fromString(
"(device file_grabber) (pattern \"image/%03d.ppm\")");
} else {
}
printf("Failed to create and configure a device\n");
return 1;
}
if (!dd.
view(grabberInterface)) {
printf("Failed to view device through IFrameGrabberImage interface\n");
return 1;
}
printf(
"Got a %dx%d image\n", img.
width(), img.
height());
return 0;
}