如何使用套接字编程在PHP网页上显示OpenCV mat图像?

时间:2014-04-09 10:46:01

标签: php c++ qt sockets opencv

您好我已经使用Qt编写了一个c ++程序,该程序从网络摄像头获取图像并通过QSocket将其发送到网页。

Mainwindow.cpp

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    MyTCPserver = new server(this);
    MyTCPserver->start_listen(SOCKET_PORT);

}

Server.cpp

#include "server.h"
server::server(QObject *parent) :
    QTcpServer(parent)
{
    connect( this,SIGNAL(newConnection()),this, SLOT(tcpReady()));
}
void server::ReadData(){
    QByteArray array = server_socket->read( server_socket->bytesAvailable() );
    qDebug()<<"I've read something and it is: "<<array<<"\n";

    static int counter = 0;
    cv::Mat TEST = cv::Mat(400,400,CV_8UC1,cv::Scalar::all(120));
    cv::putText(TEST,QString::number(counter++).toUtf8().constData(),cv::Point(TEST.cols*0.4,TEST.rows*0.4),CV_FONT_HERSHEY_COMPLEX,.7,cv::Scalar(255));
    cv::imshow("TEST",TEST);
    cv::waitKey(30);
    server_socket->write((const char *)TEST.data);
}

server::~server()
{
    server_socket->disconnectFromHost();
    server_socket->waitForDisconnected();
}

void server::tcpReady()
{
    qDebug()<<"Server Is Okay \n";
    server_socket = this->nextPendingConnection();
    connect( server_socket, SIGNAL(readyRead()),this, SLOT(ReadData()) );

}

bool server::start_listen(int port_no)
{

    if( !this->listen( QHostAddress::LocalHost, port_no ) )
    {
        QMessageBox::warning( (QWidget *)this->parent(), tr("Error!"), tr("Cannot listen to port %1").arg(port_no) );

    }
    else
    {
        return true;
    }
}

Client.php

<?php
    $host    = "127.0.0.1";
    $port    = 12345;
    $message = 'm ';
    $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
    $result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");    
    socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
    $buf = '';
    $bytes = socket_recv($socket, $buf, 2048, MSG_WAITALL);
    $imgData = base64_encode($buf);
        $img = "<img src= 'data:image/png;base64, $imgData' />";
    print($img);
    socket_close($socket);
?>

我可以在我的c ++程序和php网页之间发送和接收字符串。

但是当我想将上述代码的图像发送到我的php网页时,网页上没有显示任何内容,网页显示损坏的图片图标。

我认为我的php代码错了,因为它可能无法获得open cv发送的内容!!!

知道如何解决这个问题吗?

编辑:

根据评论中的建议,我将server :: ReadData()更改为以下内容,但它也不起作用!!

void server::ReadData(){
    QByteArray array = server_socket->read( server_socket->bytesAvailable() );
    qDebug()<<"I've read something and it is: "<<array<<"\n";

    static int counter = 0;
    cv::Mat TEST = cv::Mat(400,400,CV_8UC1,cv::Scalar::all(120));
    cv::putText(TEST,QString::number(counter++).toUtf8().constData(),cv::Point(TEST.cols*0.4,TEST.rows*0.4),CV_FONT_HERSHEY_COMPLEX,.7,cv::Scalar(255));
    cv::imshow("TEST",TEST);
    cv::waitKey(30);
    std::vector<uchar> BUF;
    cv::imencode(".png",TEST,BUF);
    int Size = TEST.total()*8;
    server_socket->write((const char *)BUF[0],Size);
}

0 个答案:

没有答案