Create image callback using Qt signal

Former-commit-id: cf469ba01ffd26d286e6fb8d9f081cf126042e50
This commit is contained in:
johan
2014-02-19 21:52:37 +01:00
parent f0c35071da
commit 69d6e47328
9 changed files with 141 additions and 106 deletions

View File

@@ -14,16 +14,21 @@ include_directories(
${QT_INCLUDES}
)
set(Hyperion_V4L2_HEADERS
ProtoConnection.h
set(Hyperion_V4L2_QT_HEADERS
ImageHandler.h
ScreenshotHandler.h
)
set(Hyperion_V4L2_HEADERS
VideoStandardParameter.h
ProtoConnection.h
)
set(Hyperion_V4L2_SOURCES
hyperion-v4l2.cpp
ProtoConnection.cpp
ImageHandler.cpp
ScreenshotHandler.cpp
)
set(Hyperion_V4L2_PROTOS
@@ -34,9 +39,13 @@ protobuf_generate_cpp(Hyperion_V4L2_PROTO_SRCS Hyperion_V4L2_PROTO_HDRS
${Hyperion_V4L2_PROTOS}
)
QT4_WRAP_CPP(Hyperion_V4L2_MOC_SOURCES ${Hyperion_V4L2_QT_HEADERS})
add_executable(hyperion-v4l2
${Hyperion_V4L2_HEADERS}
${Hyperion_V4L2_SOURCES}
${Hyperion_V4L2_QT_HEADERS}
${Hyperion_V4L2_MOC_SOURCES}
${Hyperion_V4L2_PROTO_SRCS}
${Hyperion_V4L2_PROTO_HDRS}
)
@@ -48,5 +57,5 @@ target_link_libraries(hyperion-v4l2
hyperion-utils
${PROTOBUF_LIBRARIES}
pthread
${QT_LIBRARIES}
${QT_LIBRARIES}
)

View File

@@ -10,6 +10,10 @@ ImageHandler::ImageHandler(const std::string & address, int priority, double sig
_connection.setSkipReply(skipProtoReply);
}
ImageHandler::~ImageHandler()
{
}
void ImageHandler::receiveImage(const Image<ColorRgb> & image)
{
// check if we should do signal detection
@@ -32,10 +36,3 @@ void ImageHandler::receiveImage(const Image<ColorRgb> & image)
}
}
}
void ImageHandler::imageCallback(void *arg, const Image<ColorRgb> &image)
{
ImageHandler * handler = static_cast<ImageHandler *>(arg);
handler->receiveImage(image);
}

View File

@@ -1,24 +1,30 @@
// Qt includes
#include <QObject>
// hyperion includes
#include <utils/Image.h>
#include <utils/ColorRgb.h>
// blackborder includes
#include <blackborder/BlackBorderProcessor.h>
// hyperion-v4l includes
// hyperion v4l2 includes
#include "ProtoConnection.h"
/// This class handles callbacks from the V4L2 grabber
class ImageHandler
class ImageHandler : public QObject
{
Q_OBJECT
public:
ImageHandler(const std::string & address, int priority, double signalThreshold, bool skipProtoReply);
virtual ~ImageHandler();
public slots:
/// Handle a single image
/// @param image The image to process
void receiveImage(const Image<ColorRgb> & image);
/// static function used to direct callbacks to a ImageHandler object
/// @param arg This should be an ImageHandler instance
/// @param image The image to process
static void imageCallback(void * arg, const Image<ColorRgb> & image);
private:
/// Priority for calls to Hyperion
const int _priority;

View File

@@ -0,0 +1,25 @@
// Qt includes
#include <QImage>
#include <QCoreApplication>
// hyperion-v4l2 includes
#include "ScreenshotHandler.h"
ScreenshotHandler::ScreenshotHandler(const std::string & filename) :
_filename(filename)
{
}
ScreenshotHandler::~ScreenshotHandler()
{
}
void ScreenshotHandler::receiveImage(const Image<ColorRgb> & image)
{
// store as PNG
QImage pngImage((const uint8_t *) image.memptr(), image.width(), image.height(), 3*image.width(), QImage::Format_RGB888);
pngImage.save(_filename.c_str());
// Quit the application after the first image
QCoreApplication::quit();
}

View File

@@ -0,0 +1,24 @@
// Qt includes
#include <QObject>
// hyperionincludes
#include <utils/Image.h>
#include <utils/ColorRgb.h>
/// This class handles callbacks from the V4L2 grabber
class ScreenshotHandler : public QObject
{
Q_OBJECT
public:
ScreenshotHandler(const std::string & filename);
virtual ~ScreenshotHandler();
public slots:
/// Handle a single image
/// @param image The image to process
void receiveImage(const Image<ColorRgb> & image);
private:
const std::string _filename;
};

View File

@@ -4,7 +4,7 @@
#include <clocale>
// QT includes
#include <QImage>
#include <QCoreApplication>
// getoptPlusPLus includes
#include <getoptPlusPlus/getoptpp.h>
@@ -19,6 +19,7 @@
#include "ProtoConnection.h"
#include "VideoStandardParameter.h"
#include "ImageHandler.h"
#include "ScreenshotHandler.h"
using namespace vlofgren;
@@ -32,8 +33,11 @@ void saveScreenshot(void *, const Image<ColorRgb> & image)
int main(int argc, char** argv)
{
QCoreApplication app(argc, argv);
// force the locale
setlocale(LC_ALL, "C");
QLocale::setDefault(QLocale::c());
try
{
@@ -113,31 +117,30 @@ int main(int argc, char** argv)
// set 3D mode if applicable
if (arg3DSBS.isSet())
{
grabber.set3D(V4L2Grabber::MODE_3DSBS);
grabber.set3D(VIDEO_3DSBS);
}
else if (arg3DTAB.isSet())
{
grabber.set3D(V4L2Grabber::MODE_3DTAB);
grabber.set3D(VIDEO_3DTAB);
}
// start the grabber
grabber.start();
// run the grabber
if (argScreenshot.isSet())
{
grabber.setCallback(&saveScreenshot, nullptr);
grabber.capture(1);
ScreenshotHandler handler("screenshot.png");
QObject::connect(&grabber, SIGNAL(newFrame(Image<ColorRgb>)), &handler, SLOT(receiveImage(Image<ColorRgb>)));
grabber.start();
QCoreApplication::exec();
grabber.stop();
}
else
{
ImageHandler handler(argAddress.getValue(), argPriority.getValue(), argSignalThreshold.getValue(), argSkipReply.isSet());
grabber.setCallback(&ImageHandler::imageCallback, &handler);
grabber.capture();
QObject::connect(&grabber, SIGNAL(newFrame(Image<ColorRgb>)), &handler, SLOT(receiveImage(Image<ColorRgb>)));
grabber.start();
QCoreApplication::exec();
grabber.stop();
}
// stop the grabber
grabber.stop();
}
catch (const std::runtime_error & e)
{