Qt接收mjpg-streamer视频流解析.docx
《Qt接收mjpg-streamer视频流解析.docx》由会员分享,可在线阅读,更多相关《Qt接收mjpg-streamer视频流解析.docx(19页珍藏版)》请在冰点文库上搜索。
![Qt接收mjpg-streamer视频流解析.docx](https://file1.bingdoc.com/fileroot1/2023-4/28/9eef0180-5738-4732-9c86-9cb050cb82b1/9eef0180-5738-4732-9c86-9cb050cb82b11.gif)
利用Qt的label控件以图片格式接收mjpg-streamer视频流
核心思想:
就是利用定时器QTimer定时向服务器请求下载一张图片(也是QT网络中http编程啦)
1、利用到的类有QPixmap,Qlabel,QTimer,QThread,QFileDialog,QDir,其中我为了布局稍微美观点,加了Phonon类来实现简易音乐播放器,该类对音乐文件有要求,wmv,wmw等一些格式(可用软件格式工厂来转换音乐格式),有一些类是当时学习时为实现功能加的,忘了删掉,但是没问题的。
2、声明Qt库版本为4.8.5版本
3、以下为cpp内容
#include"mainwindow.h"
#include"ui_mainwindow.h"
#include"QPixmap"
#include"QLabel"
#include"QtCore"
#include"QTimer"
#include"QThread"
#include"software.h"
#include"phonon/phonon"
#include"QFileDialog"
#include"QDir"
#include"QDesktopServices"
MainWindow:
:
MainWindow(QWidget*parent):
QMainWindow(parent),
ui(newUi:
:
MainWindow)
{
ui->setupUi(this);
//setWindowFlags(Qt:
:
FramelessWindowHint|Qt:
:
WindowSystemMenuHint|Qt:
:
WindowMinimizeButtonHint);//无边框
img=newQImage;
scaledimg=newQImage;
this->setWindowTitle("实时视频监控(作者:
存w在QQ:
1206110987)");
SetWindow();
InitStatusBar();//初始化状态栏
Mkdirfile();
showTime=newQTimer;
showTime->setInterval(100);//毫秒级
QObject:
:
connect(showTime,SIGNAL(timeout()),this,SLOT(flushPic()));
manager=newQNetworkAccessManager(this);
QObject:
:
connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(slot_replyFinished(QNetworkReply*)));
QObject:
:
connect(ui->Openaction,SIGNAL(triggered()),this,SLOT(OpenFileNameSlot()));
QObject:
:
connect(ui->Quitaction,SIGNAL(triggered()),qApp,SLOT(quit()));
QObject:
:
connect(ui->AboutQtaction,SIGNAL(triggered()),qApp,SLOT(aboutQt()));//关于Qt
QObject:
:
connect(ui->Softwareaction,SIGNAL(triggered()),this,SLOT(slot_software()));//关于软件
QObject:
:
connect(ui->LANaction,SIGNAL(triggered()),this,SLOT(slot_LAN()));
QObject:
:
connect(ui->WANaction,SIGNAL(triggered()),this,SLOT(slot_WAN()));
button_movieIsClicked=false;//初始化ButtonMovie按钮的状态
IsLan=true;//初始化内网模式
//音频
audio=newPhonon:
:
MediaObject(this);//媒体对象
audio->setTickInterval
(1);
audioOutput=newPhonon:
:
AudioOutput(Phonon:
:
MusicCategory,this);//音频输出
Phonon:
:
createPath(audio,audioOutput);//连接媒体对象与音频输出
volumeSlider=newPhonon:
:
VolumeSlider(audioOutput,this);//音量滑动条
volumeSlider->move(650,520);
volumeSlider->resize(200,20);
volumeSlider->setStyleSheet("background-color:
rgb(255,255,255,100)");
volumeSlider->setFixedWidth(100);//固定音量条大小
seekSlider=newPhonon:
:
SeekSlider(audio,this);//进度滑动条
seekSlider->move(250,525);
seekSlider->resize(350,20);
seekSlider->setStyleSheet("background-color:
rgb(255,255,255,100)");
creatActions();
QObject:
:
connect(audio,SIGNAL(tick(qint64)),this,SLOT(UpdateTime(qint64)));//时间更新
QObject:
:
connect(audio,SIGNAL(aboutToFinish()),this,SLOT(AboutToFinish()));
QObject:
:
connect(ui->OpenWAVaction,SIGNAL(triggered()),this,SLOT(slot_OpenMusic()));
musicInformationMediaObject=newPhonon:
:
MediaObject(this);//音乐信息对象
metaInformationAudioOutput=newPhonon:
:
AudioOutput(Phonon:
:
MusicCategory,this);
Phonon:
:
createPath(musicInformationMediaObject,metaInformationAudioOutput);
QObject:
:
connect(ui->tableWidget,SIGNAL(customContextMenuRequested(constQPoint&)),this,SLOT(ShowContextMenu(constQPoint&)));
QObject:
:
connect(audio,SIGNAL(stateChanged(Phonon:
:
State,Phonon:
:
State)),this,SLOT(StateChanged(Phonon:
:
State,Phonon:
:
State)));
QObject:
:
connect(musicInformationMediaObject,SIGNAL(stateChanged(Phonon:
:
State,Phonon:
:
State)),this,SLOT(MetaStateChanged(Phonon:
:
State,Phonon:
:
State)));
QObject:
:
connect(audio,SIGNAL(currentSourceChanged(Phonon:
:
MediaSource)),this,SLOT(SourceChanged(Phonon:
:
MediaSource)));
QObject:
:
connect(ui->tableWidget,SIGNAL(cellDoubleClicked(int,int)),this,SLOT(tableClicked(int)));
}
MainWindow:
:
~MainWindow()
{
deleteui;
QFile:
:
remove("mjpg-streamer/temp1.jpg");
}
voidMainWindow:
:
slot_replyFinished(QNetworkReply*reply)
{
if(button_movieIsClicked==true)
{
QPixmappix;
QByteArraydata=reply->readAll();
pix.loadFromData(data,"JPG");
pix.save("mjpg-streamer/tmp1.jpg","JPG",100);//文件名图像格式压缩率图片质量
if(!
(img->load("mjpg-streamer/tmp1.jpg")))
{
showTime->stop();
QMessageBox:
:
information(this,tr("打开图像失败"),tr("打开图像失败!
"));
button_movieIsClicked=false;
return;
}
*scaledimg=img->scaled(640,480,Qt:
:
KeepAspectRatio);
ui->PicLabel->setPixmap(QPixmap:
:
fromImage(*scaledimg));
}
else
{
QPixmappixmap(":
/new/prefix1/背景.jpg");
ui->PicLabel->setPixmap(pixmap);
}
}
voidMainWindow:
:
flushPic()
{
request.setUrl(QUrl(PU));
manager->get(request);
}
voidMainWindow:
:
SetWindow()
{
this->resize(861,572);
ui->PicLabel->setGeometry(230,0,640,480);
//ui->PicLabel->setText("视频通道一");
ui->ButtonMovie->setGeometry(10,160,51,23);
ui->PicLabel->setFrameShape(QFrame:
:
Box);//边框
ui->PicLabel->setStyleSheet("border:
2pxsolidblack");//#999999");//红色边框风格ff0000
ui->PicLabel->setStyleSheet("QLabel{border-image:
url(:
/new/prefix1/wifi.jpg);}");
ui->TaiLongLabel->setStyleSheet("QLabel{border-image:
url(:
/new/prefix1/TaiLong.png);}");
ui->ButtonMovie->setText("视频");
}
voidMainWindow:
:
InitStatusBar()//初始化状态栏
{
QStatusBar*bar=ui->statusBar;//获取状态栏
first_statusLabel=newQLabel;//新建标签
first_statusLabel->setMinimumSize(100,10);//设置标签最小尺寸
//first_statusLabel->setFrameShape(QFrame:
:
WinPanel);//设置标签形状
first_statusLabel->setFrameShadow(QFrame:
:
Sunken);//设置标签阴影
second_statusLabel=newQLabel;
second_statusLabel->setMinimumSize(200,10);
//second_statusLabel->setFrameShape(QFrame:
:
WinPanel);
second_statusLabel->setFrameShadow(QFrame:
:
Sunken);
bar->addWidget(first_statusLabel);
bar->addWidget(second_statusLabel);
QDateTimetime=QDateTime:
:
currentDateTime();//获取系统现在的时间
QStringstr=time.toString("yyyy-MM-ddhh:
mm:
ssddd");//设置显示格式
first_statusLabel->setText(tr("就绪"));//初始化内容
second_statusLabel->setText(str);//在标签上显示时间
}
voidMainWindow:
:
Mkdirfile()
{
/*QDir*temp=newQDir;
boolexist=temp->exists("F:
//temp");
//if(exist)
//QMessageBox:
:
information(this,tr("创建文件夹"),tr("文件夹已经存在!
"));
if(!
exist)
{
temp->mkdir("F:
//temp");
}*/
if(!
QDir("mjpg-streamer").exists())//相对路径
{
QDirdir;
dir.mkpath("mjpg-streamer");
}
if(!
QDir("photos").exists())
{
QDirdir;
dir.mkpath("photos");
}
if(!
QDir("Musics").exists())
{
QDirdir;
dir.mkpath("Musics");
}
}
voidMainWindow:
:
slot_software()//关于软件dialog窗口
{
SoftWare*aboutsoftware=newSoftWare;
aboutsoftware->show();
}
voidMainWindow:
:
slot_WAN()
{
IsLan=false;
QMessageBox:
:
information(this,tr("模式切换"),tr("远程视频模式!
"));
}
voidMainWindow:
:
slot_LAN()
{
IsLan=true;
QMessageBox:
:
information(this,tr("模式切换"),tr("内网视频模式!
"));
}
voidMainWindow:
:
on_ButtonMovie_clicked()
{
if(IsLan==true)//判断是否为内网视频
{Address=ui->AddressLineEdit->text();
Port=ui->PortlineEdit->text();
PU="http:
//"+Address+":
"+Port+"/?
action=snapshot";
}
else
{
RemoteAddress=ui->RemoteLineEdit->text();
RemotePort=ui->RemotePortlineEdit->text();
PU="http:
//"+RemoteAddress+"/?
action=snapshot";
}
if(button_movieIsClicked==false)
{
button_movieIsClicked=true;
ui->ButtonMovie->setText("结束");
showTime->start();
}
else
{
button_movieIsClicked=false;
ui->ButtonMovie->setText("视频");
showTime->stop();
}
}
voidMainWindow:
:
on_ScreenshotButton_clicked()//截图
{
QStringstr_time,photo;
QDateTimetime=QDateTime:
:
currentDateTime();
str_time=time.toString("yyyy-MM-ddhh-mm-ss");
photo="photos/"+str_time+".jpg";
QPixmap:
:
fromImage(*img).save(photo,"JPG",100);//和load相似
/*QMessageBox:
:
information(this,
tr("save"),
tr("PICTUREhasbeensavedtofolder'photo!
'"));*/
}
voidMainWindow:
:
OpenFileNameSlot()
{
QStringFileName=QFileDialog:
:
getOpenFileName(this,tr("打开图片"),"temp",
tr("Images(*.png*.xpm*.jpg)"));
if(FileName.isEmpty())
{
QMessageBox:
:
information(this,"错误信息","请选择图片");
return;
}
QImagesrc(FileName);
ui->PicLabel->setPixmap(QPixmap:
:
fromImage(src));
}
//以下为音频输出函数
voidMainWindow:
:
creatActions()
{
ui->Next->setEnabled(false);
ui->Last->setEnabled(false);
ui->PlayPause->setEnabled(false);
ui->Stop->setEnabled(false);
ui->PlayPause->setIcon(QIcon(":
/new/prefix1/Pause.png"));
ui->tableWidget->setContextMenuPolicy(Qt:
:
CustomContextMenu);
ui->tableWidget->setRowCount(0);//一定要初始化行和列作为一个标度
ui->tableWidget->setColumnCount
(2);
ui->tableWidget->setColumnWidth(0,2*ui->tableWidget->width()/3);
ui->tableWidget->setColumnWidth(1,ui->tableWidget->width()/3);
//ui->tableWidget->setHorizontalScrollBarPolicy(Qt:
:
ScrollBarAlwaysOn);
QStringListlist;
list<
ui->tableWidget->setHorizontalHeaderLabels(list);
ui->tableWidget->setSelectionMode(QAbstractItemView:
:
SingleSelection);//设置只能选择单行
ui->tableWidget->setSelectionBehavior(QAbstractItemView:
:
SelectRows);
ui->tableWidget->setShowGrid(false);//设置不显示网格
}
voidMainWindow:
:
slot_OpenMusic()
{
QStringListLists=QFileDialog:
:
getOpenFileNames(this,tr("打开音乐文件"),"Musics");//,tr("*.wmv"));
//使用QFileDialog的getOpenFileNames方法获取若干个音乐文件
intindex=sourceList.size();
QStringfile;
foreach(file,Lists)//使用Qt中的foreach遍历每个选中的文件,将其添加到播放列表中。
{
Phonon:
:
MediaSourcesource(file);
sourceL
展开阅读全文
相关搜索
|