当前位置:网站首页>QT video transmission
QT video transmission
2022-07-07 16:58:00 【God port】
Preface
We often transmit video , In this article, we will explain QT Video transmission under .
client
udp client , We mainly get a video file , use opencv After reading , Framing transmission . We decode the picture into base-64 Format
Reference code
#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())// Stop sending if the video has been read timeout() The signal
{
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 Broadcast address
7777);
}
Server side
Get client data and show it . The code is as follows :
#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 Data reception
}
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)
{
}
}
effect
summary
A simple test effect is completed , It's not bad .
边栏推荐
- AutoLISP series (3): function function 3
- ORACLE进阶(六)ORACLE expdp/impdp详解
- [PHP] PHP interface inheritance and interface multi inheritance principle and implementation method
- three. JS create cool snow effect
- LeetCode 1696. 跳跃游戏 VI 每日一题
- skimage学习(3)——使灰度滤镜适应 RGB 图像、免疫组化染色分离颜色、过滤区域最大值
- Three. JS series (2): API structure diagram-2
- 如何选择合适的自动化测试工具?
- [designmode] facade patterns
- 低代码(lowcode)帮助运输公司增强供应链管理的4种方式
猜你喜欢
预测——灰色预测
The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit
字节跳动Android面试,知识点总结+面试题解析
Advanced C language -- function pointer
Opencv personal notes
Personal notes of graphics (3)
Pycharm IDE下载
面向接口编程
掌握这个提升路径,面试资料分享
Three. JS series (2): API structure diagram-2
随机推荐
Sort out several important Android knowledge and advanced Android development interview questions
01tire+ chain forward star +dfs+ greedy exercise one
Prometheus API deletes all data of a specified job
Introduction and use of gateway
最新高频Android面试题目分享,带你一起探究Android事件分发机制
LeetCode 1981. 最小化目标值与所选元素的差 每日一题
A tour of gRPC:03 - proto序列化/反序列化
ATM系统
Opencv personal notes
Usage of config in laravel
Inner monologue of accidental promotion
字节跳动Android面试,知识点总结+面试题解析
Pycharm IDE下载
Prediction - Grey Prediction
Laravel5.1 Routing - routing packets
node:504报错
最新Android面试合集,android视频提取音频
Three. JS series (1): API structure diagram-1
Introduction to ThinkPHP URL routing
Sqlserver2014+: create indexes while creating tables