Skip to content

Commit

Permalink
Добавил тестовые приложения
Browse files Browse the repository at this point in the history
  • Loading branch information
MimusTriurus committed Dec 2, 2017
1 parent 7ee31c1 commit 9243fdb
Show file tree
Hide file tree
Showing 15 changed files with 192 additions and 32 deletions.
42 changes: 40 additions & 2 deletions lib/static/ImageSerialization/ImageSerialization.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "ImageSerialization.h"

#include <QImageWriter>
#include <QImageReader>
#include <QDataStream>

#define IMG_EXT "JPG"

QByteArray ImageSerialization::serialize( QImage img ) {
QByteArray ImageSerialization::serializeImg( QImage img ) {
QBuffer buffer;
QImageWriter writer( &buffer, IMG_EXT );
writer.write( img );
Expand All @@ -13,9 +15,45 @@ QByteArray ImageSerialization::serialize( QImage img ) {
return data;
}

QImage ImageSerialization::deserialize( QByteArray imgData ) {
QImage ImageSerialization::deserializeImg( QByteArray imgData ) {
QBuffer buffer( &imgData );
QImageReader reader( &buffer, IMG_EXT );
QImage img( reader.read( ) );
return img;
}

QByteArray ImageSerialization::serializeMat( cv::Mat mat ) {
if ( !mat.data ) {
qDebug( ) << "Could not open or find the image";
return nullptr;
}
cv::cvtColor( mat, mat, CV_BGR2BGRA );
QByteArray outBytes;
QDataStream outStream{ &outBytes, QIODevice::WriteOnly };
outStream << mat.rows;
outStream << mat.cols;
outStream << mat.type( );
QByteArray imgBytes{ ( char* )( mat.data ) };
outStream << imgBytes;
//qDebug( ) << "receive" << mat.rows << mat.cols << imgBytes.count( );
return outBytes;
}

cv::Mat ImageSerialization::deserializeMat( QByteArray matData ) {
int rows{ 0 };
int cols{ 0 };
int type{ CV_8UC4 };
QByteArray imgBytes;
QByteArray inBytes{ matData };
QDataStream inStream{ &inBytes, QIODevice::ReadOnly };
inStream >> rows;
inStream >> cols;
inStream >> type;
inStream >> imgBytes;
cv::Mat img = cv::Mat{ rows, cols, type, imgBytes.data( ) };
if ( imgBytes.count( ) >= ( imgBytes.count( ) * 4096 ) )
cv::imshow( "Display window", img );
else
qDebug( ) << "receive" << rows << cols << type << imgBytes.count( );
return img;
}
8 changes: 6 additions & 2 deletions lib/static/ImageSerialization/ImageSerialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@
#define IMAGESERIALIZATION_H

#include <QtCore>
#include "opencv2/opencv.hpp"
/**
* @brief Сериализация и десериализация QImage
*/
class ImageSerialization
{
public:
ImageSerialization( ) = delete;
static QByteArray serialize( QImage img );
static QImage deserialize( QByteArray imgData );
static QByteArray serializeImg( QImage img );
static QImage deserializeImg( QByteArray imgData );

static QByteArray serializeMat( cv::Mat mat );
static cv::Mat deserializeMat( QByteArray matData );
};

#endif // IMAGESERIALIZATION_H
13 changes: 13 additions & 0 deletions lib/static/ImageSerialization/ImageSerialization.pro
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,16 @@ unix {
target.path = /usr/lib
INSTALLS += target
}

unix {
CONFIG += link_pkgconfig
PKGCONFIG += opencv
}

win32 { LIBS += -L$$PWD/../../dependencies/lib/ -lopencv_core2413 \
-lopencv_highgui2413 \
-lopencv_imgproc2413

INCLUDEPATH += $$PWD/../../../dependencies/include
DEPENDPATH += $$PWD/../../../dependencies/include
}
10 changes: 5 additions & 5 deletions lib/static/VideoCapture/Capture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ Capture::Capture( QObject *parent ) :
bool Capture::open( int deviceId ) {
_cap.open( deviceId );
if( _cap.isOpened( ) == false ) {
qDebug() << "Camera not successfully connected.";
qDebug( ) << "Camera not successfully connected.";
return false;
}
return true;
}

bool Capture::open( const QString &source ) {
_cap.open( source.toUtf8( ).data( ) );
if( _cap.isOpened( ) == false ) {
qDebug() << "Source " << source << "not available";
if ( _cap.isOpened( ) == false ) {
qDebug( ) << "Source " << source << "not available";
return false;
}
return true;
Expand All @@ -30,8 +30,8 @@ bool Capture::open( const QString &source ) {
bool Capture::read( ) {
cv::Mat cvFrame;
_cap.read( cvFrame );
if( cvFrame.empty( ) == true ) {
qDebug("Frame is empty!");
if ( cvFrame.empty( ) == true ) {
qDebug( "Frame is empty!" );
return false;
}
//cv::resize( cvFrame, cvFrame, cv::Size( ), 0.5, 0.5 );
Expand Down
49 changes: 45 additions & 4 deletions lib/static/VideoReciever/VideoReciever.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,51 @@

VideoReciever::VideoReciever(quint16 port, QObject *parent ) : QObject( parent ) {
_server.bind( QHostAddress::Any, port );
connect( &_server, &QUdpSocket::readyRead, this, &VideoReciever::onRecieveData );
// connect( &_server, &QUdpSocket::readyRead, this, &VideoReciever::onReceiveData );
connect( &_server, &QUdpSocket::readyRead, this, &VideoReciever::onReceiveMatData );
}

void VideoReciever::onRecieveData( ) {
while (_server.hasPendingDatagrams( ) ) {
void VideoReciever::onReceiveMatData( ) {
/*
while ( _server.hasPendingDatagrams( ) ) {
QByteArray datagram;
datagram.resize( _server.pendingDatagramSize( ) );
QHostAddress *address = new QHostAddress( );
_server.readDatagram( datagram.data( ), datagram.size( ), address );
emit matReceived( ImageSerialization::deserializeMat( datagram ) );
}
*/
while ( _server.hasPendingDatagrams( ) ) {
QByteArray datagram;
datagram.resize( _server.pendingDatagramSize( ) );
QHostAddress *address = new QHostAddress( );
_server.readDatagram( datagram.data( ), datagram.size( ), address );
int msgSize{ datagram.size( ) };
if ( msgSize == sizeof( int ) ) {
QDataStream stream( datagram );
stream >> _packetCount;
//qDebug( ) << "count:" << _packetCount;
if ( _imgBytes.count( ) != 0 ) {
emit matReceived( ImageSerialization::deserializeMat( _imgBytes ) );
}
_imgBytes.clear( );
return;
}
_imgBytes.append( datagram );
}
}

void VideoReciever::onReceiveData( ) {
/*
while ( _server.hasPendingDatagrams( ) ) {
QByteArray datagram;
datagram.resize( _server.pendingDatagramSize( ) );
QHostAddress *address = new QHostAddress( );
_server.readDatagram( datagram.data( ), datagram.size( ), address );
emit imageReceived( ImageSerialization::deserializeImg( datagram ) );
}
*/
while ( _server.hasPendingDatagrams( ) ) {
QByteArray datagram;
datagram.resize( _server.pendingDatagramSize( ) );
QHostAddress *address = new QHostAddress( );
Expand All @@ -18,8 +58,9 @@ void VideoReciever::onRecieveData( ) {
if ( msgSize == sizeof( int ) ) {
QDataStream stream( datagram );
stream >> _packetCount;
qDebug( ) << "count:" << _packetCount;
if ( _imgBytes.count( ) != 0 ) {
emit imageReceived( ImageSerialization::deserialize( _imgBytes ) );
emit matReceived( ImageSerialization::deserializeMat( _imgBytes ) );
}
_imgBytes.clear( );
return;
Expand Down
5 changes: 4 additions & 1 deletion lib/static/VideoReciever/VideoReciever.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <QtCore>
#include <QUdpSocket>
#include <QImage>
#include "opencv2/opencv.hpp"

/**
* @brief Приемник видеопотока
Expand All @@ -15,13 +16,15 @@ class VideoReciever : public QObject
explicit VideoReciever( quint16 port = 10000, QObject *parent = nullptr );
signals:
void imageReceived( QImage img );
void matReceived( cv::Mat mat );
private:
QUdpSocket _server;
int _packetCount;
QByteArray _imgBytes;
QBuffer _buffer;
private slots:
void onRecieveData( );
void onReceiveData( );
void onReceiveMatData( );
};

#endif // VIDEORECIEVER_H
13 changes: 13 additions & 0 deletions lib/static/VideoReciever/VideoReciever.pro
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ unix {
INSTALLS += target
}

unix {
CONFIG += link_pkgconfig
PKGCONFIG += opencv
}

win32 { LIBS += -L$$PWD/../../dependencies/lib/ -lopencv_core2413 \
-lopencv_highgui2413 \
-lopencv_imgproc2413

INCLUDEPATH += $$PWD/../../../dependencies/include
DEPENDPATH += $$PWD/../../../dependencies/include
}

win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../Converter/release/ -lConverter
else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../Converter/debug/ -lConverter
else:unix: LIBS += -L$$OUT_PWD/../Converter/ -lConverter
Expand Down
19 changes: 11 additions & 8 deletions lib/static/VideoTransmitter/VideoTransmitter.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#include "VideoTransmitter.h"
#include <ImageSerialization.h>

VideoTransmitter::VideoTransmitter( QString host, quint16 port, QObject *parent ) :
HOST{ host },
PORT{ port },
PACKET_SIZE{ 4096 },
QObject( parent )
QObject{ parent }
{
connect( &_socket, SIGNAL( error( QAbstractSocket::SocketError ) ),
this, SLOT( onError( QAbstractSocket::SocketError ) ) );

//_socket.connectToHost( HOST, PORT, QIODevice::OpenModeFlag::WriteOnly );
}

void VideoTransmitter::sendNewFrame( QByteArray imgData ) {
//_socket.writeDatagram( imgData, HOST, PORT );
int totalPack = 1 + ( imgData.size( ) - 1 ) / PACKET_SIZE;
sendPacketsCount( totalPack );
while ( !imgData.isEmpty( ) ) {
Expand All @@ -21,14 +21,17 @@ void VideoTransmitter::sendNewFrame( QByteArray imgData ) {
}
}

void VideoTransmitter::sendNewFrame( cv::Mat mat ) {
sendNewFrame( ImageSerialization::serializeMat( mat ) );
}

void VideoTransmitter::onError( QAbstractSocket::SocketError errorMessage ) {
qDebug( ) << "VideoTransmitter error:" << errorMessage;
}

void VideoTransmitter::sendPacketsCount( int count ) {
QByteArray array;
QDataStream streamIn( &array, QIODevice::WriteOnly );
streamIn << count;
//qDebug( ) << "send packet count:" << count << " raw:" << array;
_socket.writeDatagram( array, HOST, PORT );
}

void VideoTransmitter::onError( QAbstractSocket::SocketError errorMessage ) {
qDebug( ) << "VideoTransmitter error:" << errorMessage;
}
6 changes: 2 additions & 4 deletions lib/static/VideoTransmitter/VideoTransmitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
#include <Converter.h>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

/**
* @brief Передатчик видео стрима
*/
Expand All @@ -19,12 +16,13 @@ class VideoTransmitter : public QObject
VideoTransmitter( QString host = "localhost", quint16 port = 10000, QObject *parent = nullptr );
public slots:
void sendNewFrame( QByteArray imgData );
void sendNewFrame( cv::Mat mat );
private:
QUdpSocket _socket;
void sendPacketsCount( int count );
const int PACKET_SIZE;
const QHostAddress HOST;
const quint16 PORT;
void sendPacketsCount( int count );
private slots:
void onError( QAbstractSocket::SocketError errorMessage );
};
Expand Down
13 changes: 13 additions & 0 deletions lib/static/VideoTransmitter/VideoTransmitter.pro
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,16 @@ else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../Conv
else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../Converter/release/Converter.lib
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../Converter/debug/Converter.lib
else:unix:!macx: PRE_TARGETDEPS += $$OUT_PWD/../Converter/libConverter.a

win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../ImageSerialization/release/ -lImageSerialization
else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../ImageSerialization/debug/ -lImageSerialization
else:unix:!macx: LIBS += -L$$OUT_PWD/../ImageSerialization/ -lImageSerialization

INCLUDEPATH += $$PWD/../ImageSerialization
DEPENDPATH += $$PWD/../ImageSerialization

win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../ImageSerialization/release/libImageSerialization.a
else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../ImageSerialization/debug/libImageSerialization.a
else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../ImageSerialization/release/ImageSerialization.lib
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../ImageSerialization/debug/ImageSerialization.lib
else:unix:!macx: PRE_TARGETDEPS += $$OUT_PWD/../ImageSerialization/libImageSerialization.a
12 changes: 11 additions & 1 deletion test/ReceiverTest/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ MainWindow::MainWindow(QWidget *parent) :
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect( &_frameReciever, &VideoReciever::imageReceived, this, &MainWindow::onReceiveImg );
//connect( &_frameReciever, &VideoReciever::imageReceived, this, &MainWindow::onReceiveImg );
connect( &_frameReceiver, SIGNAL( matReceived( cv::Mat ) ), this, SLOT( onReceiveImg( cv::Mat ) ) );
}

MainWindow::~MainWindow( ) {
Expand All @@ -24,3 +25,12 @@ void MainWindow::onReceiveImg( QImage frame ) {
_background = frame;
this->repaint( );
}

void MainWindow::onReceiveImg( cv::Mat img ) {
//qDebug( ) << "receive img";
if ( img.data ) {
qDebug( ) << img.rows << img.cols;
//cv::namedWindow( "Display window", cv::WINDOW_AUTOSIZE );
//cv::imshow( "Display window", img );
}
}
4 changes: 3 additions & 1 deletion test/ReceiverTest/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <QImage>
#include <QPainter>
#include <VideoReciever.h>
#include "opencv2/opencv.hpp"

namespace Ui {
class MainWindow;
Expand All @@ -22,9 +23,10 @@ class MainWindow : public QMainWindow
private:
QImage _background;
Ui::MainWindow *ui;
VideoReciever _frameReciever;
VideoReciever _frameReceiver;
private slots:
void onReceiveImg( QImage img );
void onReceiveImg( cv::Mat img );
};

#endif // MAINWINDOW_H
13 changes: 13 additions & 0 deletions test/ReceiverTest/ReceiverTest.pro
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,16 @@ else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../l
else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../lib/static/ImageSerialization/release/ImageSerialization.lib
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../lib/static/ImageSerialization/debug/ImageSerialization.lib
else:unix:!macx: PRE_TARGETDEPS += $$OUT_PWD/../../lib/static/ImageSerialization/libImageSerialization.a

unix {
CONFIG += link_pkgconfig
PKGCONFIG += opencv
}

win32 { LIBS += -L$$PWD/../../dependencies/lib/ -lopencv_core2413 \
-lopencv_imgproc2413 \
-lopencv_highgui2413

INCLUDEPATH += $$PWD/../../dependencies/include
DEPENDPATH += $$PWD/../../dependencies/include
}
Loading

0 comments on commit 9243fdb

Please sign in to comment.