2015-09-23 50 views
-1

我想使用QNetworkAccessManager從指定的URL獲取mjpeg流,但我失敗了。Qt5從局域網獲取mjpeg流

這是我的代碼:

#include "mainwindow.h" 
#include "ui_mainwindow.h" 

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 
    webCam = new QNetworkAccessManager(this); 
    connect(webCam,SIGNAL(finished(QNetworkReply*)),this,SLOT(replySteamFinish(QNetworkReply*))); 
    QString cam = "http://192.168.1.1:8080/?action=stream"; 
    QNetworkRequest req; 
    req.setUrl(cam); 
    webCam->get(req); 
} 

MainWindow::~MainWindow() 
{ 
    delete ui; 
} 

void MainWindow::replySteamFinish(QNetworkReply *reply) 
{ 
    if(reply->error() == QNetworkReply::NoError){ 
     QByteArray data = reply->readAll(); 
     qDebug() << data; 
    /*I just test whether receive the mjpeg stream data.*/ 
    } 
    else{ 
     qDebug() << reply->error(); 
    } 
} 

我想接收MJPEG流數據,但qDebug()我以前不工作,沒有輸出。 當我把「http://192.168.1.1:8080/?action=stream」置於chrome中時,網頁顯示mjpeg流暢。

我應該如何解決它? :)

回答

0

最後,我用QTcpSocket解決了這個問題。

#include "mainwindow.h" 
    #include "ui_mainwindow.h" 

    MainWindow::MainWindow(QWidget *parent) : 
     QMainWindow(parent), 
     ui(new Ui::MainWindow) 
    { 
     ui->setupUi(this); 
     tcpSocket = new QTcpSocket(this); 
     tcpSocket->connectToHost("192.168.1.1",8080); 
     connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(tcpDataReceive())); 

    } 

    MainWindow::~MainWindow() 
    { 
     delete ui; 
    } 


    void MainWindow::tcpDataReceive() 
    { 
     QByteArray data = QByteArray::fromHex(tcpSocket->readAll()); 
     qDebug() << data; 
    } 

    void MainWindow::on_pushButton_clicked() 
    { 
     tcpSocket->write("GET /?action=stream\r\n\r\n"); 
    } 

我覺得MJPEG流不能觸發信號完成,因爲MJPEG數據八方通transfer.So,我選擇使用SIGNAL readyRead()。 :)