-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScreenGrabber.cpp
73 lines (60 loc) · 1.6 KB
/
ScreenGrabber.cpp
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
#include "ScreenGrabber.h"
#include <X11/Xutil.h>
#include <cstring>
ScreenGrabber::ScreenGrabber()
{
display = XOpenDisplay(nullptr);
setRectangle(0,0,0,0);
}
ScreenGrabber::ScreenGrabber(int x,int y,int width,int height)
{
display = XOpenDisplay(nullptr);
setRectangle(x,y,width,height);
}
/*
void ScreenGrabber::ImageFromDisplay(std::vector<uint8_t>& Pixels, int& Width, int& Height, int& BitsPerPixel)
{
XImage* img = XGetImage(display, root, 0, 0 , Width, Height, AllPlanes, ZPixmap);
BitsPerPixel = img->bits_per_pixel;
Pixels.resize(Width * Height * 4);
memcpy(&Pixels[0], img->data, Pixels.size());
XFree(img);
}
*/
void ScreenGrabber::getScreenSize(int &width,int &height)
{
Window root = DefaultRootWindow(display);
XWindowAttributes attributes = {0};
XGetWindowAttributes(display, root, &attributes);
width = attributes.width;
height = attributes.height;
}
void ScreenGrabber::setRectangle(int x,int y,int width,int height)
{
this-> x = x;
this -> y = y;
this-> width = width;
this -> height = height;
if(width <= 0 || height <= 0) {
getScreenSize(this->width,this->height);
x=0;
y=0;
}
}
void ScreenGrabber::setRectangle(cv::Rect rect)
{
setRectangle(rect.x,rect.y,rect.width,rect.height);
}
cv::Mat ScreenGrabber::getNextFrame()
{
Window root = DefaultRootWindow(display);
XImage* img = XGetImage(display, root, x, y , width, height, AllPlanes, ZPixmap);
cv::Mat frame(height,width,img->bits_per_pixel > 24 ? CV_8UC4 : CV_8UC3,img->data);
cv::Mat frame2 = frame.clone();
XDestroyImage(img);
return frame2;
}
ScreenGrabber::~ScreenGrabber()
{
XCloseDisplay(display);
}