当前位置:网站首页>QT视频传输
QT视频传输
2022-07-07 15:32:00 【God港】
前言
我们经常会进行视频的传输,这篇文章我们将讲解QT下的视频传输。
客户端
udp客户端,我们主要获取某个视频的文件,采用opencv读取后,分帧传输。我们将图片解码成base-64格式
参考代码
#include "ImageClient.h"
#include<QDebug>
#include<QBuffer>
#include<QFileDialog>
#include <opencv2/opencv.hpp>
ImageClient::ImageClient(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
counter = 0;
time = new QTimer(this);
time->start(180);
connect(time, SIGNAL(timeout()), this, SLOT(readFrame()));
cap.open("F:/1.avi");
if (!cap.isOpened())
{
qDebug() << " ===> Failed";
}
else
{
qDebug() << " ===> Succeed";
}
receiver = new QUdpSocket(this);
}
QByteArray ImageClient::getImageData2( QImage &image)
{
QByteArray imageData;
QBuffer buffer(&imageData);
image.save(&buffer, "jpg");
imageData = imageData.toBase64();
return imageData;
}
QImage ImageClient::mat2QImage(cv::Mat cvImg)
{
QImage qImg;
if (cvImg.channels() == 3) //3 channels color image
{
cv::cvtColor(cvImg, cvImg, COLOR_BGR2RGB);
qImg = QImage((const unsigned char*)(cvImg.data),
cvImg.cols, cvImg.rows,
cvImg.cols*cvImg.channels(),
QImage::Format_RGB888);
}
else if (cvImg.channels() == 1) //grayscale image
{
qImg = QImage((const unsigned char*)(cvImg.data),
cvImg.cols, cvImg.rows,
cvImg.cols*cvImg.channels(),
QImage::Format_Indexed8);
}
else
{
qImg = QImage((const unsigned char*)(cvImg.data),
cvImg.cols, cvImg.rows,
cvImg.cols*cvImg.channels(),
QImage::Format_RGB888);
}
return qImg;
}
void ImageClient::readFrame()
{
counter++;
Mat source, result;
qDebug() << counter;
cap.read(source);
if (source.empty())//如果视频已经读取完成则停止发送timeout()信号
{
time->stop();
qDebug() << "End";
return;
}
auto image = mat2QImage(source) ;
ui.label->setPixmap(QPixmap::fromImage(image));
auto data = getImageData2(image);
receiver->writeDatagram(data.data(),
data.size(),
QHostAddress::Broadcast, //udp广播地址
7777);
}
服务端
获取客户端数据然后展现。代码如下:
#include "Server.h"
#include<QMutexLocker>
#pragma execution_character_set("utf-8")
Server::Server(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
udpSocket = new QUdpSocket(this);
udpSocket->bind(QHostAddress::AnyIPv4, 7777);
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(Run()), Qt::QueuedConnection);//udp数据接收
}
QImage Server::getImage(const QString &data)
{
QByteArray imageData = QByteArray::fromBase64(data.toLatin1());
QImage image;
image.loadFromData(imageData);
return image;
}
void Server::Run()
{
try
{
QMutexLocker locker(&mutexImage);
datagram.resize(udpSocket->pendingDatagramSize());
auto len = udpSocket->readDatagram(datagram.data(), datagram.size());
auto image = Server::getImage(datagram);
ui.label->setPixmap(QPixmap::fromImage(image));
this_thread::sleep_for(std::chrono::milliseconds(180));
}
catch (const std::exception& e)
{
}
}
效果
总结
一个简单的测试效果完成了,效果还不错。
边栏推荐
- Tidb cannot start after modifying the configuration file
- LeetCode-SQL第一天
- The difference and working principle between compiler and interpreter
- Laravel5.1 Routing - routing packets
- 字节跳动Android面试,知识点总结+面试题解析
- three. JS create cool snow effect
- 47_ Contour lookup in opencv cv:: findcontours()
- SqlServer2014+: 创建表的同时创建索引
- Lowcode: four ways to help transportation companies enhance supply chain management
- [designmode] flyweight pattern
猜你喜欢
随机推荐
[C language] question set of X
LeetCode 1774. 最接近目标价格的甜点成本 每日一题
记录Servlet学习时的一次乱码
Master this set of refined Android advanced interview questions analysis, oppoandroid interview questions
[hcsd celebrity live broadcast] teach the interview tips of big companies in person - brief notes
在哪个期货公司开期货户最安全?
Imitate the choice of enterprise wechat conference room
Localstorage and sessionstorage
OpenGL personal notes
值得一看,面试考点与面试技巧
three. JS create cool snow effect
Pisa-Proxy SQL 解析之 Lex & Yacc
ORACLE进阶(六)ORACLE expdp/impdp详解
爬虫(17) - 面试(2) | 爬虫面试题库
[designmode] template method pattern
Sort out several important Android knowledge and advanced Android development interview questions
"The" "PIP" "entry cannot be recognized as the name of a cmdlet, function, script file, or runnable program."
3000 words speak through HTTP cache
DNS 系列(一):为什么更新了 DNS 记录不生效?
time标准库