You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
PayloadAPP/Src/Video/ffmpegvideodlg.cpp

102 lines
2.9 KiB
C++

#include "ffmpegvideodlg.h"
#include "ui_ffmpegvideodlg.h"
#include <QMessageBox>
ffmpegvideoDlg::ffmpegvideoDlg(QWidget *parent)
: QWidget(parent), ui(new Ui::ffmpegvideoDlg) {
ui->setupUi(this);
iniWindow();
}
ffmpegvideoDlg::~ffmpegvideoDlg() {
stop();
delete ui;
}
void ffmpegvideoDlg::iniWindow() {
QString VideoFilePath = QDir::currentPath() + "./Video";
QDir VideoDir(VideoFilePath);
if (!VideoDir.exists()) {
VideoDir.mkdir(VideoFilePath);
qDebug() << "文件夹创建成功";
}
}
void ffmpegvideoDlg::setUrl(QString url) { ffmpeg->setUrl(url); }
void ffmpegvideoDlg::play() {
if (!m_PlayStatus) {
m_PlayStatus = true;
ffmpeg = new Cffmpeg_decode;
ffmpeg->IsstopPlay = false;
ffmpeg->moveToThread(&workerThread);
connect(&workerThread, &QThread::finished, ffmpeg,
&QObject::deleteLater); // 线程发送结束标志
connect(this, &ffmpegvideoDlg::operate, ffmpeg,
&Cffmpeg_decode::run); // 线程开始处理数据
connect(ffmpeg, SIGNAL(sendQImage(QImage)), this,
SLOT(receiveQImage(QImage))); // 发送解析的图片
connect(ffmpeg, SIGNAL(sendConnectFail(int)), this,
SLOT(showMessagBox(int))); // 发送错误信息提示
workerThread.start();
emit this->operate(); // 启用线程信号
}
}
void ffmpegvideoDlg::stop() {
if (m_PlayStatus) {
ffmpeg->stop();
m_PlayStatus = false;
ffmpeg->IsstopPlay = true; // 线程结束标志
workerThread.quit(); // 线程退出时自动delete线程里的类
workerThread.wait();
img.fill(Qt::black);
}
}
// 绘图事件
void ffmpegvideoDlg::paintEvent(QPaintEvent *) {
QPainter painter(this);
painter.drawImage(m_ax, m_ay, img);
}
// 区域大小改变事件
void ffmpegvideoDlg::resizeEvent(QResizeEvent *event) {
// 区域1920*1080自适应
double VideoAreaHeight = this->height();
double VideoAreaWidth = this->width();
// 高度值相对比较大
if (VideoAreaHeight * 16 > VideoAreaWidth * 9) {
m_setVideoAreaWidth = VideoAreaWidth;
m_setVideoAreaHeight = VideoAreaWidth * 9 / 16;
m_ax = 0;
m_ay = 0.5 * (VideoAreaHeight - m_setVideoAreaHeight);
}
// 宽度值相对比较大
else {
m_setVideoAreaHeight = VideoAreaHeight;
m_setVideoAreaWidth = VideoAreaHeight * 16 / 9;
m_ax = 0.5 * (VideoAreaWidth - m_setVideoAreaWidth);
m_ay = 0;
}
}
void ffmpegvideoDlg::receiveQImage(const QImage &rImg) {
// img = rImg.scaled(this->size());
QSize imgsize(m_setVideoAreaWidth, m_setVideoAreaHeight);
img = rImg.scaled(imgsize);
update();
}
// 显示不同提示消息
void ffmpegvideoDlg::showMessagBox(int type) {
switch (type) {
case 1:
QMessageBox::information(NULL, tr("提示"), "获取视频失败", QMessageBox::Ok);
break;
default:
break;
}
}
bool ffmpegvideoDlg::Isplay(bool IsstopPlay) {
return ffmpeg->IsstopPlay = IsstopPlay;
}