我需要创建一个应用程序,它将为每个打开的应用程序截取屏幕截图。 例如,当应用程序运行时,如果我有4个窗口,它将创建4个截图,包含4个不同的图片。
我有以下代码,但不知道该怎么办..
#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;
}
最好的方法是什么?
问候 亚历
答案 0 :(得分:3)
您需要使用XQueryTree
查找子窗口,然后保存每个窗口。
这是一个使用它的例子,只打印窗口名称
#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);
}
}