您好,登錄后才能下訂單哦!
這篇文章主要介紹“怎么用QT實(shí)現(xiàn)視頻傳輸功能”,在日常操作中,相信很多人在怎么用QT實(shí)現(xiàn)視頻傳輸功能問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”怎么用QT實(shí)現(xiàn)視頻傳輸功能”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
udp客戶端,我們主要獲取某個(gè)視頻的文件,采用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())//如果視頻已經(jīng)讀取完成則停止發(fā)送timeout()信號(hào) { 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); }
獲取客戶端數(shù)據(jù)然后展現(xiàn)。代碼如下:
#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數(shù)據(jù)接收 } 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) { } }
到此,關(guān)于“怎么用QT實(shí)現(xiàn)視頻傳輸功能”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。