当前位置:网站首页>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

 Insert picture description here

summary

A simple test effect is completed , It's not bad .

原网站

版权声明
本文为[God port]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071512254585.html