linux下视频传输测试

本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.

在上一篇《ubuntu下基于qt+opencv控制摄像头》的基础上测试了视频传输.

环境:主机:PC + Ubuntu10.04 + OpenCV + Qt

      从机:s3c6410 + linux2.6.38 + Qt

主机有摄像头,捕捉摄像头,然后通过网络传输,从机接收数据后显示。


实现流程:

主机代码:

主要代码如下,socket编程采用linux本身提供的方法。

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include "public.h"

namespace Ui {
    class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private:
    Ui::Widget *ui;

    CvCapture *capture;             //视频数据结构
    IplImage *frame;
    QTimer *timer;
    QImage *img;

private slots:
    void slot_timer();

protected:
    void paintEvent (QPaintEvent *);
};

#endif // WIDGET_H

widget.c

#include "widget.h"
#include "ui_widget.h"
#include "public.h"

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

    timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(slot_timer()));
    timer->start(FPS);

    capture = cvCreateCameraCapture(0);
    //cvNamedWindow("jdh",CV_WINDOW_AUTOSIZE);

    //初始化网络
    init_sockaddr_in(&Dst_Addr,Udp_Port,QString(DST_IP));
    //套接字初始化
    Socket_Send = socket(AF_INET,SOCK_DGRAM,0);

    //img = new QImage("test.jpg");
    //img->save("test_save.png","PNG",0);
    //img->save("test_save.jpeg","JPEG",10);
}

void Widget::slot_timer()
{
    frame = cvQueryFrame(capture);
    if (!frame)
    {
        return;
    }

    //img->load("test.jpg");
    cvCvtColor(frame,frame,CV_BGR2RGB);
    img = new QImage((unsigned char*)frame->imageData,frame->width,frame->height,frame->widthStep,QImage::Format_RGB888);
    //img = new QImage((unsigned char*)frame->imageData,frame->width,frame->height,QImage::Format_RGB888);
    update();
    //cvShowImage("jdh",frame);

    img->save("test_save.jpeg","JPEG",ZHILIANG);
    QFile file("test_save.jpeg");
    if (!file.open(QIODevice::ReadOnly))
             return;
    QByteArray buffer = file.readAll();

    if (sendto(Socket_Send,buffer.data(),buffer.size(),0,(struct sockaddr *)(&Dst_Addr),sizeof(struct sockaddr_in)) < 0)
    {
        cout << "send fail" << " " << buffer.size() << endl;
        perror("hello");
    }
    file.close();

    //网络发送
    /*
    if (sendto(Socket_Send,img->data_ptr(),img->byteCount(),0,(struct sockaddr *)(&Dst_Addr),sizeof(struct sockaddr_in)) < 0)
    {
        cout << "send fail" << " " << frame->width << " " << frame->height << " " << img->byteCount() << endl;
        perror("hello");
    }
    */
}

void Widget::paintEvent(QPaintEvent * event)
{
    //painter->drawImage(0,0,mm);
    QPainter *pp = new QPainter(this);
    pp->drawImage(0,0,*img);
}

Widget::~Widget()
{
    delete ui;

    cvReleaseImage(&frame);
    //cvDestroyWindow("jdh");
}

从机主要代码如下,socket编程采用qt里机制

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include "public.h"

namespace Ui {
    class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private:
    Ui::Widget *ui;

    QImage *img;

    QUdpSocket *receiver;
protected:
    void paintEvent(QPaintEvent *);

private slots:
    void processPendingDatagram();
};

#endif // WIDGET_H

widget.c

#include "widget.h"
#include "ui_widget.h"

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

    receiver = new QUdpSocket(this);
    receiver->bind(Udp_Port,QUdpSocket::ShareAddress);
    connect(receiver,SIGNAL(readyRead()),this,SLOT(processPendingDatagram()));
}

void Widget::paintEvent(QPaintEvent *)
{
    QPainter *pp = new QPainter(this);
    pp->drawImage(0,0,*img);
}

void Widget::processPendingDatagram()
{
    qint64 num = receiver->pendingDatagramSize();
    cout << "receive size = " << num << endl;

    QByteArray buffer;
    buffer.resize(num);
    receiver->readDatagram((char *)buffer.data(),num);

    QFile file("test_save.jpeg");
    if (!file.open(QIODevice::WriteOnly))
             return;
    file.write(buffer);
    file.close();
    img = new QImage("test_save.jpeg");
    update();

}

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

从机端显示:

linux下视频传输测试_第1张图片






你可能感兴趣的:(linux下视频传输测试)