一個基於Qt的截屏程序


最近有一個arm板上的程序需要重寫用戶手冊,在網上找了許久,沒找到合適的截屏工具。於是只好自己動手做一個了。

因為arm板上有已經有了Qt環境,於是想到用 Qt的QPixmap::grabWindow 直接抓屏即可。

部署到arm設備后用com連接設備,抓屏,一步OK!雖然簡單,但給編寫手冊的小伙伴們使用足夠了。

代碼沒幾行,貼上全部代碼了。

#include <QApplication>
#include <QPixmap>
#include <QDesktopWidget>
#include <QDateTime>
#include <QFileInfo>
#include <iostream>
using namespace std;

void printHelp()
{
    QString msg =
            "grab screen tool v1.0.0(2016.06.15)\n"
            "author:tech@baijunjie.com\n"
            "usage: grabscreen [path]";
    cout << msg.toLocal8Bit().data() << endl;
}

int main(int argc, char *argv[])
{
    if( argc > 2 )
    {
        printHelp();
        return 0;
    }

    QApplication a(argc, argv);

    QString path;

    QStringList args = QApplication::arguments();
    for( int n = 1; n < args.size(); n++ )
    {
        QString arg = args.at( n );
        if( arg.size() > 0 )
        {
            if( arg.startsWith( "--" ) )
            {
                if( arg == "--help" )
                {
                    printHelp();
                    return 0;
                }
            }
            else
            {
                if( path.isEmpty() )
                {
                    path = arg;
                }
            }
        }
    }

    if( path.isEmpty() )
    {
        path = QString("grabscreen%1.png").arg( QDateTime::currentDateTime().toString("yyyyMMddhhmmsszzz") );
    }
    else if( path.endsWith("/") || path.endsWith("\\") )
    {
        path += QString("grabscreen%1.png").arg( QDateTime::currentDateTime().toString("yyyyMMddhhmmsszzz") );
    }

    QPixmap pixmap = QPixmap::grabWindow(QApplication::desktop()->winId());
    if( !pixmap.isNull() )
    {
        if( pixmap.save( path ) )
        {
            cout << "screen grabbed " << path.toLocal8Bit().data() << endl;
        }
        else
        {
            cout << "faile to save grabbed image to " << path.toLocal8Bit().data() << endl;
        }
    }
    else
    {
        cout << "grab screen failed!" << endl;
    }
    
    return 0;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM