QT次线程录屏

13 篇文章 1 订阅

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值