Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
396 views
in Technique[技术] by (71.8m points)

qt - Received Image from socket and display in Qml

I want to received image through using TCP socket and display in QMl window.

currently :I am able to receive .png image from socket. and : able to display the image from local disk in qml using image provider. but while integrating the both things i am facing the issue. how to call the socket's function.


    tcpserver.h
    class MyTcpServer : public QObject
    {
        Q_OBJECT
    public:
    explicit MyTcpServer(QObject *parent = 0);
    signals:
    public slots:
        void newConnection();
        void on_readyRead();
     private:
        QTcpServer *server;
    };```
    ```
    tcpserver.cpp
    QByteArray array,array1;
    MyTcpServer::MyTcpServer(QObject *parent) :
        QObject(parent)
    {server = new QTcpServer(this);
        connect(server, SIGNAL(newConnection()),
                this, SLOT(newConnection()));
        connect(server,SIGNAL(readyRead()),this,SLOT(readyRead()));
         if(!server->listen(QHostAddress::Any, 9999))
        {
            qDebug() << "Server could not start";
        }
        else
        {
            qDebug() << "Server started!";
        }
    }
    
    void MyTcpServer::newConnection()
    {
        QTcpSocket *socket = server->nextPendingConnection();
        if(socket)
        {
        connect(socket ,SIGNAL(readyRead()),this,SLOT(on_readyRead()));
        socket->write("Hello client 
");
        socket->flush();
        socket->waitForBytesWritten(3000);
        }
    }
    void MyTcpServer::on_readyRead()
    {
        QTcpSocket * server = dynamic_cast<QTcpSocket*>(sender());
        qDebug()<<"reading..";
    
             if(server)
             {
              qDebug()<<"Array size " << array.size() ;
              array.append(server->readAll());
              if(array.size()<230400)
              {
                qDebug()<<"Array size " << array.size() ;
              }
    
              else {
                qDebug()<<"Array size else " << array.size() ;
                cv::Mat img,img1;
                cv::Size const frame_size(240,320);
                img = cv::Mat(240,320, CV_8UC3,array.data());
                qDebug()<<"Image type " <<img.type();
                qDebug()<<"Array size " << array.size() ;
                qDebug()<<"beforeImage size size " << img.dims;
                cv::imshow("image display",img);
                cv::waitKey(5000);
                QImage imdisplay((uchar*)img.data, img.cols, img.rows, img.step, QImage::Format_RGB888);
            }
        }
    }
    //socket->close()
    
    }
    
    ```

imageprovider.cpp

QImage QmlImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
    printf("In request function ..
");
    MyTcpServer *m = new MyTcpServer();
    m->newConnection();
    printf("after connectrion....");
    QImage image1= m->on_readyRead();
    printf("after request....");
    return image;

}
```
question from:https://stackoverflow.com/questions/65937095/received-image-from-socket-and-display-in-qml

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should move MyTcpServer *m = new MyTcpServer(); to ImageProvider class constructor and m to class members, also create a signal in your MyTcpServer class and connect it to your ImageProvider updateImage slot. P.S Maybe need to change updateImage from simple function to slot.

tcpserver.h

class MyTcpServer : public QObject
{
    Q_OBJECT
public:
explicit MyTcpServer(QObject *parent = 0);
signals:
    void newImageReceived(QImage);
.
.
.

tcpserver.cpp on_readyRead()

            cv::imshow("image display",img);
            cv::waitKey(5000);
            QImage imdisplay((uchar*)img.data, img.cols, img.rows, img.step, QImage::Format_RGB888);

            emit newImageReceived(imdisplay);

qmlimageprovider.cpp

QmlImageProvider::QmlImageProvider()
 : QQuickImageProvider(QQuickImageProvider::Image)
{
    m = new MyTcpServer();
    connect(m, &MyTcpServer::newImageReceived, this, &QmlImageProvider::updateImage);
    m->newConnection();
}

qmlimageprovider.h

class QmlImageProvider : public QQuickImageProvider
{
public:
    QmlImageProvider();
    QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize) override;
public slots:
    void updateImage(QImage new_image);

private:
    QImage image;
    MyTcpServer *m;
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...