Reputation: 470
I need to create an application that will take a screenshot for each open application. for example, when the application runs, if I have 4 windows, it will create 4 screenshots with 4 different pictures.
I have the following code but do not know what to do anymore..
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
IplImage *XImage2IplImageAdapter(XImage *ximage)
{
IplImage *iplImage;
assert(ximage->format == ZPixmap);
assert(ximage->depth == 24);
iplImage = cvCreateImageHeader(
cvSize(ximage->width, ximage->height),
IPL_DEPTH_8U,
ximage->bits_per_pixel/8);
iplImage->widthStep = ximage->bytes_per_line;
if(ximage->data != NULL)
iplImage->imageData = ximage->data;
return iplImage;
}
using namespace cv;
int main(){
Display* display = XOpenDisplay(NULL);
Screen *screen = DefaultScreenOfDisplay(display);
int widthX = screen->width;
int heightY = screen->height;
XImage* xImageSample = XGetImage(display, DefaultRootWindow(display), 0, 0, widthX, heightY, AllPlanes, ZPixmap);
if (!(xImageSample != NULL && display != NULL && screen != NULL)){
return EXIT_FAILURE;
}
IplImage *cvImageSample = XImage2IplImageAdapter(xImageSample);
Mat matImg = Mat(cvImageSample);
Size dynSize(widthX/3, heightY/3);
Mat finalMat = Mat(dynSize,CV_8UC1);
resize(matImg, finalMat, finalMat.size(), 0, 0, INTER_CUBIC);
imshow("Test",finalMat);
waitKey(0);
return 0;
}
What is the best way to do that?
regards Alex
Upvotes: 1
Views: 725
Reputation: 7792
You'll need to use XQueryTree
to find the child windows and then save each one.
Here's an example of it's use that just prints the window names
#include <X11/Xlib.h>
#include <iostream>
int main(int argc, char ** argv)
{
Display *dpy = XOpenDisplay(NULL);
Window root;
Window parent;
Window *children;
unsigned int num_children;
Status s = XQueryTree(dpy, Window DefaultRootWindow(dpy), &root, &parent,
&children, &num_children);
if (s != BadWindow) {
std::cout << "Children: " << num_children << std::endl;
}
for (int i=0; i<num_children; i++) {
char* name;
XFetchName(dpy, children[i], &name);
if (name != NULL) {
std::cout << "Child " << i << ": " << name << std::endl;
}
}
if (children != NULL) {
XFree(children);
}
}
Upvotes: 3