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

效果

在这里插入图片描述

总结

一个简单的测试效果完成了,效果还不错。

原网站

版权声明
本文为[God港]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_38491692/article/details/125084978