-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathwebcam.h
69 lines (55 loc) · 1.42 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
/** Small C++ wrapper around V4L example code to access the webcam
**/
#include <string>
#include <memory> // unique_ptr
#include <linux/videodev2.h>
struct buffer
{
void *data;
size_t size;
};
struct YUV420Image
{
unsigned char *data; // YUV420
size_t width;
size_t height;
size_t size; // width * height * 3/2
};
class Webcam
{
public:
Webcam(const std::string &device = "/dev/video0");
~Webcam();
void GetCameraSize(int &Width, int &Height);
int ConvertColor(unsigned char *out, unsigned char *in);
void SetOmxBuffer(unsigned char *Buffer);
/** Captures and returns a frame from the webcam.
*
* The returned object contains a field 'data' with the image data in YUV420
* format
* 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 YUV420Image &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;
YUV420Image yuv420frame;
struct buffer *buffers;
unsigned int n_buffers;
size_t xres, yres;
size_t stride;
struct v4l2_format fmt;
bool force_format = false;
bool StatusCapturing = false;
};