2015-10-11 74 views
3

我想創建一個Http服務器來發送一個MJPEG流。我已經能夠發送圖像但沒有實時流。如何使用QTcp-Server套接字創建HTTP MJPEG流式服務器?

我做了什麼:創建了一個TCP服務器。當客戶端連接TCP套接字創建。然後,我實現了一個ReadyRead SLOT,它在瀏覽器向服務器發送「GET」請求時執行。

GET/HTTP/1.1 
Host: 127.0.0.1:8889 
User-Agent: Mozilla/5.0... 

然後我運行下面的代碼

QString inbound = m_Client->readAll(); 

QByteArray header = "HTTP/1.1 200 OK\r\n"; 
m_Client->write(header); 

QByteArray ContentType = "Content-Type: image/jpeg\r\n\n"; 
m_Client->write(ContentType); 

Mat first_img; //OPENCV Material 
m_Stream->read(first_img); // Read Image from Webcam 
QImage img = Mat2QImage(first_img); // Convert to QImage 
QByteArray ba; // QByteArray as Buffer for JPG Envoded QImage 
QBuffer buffer(&ba); 
buffer.open(QIODevice::WriteOnly); 
img.save(&buffer, "JPG"); 
m_Client->write(ba); // Write The Encoded Image 

m_Client->close(); 

我想到了創建一個循環重複圖像的流部分 但這不起作用。瀏覽器只是繼續加載,沒有任何反應....

while(1){ 
    Mat first_img; //OPENCV Material 
    m_Stream->read(first_img); // Read Image from Webcam 

    QImage img = Mat2QImage(first_img); // Convert to QImage 
    QByteArray ba; // QByteArray as Buffer for JPG Envoded QImage 
    QBuffer buffer(&ba); 
    buffer.open(QIODevice::WriteOnly); 
    img.save(&buffer, "JPG"); 

    m_Client->write(ba); // Write The Encoded Image 

    QThread::usleep(500); 
} 

我在想什麼?編碼是錯誤的還是我處理請求的方式?也許是mime類型?


更新 我在
http://www.damonkohler.com/2010/10/mjpeg-streaming-protocol.htmlhttps://en.wikipedia.org/wiki/Motion_JPEG 一看,並試圖實現論文的方法,但沒有任何結果....

QString inbound = m_Client->readAll(); 

QByteArray ContentType = ("HTTP/1.0 200 OK\r\n" \ 
     "Server: YourServerName\r\n" \ 
     "Connection: close\r\n" \ 
     "Max-Age: 0\r\n" \ 
     "Expires: 0\r\n" \ 
     "Cache-Control: no-cache, private\r\n" \ 
     "Pragma: no-cache\r\n" \ 
     "Content-Type: multipart/x-mixed-replace; " \ 
     "boundary=--BoundaryString\r\n\r\n"); 
m_Client->write(ContentType); 



while(1){ 
    Mat first_img; //OPENCV Material 
    m_Stream->read(first_img); // Read Image from Webcam 

    QImage img = Mat2QImage(first_img); // Convert to QImage 
    QByteArray ba; // QByteArray as Buffer for JPG Envoded QImage 
    QBuffer buffer(&ba); 
    buffer.open(QIODevice::WriteOnly); 
    img.save(&buffer, "JPG"); 

    QByteArray BoundaryString = ("--BoundaryString\r\n" \ 
           "Content-type: image/jpg\r\n\r\n"); 

    m_Client->write(BoundaryString); 
    m_Client->write(ba); // Write The Encoded Image 

    QThread::usleep(500); 
} 

m_Client->close(); 

回答

2

我解決它自己。 ... 我只是不得不調整一些協議相關的東西....

m_TcpHttpClient->readAll(); // Discard "Get Request String" 

QByteArray ContentType = ("HTTP/1.0 200 OK\r\n" \ 
          "Server: en.code-bude.net example server\r\n" \ 
          "Cache-Control: no-cache\r\n" \ 
          "Cache-Control: private\r\n" \ 
          "Content-Type: multipart/x-mixed-replace;boundary=--boundary\r\n\r\n"); 

m_TcpHttpClient->write(ContentType); 


while(1){ 

    // Image to Byte Array via OPENCV Method 
    std::vector<uchar> buff; 
    imencode(".jpg",m_VisualEngine->GetActualFrame(),buff); 
    std::string content(buff.begin(), buff.end()); 
    QByteArray CurrentImg(QByteArray::fromStdString(content)); 


    QByteArray BoundaryString = ("--boundary\r\n" \ 
           "Content-Type: image/jpeg\r\n" \ 
           "Content-Length: "); 

    BoundaryString.append(QString::number(CurrentImg.length())); 
    BoundaryString.append("\r\n\r\n"); 

    m_TcpHttpClient->write(BoundaryString); 
    m_TcpHttpClient->write(CurrentImg); // Write The Encoded Image 

    m_TcpHttpClient->flush(); 
} 
+0

我試過上面的代碼,但是會引發錯誤。任何幫助? – FadedCoder

+0

請給我一些關於你收到的錯誤信息 –

+0

@MichaelRamph這裏是包含錯誤的問題 - http://stackoverflow.com/questions/37025383/ – FadedCoder