使用Qt 开发一个屏幕录像程序

来源:互联网 发布:交换机端口设置 编辑:程序博客网 时间:2024/05/21 15:33

 

作者:丁冬,华清远见嵌入式学院讲师。

使用Qt 来开发一个屏幕录像程序,需要使用到QPixmap 来保存图片,类似于早期电影,播放一样,每一个帧都被保存为一个图片,最后使用windows 自带的工具来合成一个视频。

如果需要使用到获得当前帧,那么就需要使用到一个定时器,下面的代码中包含了针对于一个定时器的编程,程序完成的功能是开启定时器,并没有关闭,读者有兴趣可以创建一个类内可见的定时器,使得多个部件可以控制。

这个例子是从屏幕截图程序变化过来,所以在最后使用的是按钮保存的效果。

#ifndef MAINWINDOW_H
        #define MAINWINDOW_H

#include <QMainWindow>
        #include <QTimer>
        #include <QPixmap>
        #include <QDesktopWidget>
        #include <QFileDialog>
        #include <QDir>
        #include <QDateTime>
        #include <QtDebug>

包含的头文件,其中最重要的是QDesktopService. 用来获得当前屏幕的截图 。每一个 屏幕都可以被看作是具有特定id 的窗口,想获得当前窗口就必须要制定当前的winId ,程序当中是使用QApplicaiton::desktop()->winId() 来获得。

下面是设计的图形用户界面:

namespace Ui {
                class MainWindow;
        }

class MainWindow : public QMainWindow {
                Q_OBJECT
        public:
                MainWindow(QWidget *parent = 0);
                ~MainWindow();
                long    counter;

protected:
                void changeEvent(QEvent *e);

private:
                Ui::MainWindow *ui;
                QTimer *timer;
                QPixmap pixmap;

private slots:
                void on_saveButton_clicked();
                void on_screenShotButton_clicked();
                void shotScreen();
        };

#endif // MAINWINDOW_H

#include "mainwindow.h"
        #include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
        {
                ui->setupUi(this);
        }

MainWindow::~MainWindow()
        {
                delete ui;
        }

void MainWindow::changeEvent(QEvent *e)
        {
                QMainWindow::changeEvent(e);
                switch (e->type()) {
                case QEvent::LanguageChange:
                        ui->retranslateUi(this);
                        break;
                default:
                        break;
                }
        }

void MainWindow::on_screenShotButton_clicked()
        {
                if(ui->hideCheckBox->isChecked()) //判断当前的复选框是否被选中, 如果选中
                {
                        //this->hide(); //窗口隐藏
                        this->counter=0;
                        this->timer=new QTimer; //设置定时器
                        QObject::connect(this->timer,SIGNAL(timeout()),this,SLOT(shotScreen()));
                        this->timer->start(150);
                        //使用spinbox 获得当前延时的时间
                }
                else
                {
                        qApp->beep(); //如果没有选择复选框, 那么发出蜂鸣声
                }
        }

void MainWindow::shotScreen()
        {

this->pixmap=QPixmap::grabWindow(QApplication::desktop()->winId(),0,0,1000,600);
        //通过使用grapWindow 来获得整个屏幕的图片, 并赋给pixmap

ui->screenLabel->setPixmap(this->pixmap.scaled(ui->screenLabel->size()));
        QString format =".png";
        //QString fileName=QFileDialog::getSaveFileName(this,"Save Picture",QDir::currentPath());
        //通过文件对话框来获得保存的路径与名称。
        // QString fileName=QDateTime::currentDateTime().toString("hh:mm:ss");
        this->counter++;
        QString fileName=QString::number(this->counter);
        qDebug()<<fileName;
        fileName.append(format); //fileName=fileName+format;
        qDebug()<<fileName;
        bool ok= this->pixmap.save(fileName);
        if(ok)
                qDebug()<<"save success";
        //使用label 的setPixmap 来设置图片, 大小是使用scaled 缩放到当前label 标签的大小
        //this->timer->stop(); //结束定时器
        //this->show(); //主窗口显示出来

}

void MainWindow::on_saveButton_clicked()
        {
                QString format =".jpg";
                //QString                fileName=QFileDialog::getSaveFileName(this,"Save Picture",QDir::currentPath());
                //通过文件对话框来获得保存的路径与名称。
                QString fileName=QDateTime::currentDateTime().toString();
                //fileName.append(format); //fileName=fileName+format;
                this->pixmap.save(fileName,format.toAscii());
                //调用 save 方法保存当前图片到指定路径
        }

事实上这个程序提供了一个全局的定时器,用来按照名称来保存图片,当程序执行过程中就会生成大量的图片。所以在定时器上可以通过定时来确定获得的帧数。

内部加上了一些调试代码,大家可以尝试书写更多的代码 ,来完善的功能。

如果您喜欢这篇文章,可以加华清远见老师为好友,单击以下链接即可:

 
http://student.csdn.net/invite.php?u=45153&c=8af704eb3cd8e773
 

(作者:华清远见嵌入式培训中心   www.embedu.org    www.farsight.com.cn  )

原创粉丝点击