所写代码

wangjing_develop
王京 1 year ago
parent f3a9eae15a
commit e0496edc22

@ -0,0 +1,44 @@
#include "getpixmap.h"
#include <QDebug>
GetPixmap::GetPixmap(QObject *parent) :
QObject(parent)
{
}
GetPixmap::~GetPixmap()
{
}
void GetPixmap::mjpeg_streamer_reply(QNetworkReply *reply)
{
QByteArray byteArr = reply->readAll();
QPixmap pix;
pix.loadFromData(byteArr);
emit get_getOnePixmap(pix);
manager->get(*request);
reply->deleteLater();
}
void GetPixmap::run()
{
QString url;
url.append("http://" + get_ipAddr + ":" + get_port + "/?action=snapshot");
qDebug() << url;
// qDebug() << "ipAddr:" << get_ipAddr;
// qDebug() << "port:" << get_port;
manager = new QNetworkAccessManager();
request = new QNetworkRequest(QUrl(url));
connect(manager,SIGNAL(finished(QNetworkReply*)), this, SLOT(mjpeg_streamer_reply(QNetworkReply*)));
manager->get(*request);
}

@ -0,0 +1,38 @@
#ifndef GETPIXMAP_H
#define GETPIXMAP_H
#include <QString>
#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QByteArray>
#include <QPixmap>
class GetPixmap : public QObject
{
Q_OBJECT
public:
GetPixmap(QObject *parent = nullptr);
~GetPixmap();
public:
QString get_ipAddr;
QString get_port;
void run();
private slots:
void mjpeg_streamer_reply(QNetworkReply*);
signals:
void get_getOnePixmap(QPixmap);
private:
QNetworkAccessManager *manager;
QNetworkRequest *request;
};
#endif // GETPIXMAP_H

@ -0,0 +1,82 @@
#include "loginwindow.h"
#include "ui_loginwindow.h"
#include <QMessageBox>
LoginWindow::LoginWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::LoginWindow)
{
ui->setupUi(this);
waitCnt = 0;
text.append("Login");
init();
}
LoginWindow::~LoginWindow()
{
delete ui;
}
void LoginWindow::init()
{
this->setWindowTitle("Please to Login");
ui->le_ipAddr->setText("219.216.115.53");
ui->le_port->setText("8080");
}
void LoginWindow::timerFinish()
{
if(tcpClient->state() == QTcpSocket::ConnectedState){
waitCnt = 0;
time->stop();
this->close();
main = new MainWindow;
main->main_ipAddr = login_ipAddr;
main->main_port = login_port;
main->init();
main->show();
}else{
waitCnt++;
if(waitCnt%10 == 3)
text = "Login.";
else if(waitCnt%10 == 6)
text = "Login..";
else if(waitCnt%10 == 9)
text = "Login...";
ui->btn_login->setText(text);
if(waitCnt == 50){
time->stop();
waitCnt = 0;
ui->btn_login->setText("Login");
QMessageBox::warning(this, "Warning", "Please to Check params\nand Login again!", QMessageBox::Ok);
}
}
}
void LoginWindow::on_btn_login_clicked()
{
login_ipAddr = ui->le_ipAddr->text();
login_port = ui->le_port->text();
tcpClient = new QTcpSocket(this);
tcpClient->connectToHost(login_ipAddr, login_port.toInt());
time = new QTimer(this);
time->setInterval(100);
connect(time, SIGNAL(timeout()), this, SLOT(timerFinish()));
time->start();
}
void LoginWindow::on_btn_logout_clicked()
{
this->close();
}

@ -0,0 +1,37 @@
#ifndef LOGINWINDOW_H
#define LOGINWINDOW_H
#include <QMainWindow>
#include <QTcpSocket>
#include <QTimer>
#include "mainwindow.h"
QT_BEGIN_NAMESPACE
namespace Ui { class LoginWindow; }
QT_END_NAMESPACE
class LoginWindow : public QMainWindow
{
Q_OBJECT
public:
LoginWindow(QWidget *parent = nullptr);
~LoginWindow();
private slots:
void timerFinish();
void on_btn_login_clicked();
void on_btn_logout_clicked();
private:
Ui::LoginWindow *ui;
QString login_ipAddr;
QString login_port;
int waitCnt;
QTimer *time;
QString text;
QTcpSocket *tcpClient;
MainWindow *main;
void init();
};
#endif // LOGINWINDOW_H

@ -0,0 +1,13 @@
#include "loginwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
LoginWindow w;
w.setWindowFlags(Qt::FramelessWindowHint);
w.show();
return a.exec();
}

@ -0,0 +1,97 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QThread>
#include <QMessageBox>
#include <QFile>
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
main_btnStartSta = true;
main_state = true;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::init()
{
ui->lb_vedio->setScaledContents(main_state);
this->setWindowTitle("Camera Client");
this->setWindowFlags(this->windowFlags()&~(Qt::WindowMaximizeButtonHint|Qt::WindowCloseButtonHint));
}
void MainWindow::on_btn_start_clicked()
{
QString name;
if ((main_btnStartSta = !main_btnStartSta) == false)
{
name = "Stop";
getPixmap = new GetPixmap;
thread = new QThread();
getPixmap->get_ipAddr = main_ipAddr;
getPixmap->get_port = main_port;
getPixmap->moveToThread(thread);
void (GetPixmap:: *sig_getOnePixmap)(QPixmap) = &GetPixmap::get_getOnePixmap;
void (MainWindow:: *slot_getOnePixmap)(QPixmap) = &MainWindow::main_getOnePixmap;
connect(getPixmap,sig_getOnePixmap,this,slot_getOnePixmap);
thread->start();
connect(thread,&QThread::started,getPixmap,&GetPixmap::run);
}
else
{
name = "Start";
thread->exit();
disconnect(getPixmap,SIGNAL(get_getOnePixmap(QPixmap)),this,NULL);
delete getPixmap;
}
ui->btn_start->setText(name);
}
void MainWindow::on_btn_screenshot_clicked()
{
save_pix = show_pix;
if (save_pix.isNull())
{
QMessageBox::information(this,tr("Info"),tr("There are no images to save."));
}
else
{
QString fileName = QFileDialog::getSaveFileName(this,tr("Save File"),QDir::homePath(),tr("jpegfile(*.jpg)"));
if (fileName.isEmpty())
{
QMessageBox::information(this,"Infor",tr("Save Cancel"));
return;
}
save_pix.save(fileName);
}
}
void MainWindow::on_btn_quit_clicked()
{
this->close();
}
void MainWindow::main_getOnePixmap(QPixmap pix)
{
show_pix = pix;
ui->lb_vedio->setPixmap(show_pix);
ui->lb_vedio->show();
}

@ -0,0 +1,44 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPixmap>
#include "getpixmap.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
QString main_ipAddr;
QString main_port;
void init();
private slots:
void on_btn_start_clicked();
void on_btn_screenshot_clicked();
void on_btn_quit_clicked();
void main_getOnePixmap(QPixmap);
private:
Ui::MainWindow *ui;
bool main_state = false;
bool main_btnStartSta = false;
GetPixmap *getPixmap;
QThread *thread;
QPixmap show_pix;
QPixmap save_pix;
};
#endif // MAINWINDOW_H

@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>658</width>
<height>384</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget"/>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>658</width>
<height>28</height>
</rect>
</property>
<widget class="QMenu" name="menuros">
<property name="title">
<string>ros</string>
</property>
</widget>
<addaction name="menuros"/>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>

@ -0,0 +1,47 @@
#-------------------------------------------------
#
# Project created by QtCreator 2023-04-16T19:37:18
#
#-------------------------------------------------
QT += core gui
QT += network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = mjpg_stream_browser
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
CONFIG += c++11
SOURCES += \
getpixmap.cpp \
loginwindow.cpp \
main.cpp \
mainwindow.cpp
HEADERS += \
getpixmap.h \
loginwindow.h \
mainwindow.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
QMAKE_CXXFLAGS += -std=c++0x

@ -0,0 +1,279 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.9.2, 2023-04-17T01:02:52. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
<value type="QByteArray">{2308cb99-24d4-4815-a674-8d51d824ca03}</value>
</data>
<data>
<variable>ProjectExplorer.Project.ActiveTarget</variable>
<value type="int">0</value>
</data>
<data>
<variable>ProjectExplorer.Project.EditorSettings</variable>
<valuemap type="QVariantMap">
<value type="bool" key="EditorConfiguration.AutoIndent">true</value>
<value type="bool" key="EditorConfiguration.AutoSpacesForTabs">false</value>
<value type="bool" key="EditorConfiguration.CamelCaseNavigation">true</value>
<valuemap type="QVariantMap" key="EditorConfiguration.CodeStyle.0">
<value type="QString" key="language">Cpp</value>
<valuemap type="QVariantMap" key="value">
<value type="QByteArray" key="CurrentPreferences">CppGlobal</value>
</valuemap>
</valuemap>
<valuemap type="QVariantMap" key="EditorConfiguration.CodeStyle.1">
<value type="QString" key="language">QmlJS</value>
<valuemap type="QVariantMap" key="value">
<value type="QByteArray" key="CurrentPreferences">QmlJSGlobal</value>
</valuemap>
</valuemap>
<value type="int" key="EditorConfiguration.CodeStyle.Count">2</value>
<value type="QByteArray" key="EditorConfiguration.Codec">UTF-8</value>
<value type="bool" key="EditorConfiguration.ConstrainTooltips">false</value>
<value type="int" key="EditorConfiguration.IndentSize">4</value>
<value type="bool" key="EditorConfiguration.KeyboardTooltips">false</value>
<value type="int" key="EditorConfiguration.MarginColumn">80</value>
<value type="bool" key="EditorConfiguration.MouseHiding">true</value>
<value type="bool" key="EditorConfiguration.MouseNavigation">true</value>
<value type="int" key="EditorConfiguration.PaddingMode">1</value>
<value type="bool" key="EditorConfiguration.ScrollWheelZooming">true</value>
<value type="bool" key="EditorConfiguration.ShowMargin">false</value>
<value type="int" key="EditorConfiguration.SmartBackspaceBehavior">0</value>
<value type="bool" key="EditorConfiguration.SmartSelectionChanging">true</value>
<value type="bool" key="EditorConfiguration.SpacesForTabs">true</value>
<value type="int" key="EditorConfiguration.TabKeyBehavior">0</value>
<value type="int" key="EditorConfiguration.TabSize">8</value>
<value type="bool" key="EditorConfiguration.UseGlobal">true</value>
<value type="int" key="EditorConfiguration.Utf8BomBehavior">1</value>
<value type="bool" key="EditorConfiguration.addFinalNewLine">true</value>
<value type="bool" key="EditorConfiguration.cleanIndentation">true</value>
<value type="bool" key="EditorConfiguration.cleanWhitespace">true</value>
<value type="bool" key="EditorConfiguration.inEntireDocument">false</value>
</valuemap>
</data>
<data>
<variable>ProjectExplorer.Project.PluginSettings</variable>
<valuemap type="QVariantMap">
<valuelist type="QVariantList" key="ClangCodeModel.CustomCommandLineKey"/>
<value type="bool" key="ClangCodeModel.UseGlobalConfig">true</value>
</valuemap>
</data>
<data>
<variable>ProjectExplorer.Project.Target.0</variable>
<valuemap type="QVariantMap">
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">桌面</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">桌面</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">{2651a340-77c7-4d40-971c-de5a4e32646d}</value>
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">/home/wangjing/build-mjpg_stream_browser-unknown-Debug</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">qmake</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">QtProjectManager.QMakeBuildStep</value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.LinkQmlDebuggingLibrary">false</value>
<value type="QString" key="QtProjectManager.QMakeBuildStep.QMakeArguments"></value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.QMakeForced">false</value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.SeparateDebugInfo">false</value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.UseQtQuickCompiler">false</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.1">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Make</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.MakeStep</value>
<valuelist type="QVariantList" key="Qt4ProjectManager.MakeStep.BuildTargets"/>
<value type="bool" key="Qt4ProjectManager.MakeStep.Clean">false</value>
<value type="QString" key="Qt4ProjectManager.MakeStep.MakeArguments"></value>
<value type="QString" key="Qt4ProjectManager.MakeStep.MakeCommand"></value>
<value type="bool" key="Qt4ProjectManager.MakeStep.OverrideMakeflags">false</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildStepList.StepsCount">2</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Build</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Build</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.1">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Make</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.MakeStep</value>
<valuelist type="QVariantList" key="Qt4ProjectManager.MakeStep.BuildTargets"/>
<value type="bool" key="Qt4ProjectManager.MakeStep.Clean">true</value>
<value type="QString" key="Qt4ProjectManager.MakeStep.MakeArguments">clean</value>
<value type="QString" key="Qt4ProjectManager.MakeStep.MakeCommand"></value>
<value type="bool" key="Qt4ProjectManager.MakeStep.OverrideMakeflags">false</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildStepList.StepsCount">1</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Clean</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Clean</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">2</value>
<value type="bool" key="ProjectExplorer.BuildConfiguration.ClearSystemEnvironment">false</value>
<valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.UserEnvironmentChanges"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Debug</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Debug</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4BuildConfiguration</value>
<value type="int" key="Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration">2</value>
<value type="bool" key="Qt4ProjectManager.Qt4BuildConfiguration.UseShadowBuild">true</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.1">
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">/home/wangjing/build-mjpg_stream_browser-unknown-Release</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">qmake</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">QtProjectManager.QMakeBuildStep</value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.LinkQmlDebuggingLibrary">false</value>
<value type="QString" key="QtProjectManager.QMakeBuildStep.QMakeArguments"></value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.QMakeForced">false</value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.SeparateDebugInfo">false</value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.UseQtQuickCompiler">false</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.1">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Make</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.MakeStep</value>
<valuelist type="QVariantList" key="Qt4ProjectManager.MakeStep.BuildTargets"/>
<value type="bool" key="Qt4ProjectManager.MakeStep.Clean">false</value>
<value type="QString" key="Qt4ProjectManager.MakeStep.MakeArguments"></value>
<value type="QString" key="Qt4ProjectManager.MakeStep.MakeCommand"></value>
<value type="bool" key="Qt4ProjectManager.MakeStep.OverrideMakeflags">false</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildStepList.StepsCount">2</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Build</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Build</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.1">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Make</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.MakeStep</value>
<valuelist type="QVariantList" key="Qt4ProjectManager.MakeStep.BuildTargets"/>
<value type="bool" key="Qt4ProjectManager.MakeStep.Clean">true</value>
<value type="QString" key="Qt4ProjectManager.MakeStep.MakeArguments">clean</value>
<value type="QString" key="Qt4ProjectManager.MakeStep.MakeCommand"></value>
<value type="bool" key="Qt4ProjectManager.MakeStep.OverrideMakeflags">false</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildStepList.StepsCount">1</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Clean</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Clean</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">2</value>
<value type="bool" key="ProjectExplorer.BuildConfiguration.ClearSystemEnvironment">false</value>
<valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.UserEnvironmentChanges"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Release</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Release</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4BuildConfiguration</value>
<value type="int" key="Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration">0</value>
<value type="bool" key="Qt4ProjectManager.Qt4BuildConfiguration.UseShadowBuild">true</value>
</valuemap>
<value type="int" key="ProjectExplorer.Target.BuildConfigurationCount">2</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.DeployConfiguration.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<value type="int" key="ProjectExplorer.BuildStepList.StepsCount">0</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">部署</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Deploy</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">1</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Deploy Configuration</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.DefaultDeployConfiguration</value>
</valuemap>
<value type="int" key="ProjectExplorer.Target.DeployConfigurationCount">1</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.PluginSettings"/>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.RunConfiguration.0">
<value type="QString" key="Analyzer.Perf.CallgraphMode">dwarf</value>
<valuelist type="QVariantList" key="Analyzer.Perf.Events">
<value type="QString">cpu-cycles</value>
</valuelist>
<valuelist type="QVariantList" key="Analyzer.Perf.ExtraArguments"/>
<value type="int" key="Analyzer.Perf.Frequency">250</value>
<value type="QString" key="Analyzer.Perf.SampleMode">-F</value>
<value type="bool" key="Analyzer.Perf.Settings.UseGlobalSettings">true</value>
<value type="int" key="Analyzer.Perf.StackSize">4096</value>
<value type="bool" key="Analyzer.QmlProfiler.AggregateTraces">false</value>
<value type="bool" key="Analyzer.QmlProfiler.FlushEnabled">false</value>
<value type="uint" key="Analyzer.QmlProfiler.FlushInterval">1000</value>
<value type="QString" key="Analyzer.QmlProfiler.LastTraceFile"></value>
<value type="bool" key="Analyzer.QmlProfiler.Settings.UseGlobalSettings">true</value>
<valuelist type="QVariantList" key="Analyzer.Valgrind.AddedSuppressionFiles"/>
<value type="bool" key="Analyzer.Valgrind.Callgrind.CollectBusEvents">false</value>
<value type="bool" key="Analyzer.Valgrind.Callgrind.CollectSystime">false</value>
<value type="bool" key="Analyzer.Valgrind.Callgrind.EnableBranchSim">false</value>
<value type="bool" key="Analyzer.Valgrind.Callgrind.EnableCacheSim">false</value>
<value type="bool" key="Analyzer.Valgrind.Callgrind.EnableEventToolTips">true</value>
<value type="double" key="Analyzer.Valgrind.Callgrind.MinimumCostRatio">0.01</value>
<value type="double" key="Analyzer.Valgrind.Callgrind.VisualisationMinimumCostRatio">10</value>
<value type="bool" key="Analyzer.Valgrind.FilterExternalIssues">true</value>
<value type="QString" key="Analyzer.Valgrind.KCachegrindExecutable">kcachegrind</value>
<value type="int" key="Analyzer.Valgrind.LeakCheckOnFinish">1</value>
<value type="int" key="Analyzer.Valgrind.NumCallers">25</value>
<valuelist type="QVariantList" key="Analyzer.Valgrind.RemovedSuppressionFiles"/>
<value type="int" key="Analyzer.Valgrind.SelfModifyingCodeDetection">1</value>
<value type="bool" key="Analyzer.Valgrind.Settings.UseGlobalSettings">true</value>
<value type="bool" key="Analyzer.Valgrind.ShowReachable">false</value>
<value type="bool" key="Analyzer.Valgrind.TrackOrigins">true</value>
<value type="QString" key="Analyzer.Valgrind.ValgrindExecutable">valgrind</value>
<valuelist type="QVariantList" key="Analyzer.Valgrind.VisibleErrorKinds">
<value type="int">0</value>
<value type="int">1</value>
<value type="int">2</value>
<value type="int">3</value>
<value type="int">4</value>
<value type="int">5</value>
<value type="int">6</value>
<value type="int">7</value>
<value type="int">8</value>
<value type="int">9</value>
<value type="int">10</value>
<value type="int">11</value>
<value type="int">12</value>
<value type="int">13</value>
<value type="int">14</value>
</valuelist>
<value type="int" key="PE.EnvironmentAspect.Base">2</value>
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">mjpg_stream_browser</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:/home/wangjing/mjpg_stream_browser/mjpg_stream_browser.pro</value>
<value type="QString" key="RunConfiguration.Arguments"></value>
<value type="uint" key="RunConfiguration.QmlDebugServerPort">3768</value>
<value type="bool" key="RunConfiguration.UseCppDebugger">false</value>
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value>
<value type="bool" key="RunConfiguration.UseLibrarySearchPath">true</value>
<value type="bool" key="RunConfiguration.UseMultiProcess">false</value>
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
<value type="QString" key="RunConfiguration.WorkingDirectory"></value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">/home/wangjing/build-mjpg_stream_browser-unknown-Debug</value>
</valuemap>
<value type="int" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
</valuemap>
</data>
<data>
<variable>ProjectExplorer.Project.TargetCount</variable>
<value type="int">1</value>
</data>
<data>
<variable>ProjectExplorer.Project.Updater.FileVersion</variable>
<value type="int">21</value>
</data>
<data>
<variable>Version</variable>
<value type="int">21</value>
</data>
</qtcreator>

@ -0,0 +1,137 @@
下面是一个简单的使用Qt Creator和OpenCV搭建可以播放RTSP视频的界面的示例。本例中为了展示更多读者可能需要的部分比例较为详细
首先在Qt Creator中创建一个新的Qt Widgets应用程序。
添加QLabel作为显示视频图像的容器并将该标签放置在MainWindow的UI界面上。
使用信号/槽机制连接QPushButton控件以提供“Play”和“Stop”按钮的功能。
在窗口的构造函数中初始化OpenCV中的VideoCapture对象。此处调用set函数配置FrameRingBuffer的长度从而满足读取较慢反应不够其余只调用isOpened方法判断是否可正常读入即可。
稍后你可以编写一个槽函数来读取视频流并将帧显示在QLabel中。
mainwindow.h文件
cpp
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <opencv2/core/mat.hpp>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_playButton_clicked();
void on_stopButton_clicked();
void readFrame();
private:
Ui::MainWindow *ui;
cv::VideoCapture mCapture; // Video capture object for reading RTSP stream
cv::Mat mFrame; // Mat object to store the video frame
bool mPlaying; // Flag to indicate if video playback is in progress
};
#endif // MAINWINDOW_H
mainwindow.cpp文件
cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QTimer>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Set up VideoCapture object for reading RTSP stream
//调节参数 取决于对视频流带宽要求以及本机性能 适点即可
mCapture.set(cv::CAP_PROP_BUFFERSIZE,5);
mCapture.open("rtsp://localhost:8554/unicast"); //修改RTSP地址
if (!mCapture.isOpened())
{
qCritical() << "Failed to open RTSP stream.";
return;
}
//初始化mFrame和mPlaying标记
mPlaying = false;
//设置Paly按钮为不可用状态
ui->playButton->setEnabled(true);
ui->stopButton->setEnabled(false);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_playButton_clicked()
{
if (!mPlaying)
{
mPlaying = true;
// 每20毫秒读取一帧并在QLabel显示该帧图像
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &MainWindow::readFrame);
timer->start(20);
//设置控件状态
ui->playButton->setEnabled(false);
ui->stopButton->setEnabled(true);
}
}
void MainWindow::readFrame()
{
if (mCapture.read(mFrame))
{
QImage frameImage((uchar*)mFrame.data, mFrame.cols, mFrame.rows, QImage::Format_RGB888);
QPixmap framePixmap = QPixmap::fromImage(frameImage.rgbSwapped());
// .rgbSwapped()因为OpenCV默认使用BGR而Qt使用RGB必须交换颜色通道才能正确地显示
ui->videoLabel->setPixmap(framePixmap.scaled(ui->videoLabel->size(), Qt::KeepAspectRatio));
}
}
void MainWindow::on_stopButton_clicked()
{
if (mPlaying)
{
mPlaying = false;
//停止计时器,断开信号/槽连接
QObject::disconnect(this, &MainWindow::readFrame, nullptr, nullptr);
//设置控件状态
ui->playButton->setEnabled(true);
ui->stopButton->setEnabled(false);
//清除QLabel
ui->videoLabel->clear();
}
}
注意上述代码中的RTSP地址需要根据实际情况进行修改。在这里只是将RTSP的地址写为本机地址从而获得源不被限制的可播放视频。
此示例仅演示了如何从RTSP流读取一帧并在QLabel中显示该帧图像。你可以使用QSpinBox或QSlider等Qt控件来添加FPS和播放进度条的功能。以及像界面美化异常处理带宽控制和多线程优化等其余实现可能 Related Post 中可能会涉及。

@ -0,0 +1,124 @@
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
// 坐标类
class Point {
public:
double x, y;
Point(double nx, double ny) : x(nx), y(ny) {}
};
// 图类
class Graph {
public:
Graph(vector<Point> points) {
for (int i = 0; i < points.size(); i++) {
vector<double> row;
for (int j = 0; j < points.size(); j++) {
if (i == j) {
row.push_back(0);
} else {
double dis = sqrt(pow(points[i].x - points[j].x, 2) + pow(points[i].y - points[j].y, 2));
row.push_back(dis);
}
}
matrix.push_back(row);
}
}
// Dijkstra算法求解最短路径网上嫖的
vector<Point> get_shortest_path(int start_index, int end_index) {
int node_num = matrix.size();
vector<bool> visited(node_num, false);
vector<int> prev(node_num, -1);
vector<double> dist(node_num, INFINITY);
dist[start_index] = 0;
// Dijkstra算法
for (int i = 0; i < node_num; i++) {
int u = -1;
double min_dist = INFINITY;
// 找到未访问过的距离最小的节点u
for (int j = 0; j < node_num; j++) {
if (!visited[j] && dist[j] < min_dist) {
u = j;
min_dist = dist[j];
}
}
if (u == -1) break;
visited[u] = true;
// 更新u的邻接节点v的距离和前驱
for (int v = 0; v < node_num; v++) {
if (!visited[v] && matrix[u][v] < INFINITY) {
double new_dist = dist[u] + matrix[u][v];
if (new_dist < dist[v]) {
dist[v] = new_dist;
prev[v] = u;
}
}
}
}
// 输出最短路径
vector<Point> path;
int u = end_index;
while (prev[u] != -1) {
path.push_back(points[u]);
u = prev[u];
}
reverse(path.begin(), path.end());
return path;
}
private:
vector<vector<double>> matrix; // 邻接矩阵,用来存放
vector<Point> points; // 顶点坐标列表
const double INFINITY = std::numeric_limits<double>::infinity(); // 无穷大
};
// 顶点序列类
class VertexSequence {
public:
// 构造函数
VertexSequence(vector<Point> points, Graph graph) {
this->points = points;
this->graph = graph;
start_index = 0; // 初始起点为第一个点
end_index = -1; // 结尾没有定义
sequence = graph.get_shortest_path(start_index, end_index); // 计算最短路径
}
// 重新排序方法
void sort(Point current_point) {
// 计算起点到各顶点的距离
vector<double> distances;
for (int i = 0; i < sequence.size(); i++) {
double dis = sqrt(pow(current_point.x - sequence[i].x, 2) + pow(current_point.y - sequence[i].y, 2));
distances.push_back(dis);
}
// 对顶点序列进行重新排序
sort(sequence.begin(), sequence.end(), [&distances](const Point& lhs, const Point& rhs) {
int index_lhs = &lhs - &distances[0];
int index_rhs = &rhs - &distances[0];
return distances[index_lhs] < distances[index_rhs];
});
}
Point pop() {
Point next_point = sequence.front();
sequence.erase(sequence.begin());
return next_point;
}
private:
vector<Point> points; // 顶点坐标
Graph graph; // 连通图
int start_index; // 起点
int end_index; // 结尾
vector<Point> sequence; // 顶点序列
};

@ -0,0 +1,34 @@
#include "类.h"
int main() {
PositionQueue queue;
string input_data;
while (true) {
getline(cin, input_data);
if (input_data.empty()) continue; // 如果输入为空,跳过
// 解析json
size_t pos1 = input_data.find("x");
size_t pos2 = input_data.find(",", pos1+3);
size_t pos3 = input_data.find("y", pos2+1);
size_t pos4 = input_data.find(",", pos3+3);
size_t pos5 = input_data.find("z", pos4+1);
size_t pos6 = input_data.find("}", pos5+3);
if (pos1 == string::npos || pos2 == string::npos || pos3 == string::npos || pos4 == string::npos || pos5 == string::npos || pos6 == string::npos) {
cout << "输入格式错误,请重新输入" << endl; //stringnops就是最大格式长度
continue;
}
double x = stod(input_data.substr(pos1+3, pos2-pos1-3));
double y = stod(input_data.substr(pos3+3, pos4-pos3-3));
double z = stod(input_data.substr(pos5+3, pos6-pos5-3));
queue.insert(Position(x, y, z));
queue.output_sorted();
}
return 0;
}

@ -0,0 +1,44 @@
#include <iostream>
#include <queue>
#include <vector>
#include <cmath>
#include <string>
using namespace std;
// 位置信息类
class Position {
public:
Position(double x, double y, double z) : _x(x), _y(y), _z(z) { }
double distance() const { return sqrt(_x*_x + _y*_y + _z*_z); }
friend bool operator<(const Position& a, const Position& b) { return a.distance() > b.distance(); } // 定义比较函数,用于排序
friend ostream& operator<<(ostream& os, const Position& pos) {
os << "{\"x\":" << pos._x << ",\"y\":" << pos._y << ",\"z\":" << pos._z << "}";
return os;
}
private:
double _x;
double _y;
double _z;
};
// 队列类
class PositionQueue {
public:
PositionQueue() { }
void insert(const Position& pos) { _queue.push(pos); } // 插入
void output_sorted() { // 输出
vector<Position> positions;
while (!_queue.empty()) {
positions.push_back(_queue.top());
_queue.pop();
}
for (auto& pos : positions) {
cout << pos << endl;
}
}
private:
priority_queue<Position> _queue;
};

@ -0,0 +1,56 @@
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <json/json.h>
#define BUF_SIZE 1024 // JSON数据分片大小对应的是接收端的那个size
using namespace std;
int main() {
int sockfd = socket(AF_INET, SOCK_STREAM, 0); // 创建socket
if (sockfd < 0) {
perror("socket error");
exit(1);
}
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // 目的主机IP
addr.sin_port = htons(8888); // 目的主机端口号
if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { // 连接目标主机
perror("connect error");
exit(1);
}
Json::Value json_data;
json_data["x"] = 0";
json_data["y"] = "1";
json_data["z"] = "1";
string json_str = json_data.toStyledString(); // 编码
char buf[BUF_SIZE];
int len = json_str.length(), count = 0;
sprintf(buf, "%d", len);
send(sockfd, buf, strlen(buf), 0);
for (int i = 0; i < len; i += BUF_SIZE) {
int size = min(BUF_SIZE, len - i);
memcpy(buf, json_str.c_str() + i, size);
send(sockfd, buf, size, 0);
printf("Send JSON packet %d\n", ++count);
usleep(10000); // 10msxunhuan
}
close(sockfd);
return 0;
}

@ -0,0 +1,78 @@
//接收端
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <json/json.h>
using namespace std;
int main() {
//创建
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("socket error");
exit(1);
}
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(8888); // 绑定端口号,改!
if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("error");
exit(1);
}
if (listen(sockfd, 5) < 0) { // 监听socket
perror("listen error");
exit(1);
}
int connfd;
struct sockaddr_in cliaddr;
socklen_t clilen = sizeof(cliaddr);
if ((connfd = accept(sockfd, (struct sockaddr *)&cliaddr, &clilen)) < 0) { // 接受连接
perror("accept error");
exit(1);
}
char buf[BUF_SIZE];
int len, count = 0;
memset(buf, 0, sizeof(buf));
recv(connfd, buf, size, 0); // 接收JSON数据长度size 通常是1024
len = atoi(buf);
string json_str;
for (int i = 0; i < len; i += BUF_SIZE) { // 分批接收JSON数据
int size = min(BUF_SIZE, len - i);
recv(connfd, buf, size, 0);
printf("Receive JSON packet %d\n", ++count); // 打印接收的JSON数据包编号
json_str.append(buf, size);
usleep(10000); // 10ms循环
}
Json::Value json_data;
Json::Reader reader;
if (!reader.parse(json_str, json_data)) { // 解码为JSON对象
cerr << "parse error" << endl;
exit(1);
}
cout << "x: " << json_data["x"].asString() << endl;
cout << "y: " << json_data["y"].asString() << endl;
cout << "z: " << json_data["z"].asString() << endl;
close(connfd);
close(sockfd);
return 0;
}

@ -0,0 +1,42 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <opencv2/opencv.hpp>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
const std::string stream_url = "http://localhost:8080/?action=stream";
cv::VideoCapture cap(stream_url);
if (!cap.isOpened())
{
qDebug() << "Failed to open MJPEG Streamer video capture";
return;
}
QTimer* timer = new QTimer(this);
connect(timer, &QTimer::timeout, [=]()
{
cv::Mat frame;
cap.read(frame);
if (frame.empty())
{
qDebug() << "Failed to capture MJPEG Streamer video frame";
return;
}
QImage qimg((uchar*)frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888);
ui->label->setPixmap(QPixmap::fromImage(qimg));
});
timer->start(33);
}
MainWindow::~MainWindow()
{
delete ui;
}
Loading…
Cancel
Save