Linux下基于OpenCV的摄像头数据采集与传输(2)

class Widget : public QWidget
{
    Q_OBJECT
   
public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
   
private:
    Ui::Widget *ui;
    QImage img;
    QUdpSocket* receiver;


private slots:
    void processPendingDatagram();

};

#endif // WIDGET_H

widget.c

#include "widget.h"
#include "ui_widget.h"
#include <QFile>
#include <QtNetwork>


#define PORTNUMBER 4444


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

receiver = new QUdpSocket(this);
    bool result = receiver->bind(PORTNUMBER, QUdpSocket::ShareAddress);
    if(result)
    {
        printf("bind right\n");
    }
    else
    {
        printf("Bind error");
    }
    connect(receiver, SIGNAL(readyRead()), this, SLOT(processPendingDatagram()));
}

void Widget::processPendingDatagram()
{
    qint64 num = receiver->pendingDatagramSize();
  //printf("receive size = %ld\n", num);

QByteArray buffer;
    buffer.resize(num);
    receiver->readDatagram((char*)buffer.data(), num);  //将接收到的数据放入buffer中

QFile file("test.jpg");
    if(!file.open(QIODevice::WriteOnly))
    {
        printf("file open error\n");
        return;
    }

file.write(buffer);  //将数据写到硬盘,保存成JPG格式图片
    file.flush();
    file.close();

img = QImage("test.jpg");
    ui->label->setPixmap(QPixmap::fromImage(img));  // 在label中显示图片
    ui->label->resize(ui->label->pixmap()->size());
}

Widget::~Widget()
{
    delete ui;
}

Linux下基于OpenCV的摄像头数据采集与传输

如果有客户端在Windows下的QT实现的还请大神指教。

推荐阅读

Ubuntu 12.04 安装 OpenCV2.4.2

CentOS下OpenCV无法读取视频文件

Ubuntu 12.04下安装OpenCV 2.4.5总结

Ubuntu 10.04中安装OpenCv2.1九步曲

基于QT和OpenCV的人脸识别系统

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/def10fe5391177695e7150cdf6e7a189.html