首页 > 代码库 > linux下视频传输测试
linux下视频传输测试
本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.
在上一篇《ubuntu下基于qt+OpenCV控制摄像头》的基础上测试了视频传输.
环境:主机:PC + Ubuntu10.04 + OpenCV + Qt
从机:s3c6410 + linux2.6.38 + Qt
主机有摄像头,捕捉摄像头,然后通过网络传输,从机接收数据后显示。
实现流程:
主机代码:
主要代码如下,socket编程采用Linux本身提供的方法。
widget.h
[html] view plain copy
- <span style="font-family:‘Arial Black‘;">#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
- </span>
widget.c
[html] view plain copy
- <span style="font-family:‘Arial Black‘;">#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");
- }
- </span>
从机主要代码如下,socket编程采用qt里机制
widget.h
[cpp] view plain copy
- <span style="font-family:‘Arial Black‘;">#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
- </span>
widget.c
[cpp] view plain copy
- <span style="font-family:‘Arial Black‘;">#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;
- }
- </span>
从机端显示:
http://blog.csdn.net/jdh99/article/details/6672481
linux下视频传输测试
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。