-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwebcam.h
105 lines (81 loc) · 2.01 KB
/
webcam.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/** Small C++ wrapper around V4L example code to access the webcam
**/
#include <iostream>
#include <string>
#include <memory> // unique_ptr
#include <cstring>
#include <iostream>
#include <stdlib.h>
#include <assert.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <stdexcept>
#include <linux/videodev2.h>
using namespace std;
struct buffer {
void *data;
size_t size;
};
class Image {
public:
unsigned char *data = NULL;
long unsigned int width = 0;
long unsigned int height = 0;
long unsigned int size = 0;
Image(long unsigned int width, long unsigned int height) {
this->width = width;
this->height = height;
}
Image(long unsigned int width, long unsigned int height,
long unsigned int size) {
data = (unsigned char *) malloc(size);
this->width = width;
this->height = height;
this->size = size;
}
~Image() {
delete[] data;
}
};
class Webcam {
public:
Webcam(const std::string& device = "/dev/video0",
int width = 640,
int height = 480);
~Webcam();
/** Captures and returns a frame from the webcam.
*
* The returned object contains a field 'data' with the image data in RGB888
* format (ie, RGB24), as well as 'width', 'height' and 'size' (equal to
* width * height * 3)
*
* This call blocks until a frame is available or until the provided
* timeout (in seconds).
*
* Throws a runtime_error if the timeout is reached.
*/
const Image* frame(int timeout = 1);
private:
void init_mmap();
void open_device();
void close_device();
void init_device();
void uninit_device();
void start_capturing();
void stop_capturing();
bool read_frame();
std::string device;
int fd;
Image *rgb_frame;
struct buffer *buffers;
unsigned int n_buffers;
size_t xres, yres;
size_t stride;
bool force_format = true;
};