我想每隔100毫秒做一次扫描,并分析像素。我已经得到了这个代码:
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%左右,这对我来说太多了。有没有什么方法可以加快速度呢?
发布于 2015-12-05 20:54:18
一种通用的方法是分析。在windows中,您可以使用visual studio,而在linux中,valgrind可以提供帮助。
https://stackoverflow.com/questions/34105054
复制相似问题