Using QT's TCP/UDP to transfer image data on ARM

Generally communicate on the same LAN segment.

  1. Using qt's tcp to implement image transmission on arm

Generally, the arm device is used as a client, and only a tcp client needs to be implemented. Then take the image data, encode it according to jpeg, and transmit it through streaming.

establish connection:

const QString IP = "192.168.3.99";//目标地址
const int PORT = 1000;
socket = new QTcpSocket();
//连接信号槽
QObject::connect(socket, &QTcpSocket::readyRead, this, &camera::socket_Read_Data);
//取消已有的连接
socket->abort();
//连接服务器
socket->connectToHost(IP, PORT);//填写服务器的地址

You can monitor the connection status and reconnect if disconnected:

if(socket->state()!= QAbstractSocket::ConnectedState)
{
    socket->connectToHost(IP, PORT);//填写服务器的地址
}

send data:

socket->write(QByteArray);//这里可以填QByteArray或者char[]都行
socket->flush();

Send image data:

if(photo == 1)
{
    QImage *mage = new QImage(rgb,640,480,QImage::Format_RGB888);//填入rgb数据即可
    QByteArray ba;
    QBuffer buf(&ba); //把ba绑定到buf上,操作buf就等于操作ba
    mage->save(&buf,"jpg",60);//压缩质量60
    //先写大小过去,告诉主机我们要传输的数据有多大
    socket->write(QString(size=%1").arg(ba.size()).toLocal8Bit().data());
    socket->waitForReadyRead();//等待主机响应“ok”,可有可无
    socket->write(ba);//把图像数据写入传输给主机,这里使用的流传输,由TCP自动控制速率
    socket->flush();
}

The signal slot has been established previously, and it will be automatically read when there is data. When photo\r\n is received, the photo flag is set to 1 and the picture data is sent:

void socket_Read_Data()
{
    QByteArray buffer;
    //读取缓冲区数据
    buffer = socket->readAll();
    if(!buffer.isEmpty())
    {
        QString str;
        str+=tr(buffer);
        qDebug() << "Receive: " << tr(buffer);
        if(tr(buffer) == tr("photo\r\n"))
        {
            photo = 1;
        }
    }
}

  1. Using qt's udp to implement image transmission on arm

UDP does not distinguish between the client and the server. You can directly create two sockets for sending and receiving. It is also possible to share one. Personally, I prefer to write them separately to make them clearer.

const QString IP = "192.168.3.99";//目标地址
//读写端口也可以一致
const int readport = 1001;
const int writeport = 1002;

//读,绑定读取本地端口
readsocket = new QUdpSocket();
readsocket->bind(readport);
//写
writesocket = new QUdpSocket();

//连接信号槽
QObject::connect(readsocket, &QUdpSocket::readyRead, this, &camera::socket_Read_Data);

send data:

char st[10] = "hello";
writesocket->writeDatagram(st, strlen[st], QHostAddress(IP), writeport);

To send image data, of course, the data can also be transmitted in packets:

if(photo == 1)
{
    QImage *mage = new QImage(rgb,640,480,QImage::Format_RGB888);
    QByteArray ba;
    QBuffer buf(&ba); //把ba绑定到buf上,操作buf就等于操作ba
    mage->save(&buf,"jpg",60);
    writesocket->writeDatagram(ba.data(), ba.size(), QHostAddress(IP), writeport);
    writesocket->flush();
}

Receive data:

void camera::socket_Read_Data()
{
    if(readsocket->hasPendingDatagrams()) 
    {
        QByteArray datagram;
        datagram.resize(readsocket->pendingDatagramSize());
        QHostAddress sender;
        quint16 senderPort;

        readsocket->readDatagram(datagram.data(), datagram.size(),&sender, &senderPort);
        qDebug() << "sender: " << sender.toString();//数据发送方IP
        qDebug() << "senderPort: " << senderPort;//数据发送方PORT
        qDebug() << "datagram: " << tr(datagram);//接收数据

        if(tr(datagram) == tr("photo\r\n"))
        {
            photo = 1;
        }        
    }
}

Guess you like

Origin blog.csdn.net/huntenganwei/article/details/128655991