当前位置:网站首页>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 .
边栏推荐
- 【Vulnhub靶场】THALES:1
- A tour of gRPC:03 - proto序列化/反序列化
- Three. JS series (2): API structure diagram-2
- 一文读懂数仓中的pg_stat
- AutoLISP series (2): function function 2
- Opencv personal notes
- Lowcode: four ways to help transportation companies enhance supply chain management
- 字节跳动高工面试,轻松入门flutter
- node:504报错
- 爬虫(17) - 面试(2) | 爬虫面试题库
猜你喜欢

null == undefined

time标准库

【C 语言】 题集 of Ⅹ

QT 图片背景色像素处理法
ByteDance Android gold, silver and four analysis, Android interview question app

Arduino 控制的双足机器人

Personal notes of graphics (1)

掌握这套精编Android高级面试题解析,oppoAndroid面试题

Talk about the realization of authority control and transaction record function of SAP system
作为Android开发程序员,android高级面试
随机推荐
Geoserver2.18 series (5): connect to SQLSERVER database
Introduction and use of gateway
3000 words speak through HTTP cache
二叉搜索树(基操篇)
Lowcode: four ways to help transportation companies enhance supply chain management
QT中自定义控件的创建到封装到工具栏过程(一):自定义控件的创建
os、sys、random标准库主要功能
字节跳动Android金三银四解析,android面试题app
【图像传感器】相关双采样CDS
谎牛计数(春季每日一题 53)
Imitate the choice of enterprise wechat conference room
深度监听 数组深度监听 watch
LeetCode-SQL第一天
Cesium(3):ThirdParty/zip. js
Binary search tree (basic operation)
typescript ts基础知识之tsconfig.json配置选项
爬虫(17) - 面试(2) | 爬虫面试题库
数据中台落地实施之法
[designmode] flyweight pattern
字节跳动Android面试,知识点总结+面试题解析