抓取窗口并使用Qt分析像素

时间:2015-12-05 12:45:30

标签: c++ qt

我想每100毫秒做一次scrennshots并分析像素。我已经得到了这段代码:

int main(int argc, char** argv)
{

    QApplication app(argc, argv);
    QtGrabber qt_grabber;
    qt_grabber.start();
    return app.exec();
}

QtGrabber::QtGrabber() : timer_(this) {

    screen_ = QGuiApplication::primaryScreen();
    timer_.setInterval(100);
    timer_.setSingleShot(false);
    timer_.connect(&timer_, SIGNAL(timeout()), this, SLOT(createScreenshot()));
}

QtGrabber::~QtGrabber() {

}

void QtGrabber::start() {
    timer_.start();
}

void QtGrabber::stop() {
    timer_.stop();
}

void QtGrabber::createScreenshot() {
   int scale_faktor = 10;
   QPixmap original_pixmap;
   if(screen_) {
       original_pixmap = screen_->grabWindow(0);
    }

    int width  = original_pixmap.width()/scale_faktor;
    int height = original_pixmap.height()/scale_faktor;
    QPixmap scaled_pixmap = original_pixmap.scaled(width, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);

    QImage scaled_image = scaled_pixmap.toImage();

    //scaled_image.save("/home/wowa/test.png", "PNG");


    for (int y=0; y<scaled_image.height(); ++y) {
        for (int x=0; x<scaled_image.width(); ++x) {
            QColor color(scaled_image.pixel(x,y));
        }
    }
}

如果我运行这个程序,我的CPU通常使用大约4%,这对我来说太过分了。有什么方法可以加快速度吗?

1 个答案:

答案 0 :(得分:0)

一般方法是分析。 在windows中,你可以使用visual studio,在linux中,valgrind可以提供帮助。