これははるかに大きな研究プロジェクトのサブプロジェクトです。私は100msごとにアクティブなウィンドウ(ブラウザ)のスクリーンショットを取ろうとしており、OpenCV処理のためにメモリに保存されます。私は同様の質問からスクリーンショットを撮るという解決策を見つけました。私は現在、コードを使って私がそれを使用できるかどうか確認しています。次のスニペットは、デスクトップのスクリーンショット全体または特定のWindowのスクリーンショットを撮るときに動作しているようですが、GTKウィンドウでは機能しません。私はDebian SqueezeでIceweasel & Nautilusのスクリーンショットを撮ろうとしましたが、うまくいきません。私はX11のトータルのnoobです。エラーをチェックする方法がわからない、またはQTウィンドウでうまくいくようにGTKに欠けているものがあるかどうかはわかりません。アクティブウィンドウのX11スクリーンショットがGTKウィンドウで失敗する
typedef int (*handler)(Display *, XErrorEvent *);
int handleX11Error(Display *d, XErrorEvent *er)
{
std::cout << "X11 Error: " << er->error_code << std::endl;
}
int main()
{
std::cout << "Sleeping 5 seconds" << std::endl;
// we may need to sleep if we want to focus another window.
sleep(5);
std::cout << "taking screenshot" << std::endl;
Display *display = XOpenDisplay(NULL);
//Window root = DefaultRootWindow(display);
XWindowAttributes gwa;
int revert = RevertToNone;
Window active;
XErrorEvent *error;
handler myHandler = &handleX11Error;
XSetErrorHandler(myHandler);
// X11 - Get Window that has focus
XGetInputFocus(display,&active,&revert);
//XGetWindowAttributes(display, root, &gwa);
if (!XGetWindowAttributes(display, active, &gwa))
std::cout << "XGetWindowAttributes failed" << std::endl;
int width = gwa.width;
int height = gwa.height;
//XImage *image = XGetImage(display,root, 0,0 , width,height,AllPlanes, ZPixmap);
XImage *image = XGetImage(display,active, 0,0 , width,height,XAllPlanes(), ZPixmap);
unsigned char *array = new unsigned char[width * height * 3];
CImg<unsigned char> pic(array,width,height,1,3);
for (int x = 0; x < width; x++){
for (int y = 0; y < height ; y++){
pic(x,y,0) = (XGetPixel(image,x,y) & image->red_mask) >> 16;
pic(x,y,1) = (XGetPixel(image,x,y) & image->green_mask) >> 8;
pic(x,y,2) = XGetPixel(image,x,y) & image->blue_mask;
}
}
delete[] array;
pic.save_png("blah.png");
std::cout << "Finished" << std::endl;
return 0;
}
上記のコードは、フルデスクトップスクリーンショットまたはQTのいずれかで動作します。私はエラーを受け取りません(正しく処理しているかどうかわかりません)。何バイトかの空の絵があるので、XGetFocusがうまく動作していないと思うので、X関数の1つ(XGetInputFocus、XGetWindowAttributes、XGetImage)が失敗すると思います。 私には何が欠けているのですか?これに代わる方法はありますか? KDE(4.4.5)が重要であれば実行しています。
UPDATE:
int main(int argc, char **argv)
{
sleep(5);
Display *display = XOpenDisplay(NULL);
int revert = RevertToNone;
Window active;
XGetInputFocus(display,&active,&revert);
QApplication app(argc, argv);
QPixmap pixmap = QPixmap::grabWindow(active);
pixmap.save("test.png","PNG");
QPushButton quit("Quit");
quit.resize(75, 30);
quit.setFont(QFont("Times", 18, QFont::Bold));
QObject::connect(&quit, SIGNAL(clicked()), &app, SLOT(quit()));
quit.show();
return app.exec();
}
私は:私はQt4をを使用してスクリーンショットを撮ってみましたが、それが正常に動作しながら、X11から焦点を当てたウィンドウを取得しようとしたとき、それは同じ問題に実行されます
したがって、XGetInputFocus()は何とか失敗すると確信しています。
私はそれをテストしましたが、このプログラムに適用するとノーチラスのウィンドウではうまくいきません:http://stackoverflow.com/questions/22749444/listening-to-keyboard-events-without-consuming-them-in- x11-キーボードフック – user2029077