ImagesTcpServer.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2011-2014, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the Universite de Sherbrooke nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 #include "find_object/Settings.h"
28 #include "find_object/QtOpenCV.h"
29 
30 #include "ImagesTcpServer.h"
31 
32 #include <QtNetwork/QNetworkInterface>
33 #include <QtNetwork/QTcpSocket>
34 #include <QtGui/QTransform>
35 
36 ImagesTcpServer::ImagesTcpServer(float hz, const QString & path, QObject * parent) :
37  QTcpSocket(parent)
38 {
39  // Set camera parameters
40  find_object::Settings::setCamera_4imageRate(hz);
41  find_object::Settings::setCamera_5mediaPath(path);
42 
43  connect(&camera_, SIGNAL(imageReceived(const cv::Mat &)), this, SLOT(publishImage(const cv::Mat &)));
44  connect(this, SIGNAL(connected()), this, SLOT(startCamera()));
45 }
46 
48 {
49  QHostAddress hostAddress;
50 
51  QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
52  // use the first non-localhost IPv4 address
53  for (int i = 0; i < ipAddressesList.size(); ++i)
54  {
55  if (ipAddressesList.at(i) != QHostAddress::LocalHost && ipAddressesList.at(i).toIPv4Address())
56  {
57  hostAddress = ipAddressesList.at(i).toString();
58  break;
59  }
60  }
61 
62  // if we did not find one, use IPv4 localhost
63  if (hostAddress.isNull())
64  {
65  hostAddress = QHostAddress(QHostAddress::LocalHost);
66  }
67 
68  return hostAddress;
69 }
70 
71 void ImagesTcpServer::publishImage(const cv::Mat & image)
72 {
73  if(image.empty())
74  {
75  printf("No more images...\n");
76  camera_.pause();
77  Q_EMIT connectionLost();
78  }
79  else
80  {
81  if(this->waitForConnected())
82  {
83  std::vector<unsigned char> buf;
84  cv::imencode(".png", image, buf);
85 
86  QByteArray block;
87  QDataStream out(&block, QIODevice::WriteOnly);
88  out.setVersion(QDataStream::Qt_4_0);
89  out << (quint64)0;
90  out.writeRawData((char*)buf.data(), (int)buf.size());
91  out.device()->seek(0);
92  out << (quint64)(block.size() - sizeof(quint64));
93  this->write(block);
94 
95  }
96  else
97  {
98  printf("Lost connection...\n");
99  camera_.pause();
100  Q_EMIT connectionLost();
101  }
102  }
103 }
104 
106 {
107  if(!camera_.isRunning())
108  {
109  printf("Start...\n");
110  camera_.start();
111  }
112 }
void connectionLost()
virtual bool start()
Definition: Camera.cpp:183
virtual bool isRunning()
Definition: Camera.h:50
static QHostAddress getHostAddress()
ImagesTcpServer(float hz=10.0f, const QString &path="", QObject *parent=0)
void publishImage(const cv::Mat &image)
find_object::Camera camera_


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Thu Jun 6 2019 19:22:26