recordscreen.h
#ifndef RECORD_SCREEN_H
#define RECORD_SCREEN_H
#include <QtGui>
#include <QTime>
#include <QWidget>
#include "avilib.h"
class RecordScreen:public QObject {
Q_OBJECT
public:
RecordScreen();
~RecordScreen();
void screenStart();
void screenEnd();
public slots:
void shotScreen();
private:
QTimer* timer;
avi_t* avi;
bool isStop = false;
};
#endif // !RECORD_SCREEN_H
recordscreen.cpp
#include "recordscreen/recordscreen.h"
#include <QObject>
#include <QDesktopWidget>
#include <QtWidgets/QApplication>
#include <QApplication>
#include <QScreen>
#include <QBuffer>
#include <QFile>
RecordScreen::RecordScreen()
{
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(shotScreen()));
}
/**
* 开始录屏
*/
void RecordScreen::screenStart()
{
QDesktopWidget *desktop = QApplication::desktop();
timer->start(100);
QString path = QDir::currentPath();//当前工作路径
QString videopath = path + "/images/screenVideo/";
QDir dir(videopath);
QString cou = QString::number(dir.count() - 1);
QString videoNamePath = videopath + "Videotest" + cou + ".avi";
QByteArray ba = videoNamePath.toLatin1();
char *cPath = ba.data();
avi = AVI_open_output_file(cPath);
AVI_set_video(avi, desktop->width(), desktop->height(), 5, "mjpg");
}
void RecordScreen::screenEnd()
{
isStop = true;
timer->stop();
AVI_close(avi);
}
void RecordScreen::shotScreen()
{
int width = QApplication::desktop()->width();
int height = QApplication::desktop()->height();
QPixmap pix = QPixmap::grabWindow(QApplication::desktop()->winId(), 0, 0, width, height);
QByteArray ba;
QBuffer bf(&ba);
if (pix.save(&bf, "jpg", 30) && !isStop)
{
AVI_write_frame(avi, ba.data(), ba.size(), 1);
}
}
RecordScreen::~RecordScreen()
{
}
screenthread
#pragma once
#include <QThread>
#include "recordscreen/recordscreen.h"
class ScreenThread :public QThread {
public:
RecordScreen* record;
void ScreenThread::run()
{
record = new RecordScreen;
record->screenStart();
exec();
}
void ScreenThread::end()
{
record->screenEnd();
ScreenThread::~ScreenThread()
{
if (record)
{
delete record;
record = NULL;
}
}
};
两个avilib文件下载链接:
https://download.csdn.net/download/u013693952/12089553