• 如何写http mjpeg server


    目的

    是为了让unity ue 等三维引擎直接读取mjpeg图像进行纹理贴图
    使用qt,opencv等等,因为经常要进行图像处理

    opencv

    使用opencv 和QImage 来转换图像

    QImage Widget::Mat2QImage(cv::Mat const& src)  
    {  
         cv::Mat temp; // make the same cv::Mat  
         cvtColor(src, temp,CV_BGR2RGB);  
         QImage dest((const uchar *) temp.data, temp.cols, temp.rows, temp.step, QImage::Format_RGB888);
         //深拷贝 
         dest.bits(); // 
         return dest;  
    }  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    QT server

    以下的server只写出主要的代码,注意http协议的边界 \r\n\r\n

    QString inbound = m_Client->readAll();
    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",Frame,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();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    实际上,server在发送的时候,每发送一个 --boundary 并且要发送content-length的后续的实际长度,循环发送,这样,client也要按照我们的这个去解析,有关于客户端接收的代码在我的另外一篇文章里面
    客户端读取http mjpeg

  • 相关阅读:
    spring
    财务管理-报支单提交中和付款申请被驳回解决措施
    在 ESP 开发板上开发 UI 不再复杂
    agileBPM 广州宏天BPM功能对比
    mysql之GROUP_CONCAT
    【无标题】
    在 IDEA 里下个五子棋不过分吧?
    4、两个栈实现一个队列
    softmax函数
    Windows下SSH配置多账号
  • 原文地址:https://blog.csdn.net/qianbo042311/article/details/132817727