当前位置:网站首页>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 .
边栏推荐
- Cesium(3):ThirdParty/zip. js
- PHP realizes wechat applet face recognition and face brushing login function
- 【DesignMode】代理模式(proxy pattern)
- Usage of config in laravel
- [vulnhub range] thales:1
- skimage学习(3)——Gamma 和 log对比度调整、直方图均衡、为灰度图像着色
- 3000 words speak through HTTP cache
- Lowcode: four ways to help transportation companies enhance supply chain management
- [PHP] PHP interface inheritance and interface multi inheritance principle and implementation method
- Opencv personal notes
猜你喜欢

C语言进阶——函数指针

Introduction and use of gateway

skimage学习(3)——使灰度滤镜适应 RGB 图像、免疫组化染色分离颜色、过滤区域最大值

AutoLISP series (3): function function 3

DNS 系列(一):为什么更新了 DNS 记录不生效?

【Vulnhub靶场】THALES:1

全网“追杀”钟薛高

skimage学习(2)——RGB转灰度、RGB 转 HSV、直方图匹配

The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit

掌握这套精编Android高级面试题解析,oppoAndroid面试题
随机推荐
[designmode] facade patterns
Usage of config in laravel
Prediction - Grey Prediction
Read PG in data warehouse in one article_ stat
直接上干货,100%好评
深度监听 数组深度监听 watch
掌握这套精编Android高级面试题解析,oppoAndroid面试题
JS中null NaN undefined这三个值有什么区别
Find tags in prefab in unity editing mode
Advanced C language -- function pointer
os、sys、random标准库主要功能
Geoserver2.18 series (5): connect to SQLSERVER database
Pycharm terminal enables virtual environment
logback. XML configure logs of different levels and set color output
Lowcode: four ways to help transportation companies enhance supply chain management
DAPP defi NFT LP single and dual currency liquidity mining system development details and source code
Pycharm IDE下载
无法将“pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
Imitate the choice of enterprise wechat conference room
值得一看,面试考点与面试技巧