[ui] add progress bar, add timers for buffer preassure and displaying images

This commit is contained in:
Jan Grewe 2020-03-13 11:59:04 +01:00
parent 506bb98879
commit df27794dd5
3 changed files with 86 additions and 15 deletions

View File

@ -3,13 +3,14 @@
#include <pylon/PylonIncludes.h> #include <pylon/PylonIncludes.h>
void Grabber::run() { void Grabber::run() {
stop_request = false;
int count = 0; int count = 0;
if (camera->isOpen()) { if (camera->isOpen()) {
Pylon::CGrabResultPtr frame; Pylon::CGrabResultPtr frame;
Pylon::CInstantCamera *cam = camera->getCamera(); Pylon::CInstantCamera *cam = camera->getCamera();
cam->StartGrabbing(); cam->StartGrabbing();
while (!stop_request) { while (!stop_request) {
camera->frameRate(75); camera->frameRate(25);
MyImage img; MyImage img;
cam->RetrieveResult( 5000, frame, Pylon::TimeoutHandling_ThrowException); cam->RetrieveResult( 5000, frame, Pylon::TimeoutHandling_ThrowException);
img.setFrame(frame); img.setFrame(frame);

View File

@ -2,7 +2,6 @@
#include <QApplication> #include <QApplication>
#include <QClipboard> #include <QClipboard>
//nclude <QColorSpace>
#include <QDir> #include <QDir>
#include <QFileDialog> #include <QFileDialog>
#include <QImageReader> #include <QImageReader>
@ -20,6 +19,7 @@
#include <QToolBar> #include <QToolBar>
#include <iostream> #include <iostream>
#include <chrono> #include <chrono>
#include <cmath>
#if defined(QT_PRINTSUPPORT_LIB) #if defined(QT_PRINTSUPPORT_LIB)
# include <QtPrintSupport/qtprintsupportglobal.h> # include <QtPrintSupport/qtprintsupportglobal.h>
@ -45,8 +45,24 @@ PylonRecorder::PylonRecorder(QWidget *parent)
grabber = new Grabber(pylon, buffer); grabber = new Grabber(pylon, buffer);
createActions(); createActions();
updateActions(); updateActions();
resize(QGuiApplication::primaryScreen()->availableSize() * 3 / 5);
frameTimer = new QTimer(this);
connect(frameTimer, &QTimer::timeout, this, &PylonRecorder::displaySingleFrame);
preassureTimer = new QTimer(this);
connect(preassureTimer, &QTimer::timeout, this, &PylonRecorder::displayBufferPreassure);
preassureBar = new QProgressBar(this);
preassureBar->setRange(0, 100);
preassureBar->setTextVisible(true);
QColor color = progressColor(0);
QPalette progressPalette = preassureBar->palette();
progressPalette.setBrush(QPalette::Highlight, QBrush(color));
preassureBar->setPalette(progressPalette);
QLabel *preassureLabel = new QLabel("Buffer preassure:", this);
statusBar()->addWidget(preassureLabel);
statusBar()->addWidget(preassureBar);
resize(QGuiApplication::primaryScreen()->availableSize() * 3 / 5);
} }
PylonRecorder::~PylonRecorder(){ PylonRecorder::~PylonRecorder(){
@ -234,6 +250,10 @@ void PylonRecorder::about() {
void PylonRecorder::createActions() { void PylonRecorder::createActions() {
const QIcon connect_icon(":/images/connect.png"); const QIcon connect_icon(":/images/connect.png");
const QIcon disconnect_icon(":/images/disconnect.png"); const QIcon disconnect_icon(":/images/disconnect.png");
const QIcon snapshot_icon(":/images/snapshot.png");
const QIcon grab_icon(":/images/record.png");
const QIcon stop_icon(":/images/stop.png");
const QIcon exit_icon(":/images/exit.png");
QMenu *fileMenu = menuBar()->addMenu(tr("&File")); QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
@ -249,7 +269,7 @@ void PylonRecorder::createActions() {
fileMenu->addSeparator(); fileMenu->addSeparator();
QAction *exitAct = fileMenu->addAction(tr("E&xit"), this, &PylonRecorder::quitApplication); QAction *exitAct = fileMenu->addAction(exit_icon, tr("E&xit"), this, &PylonRecorder::quitApplication);
exitAct->setShortcut(tr("Ctrl+Q")); exitAct->setShortcut(tr("Ctrl+Q"));
QMenu *editMenu = menuBar()->addMenu(tr("&Edit")); QMenu *editMenu = menuBar()->addMenu(tr("&Edit"));
@ -283,23 +303,24 @@ void PylonRecorder::createActions() {
fitToWindowAct->setShortcut(tr("Ctrl+F")); fitToWindowAct->setShortcut(tr("Ctrl+F"));
QMenu *camera_menu = menuBar()->addMenu(tr("&Camera")); QMenu *camera_menu = menuBar()->addMenu(tr("&Camera"));
connect_camera_action = camera_menu->addAction(connect_icon, tr("&Connect"), this, &PylonRecorder::connectCamera); connect_camera_action = camera_menu->addAction(connect_icon, tr("&connect"), this, &PylonRecorder::connectCamera);
connect_camera_action->setStatusTip(tr("Connect to to camera and open device")); connect_camera_action->setStatusTip(tr("Connect to to camera and open device"));
disconnect_camera_action = camera_menu->addAction(disconnect_icon, tr("&Disconnect"), this, &PylonRecorder::disconnectCamera); disconnect_camera_action = camera_menu->addAction(disconnect_icon, tr("&disconnect"), this, &PylonRecorder::disconnectCamera);
disconnect_camera_action->setStatusTip(tr("Disconnect from the camera device")); disconnect_camera_action->setStatusTip(tr("Disconnect from the camera device"));
camera_menu->addSeparator(); camera_menu->addSeparator();
grab_still_action = camera_menu->addAction(tr("&Grab still"), this, &PylonRecorder::grabStillFromPylon); grab_still_action = camera_menu->addAction(snapshot_icon, tr("&grab still"), this, &PylonRecorder::grabStillFromPylon);
grab_still_action->setStatusTip(tr("Grab single image from Pylon camera")); grab_still_action->setStatusTip(tr("Grab single image from Pylon camera"));
grab_still_action->setShortcut(tr("Ctrl+ ")); grab_still_action->setShortcut(tr("Ctrl+ "));
grab_continuous_action = camera_menu->addAction(tr("&grab continuous"), this, &PylonRecorder::startRecording); grab_continuous_action = camera_menu->addAction(grab_icon, tr("&grab continuous"), this, &PylonRecorder::startRecording);
grab_continuous_action->setShortcut(tr("Ctrl+Enter")); grab_continuous_action->setShortcut(tr("Ctrl+Enter"));
grab_stop_action = camera_menu->addAction(tr("&stop grabbing"), this, &PylonRecorder::stopRecording); grab_stop_action = camera_menu->addAction(stop_icon, tr("&stop grabbing"), this, &PylonRecorder::stopRecording);
QMenu *helpMenu = menuBar()->addMenu(tr("&Help")); QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(tr("&About"), this, &PylonRecorder::about); helpMenu->addAction(tr("&About"), this, &PylonRecorder::about);
helpMenu->addAction(tr("About &Qt"), &QApplication::aboutQt); helpMenu->addAction(tr("About &Qt"), &QApplication::aboutQt);
QToolBar *toolbar = addToolBar("main toolbar"); QToolBar *toolbar = addToolBar("main toolbar");
toolbar->addAction(exitAct);
toolbar->addSeparator(); toolbar->addSeparator();
toolbar->addAction(connect_camera_action); toolbar->addAction(connect_camera_action);
toolbar->addAction(disconnect_camera_action); toolbar->addAction(disconnect_camera_action);
@ -318,8 +339,8 @@ void PylonRecorder::updateActions() {
disconnect_camera_action->setEnabled(pylon->isOpen()); disconnect_camera_action->setEnabled(pylon->isOpen());
connect_camera_action->setEnabled(!pylon->isOpen()); connect_camera_action->setEnabled(!pylon->isOpen());
grab_still_action->setEnabled(pylon->isOpen()); grab_still_action->setEnabled(pylon->isOpen());
grab_continuous_action->setEnabled(pylon->isOpen()); grab_continuous_action->setEnabled(pylon->isOpen() && !grabbing);
grab_stop_action->setEnabled(grabbing);
} }
void PylonRecorder::scaleImage(double factor) { void PylonRecorder::scaleImage(double factor) {
@ -336,9 +357,11 @@ void PylonRecorder::scaleImage(double factor) {
void PylonRecorder::quitApplication() { void PylonRecorder::quitApplication() {
if (pylon->isOpen()) { if (pylon->isOpen()) {
if (grabbing) {
stopRecording();
}
pylon->closeCamera(); pylon->closeCamera();
} }
//pylon->terminate();
this->close(); this->close();
} }
@ -352,10 +375,8 @@ void PylonRecorder::connectCamera() {
pylon->openCamera(message); pylon->openCamera(message);
statusBar()->showMessage(QString::fromStdString(message)); statusBar()->showMessage(QString::fromStdString(message));
updateActions(); updateActions();
std::cerr << pylon->maxFrameRate() << std::endl;
} }
void PylonRecorder::disconnectCamera() { void PylonRecorder::disconnectCamera() {
pylon->closeCamera(); pylon->closeCamera();
statusBar()->showMessage(tr("Camera closed!")); statusBar()->showMessage(tr("Camera closed!"));
@ -363,13 +384,53 @@ void PylonRecorder::disconnectCamera() {
} }
void PylonRecorder::startRecording() { void PylonRecorder::startRecording() {
int framecount = 500; buffer->clear();
grabber->start(); grabber->start();
grabbing = true;
updateActions();
preassureTimer->start(25);
frameTimer->start(100);
} }
void PylonRecorder::stopRecording() { void PylonRecorder::stopRecording() {
grabber->requestStop(); grabber->requestStop();
grabber->wait(10000); grabber->wait(10000);
grabbing = false;
frameTimer->stop();
preassureTimer->stop();
preassureBar->reset();
updateActions();
}
void PylonRecorder::displaySingleFrame() {
MyImage img;
bool valid = buffer->readLast(img);
if (valid) {
QImage qimg(static_cast<uchar *>(img.data()), img.width(), img.height(),
QImage::Format::Format_Grayscale8);
setImage(qimg);
} else {
std::cerr << "Error reading last image" << std::endl;
}
}
QColor PylonRecorder::progressColor(int value) {
int c, m, k = 0, y = 255;
c = 255 - 255 * value/100;
if (value < 50) {
m = 127 - 127 * value/50;
} else {
m = 255 * (value-50)/50;
}
return QColor::fromCmyk(c, m, y, k);
}
void PylonRecorder::displayBufferPreassure() {
int value = static_cast<int>(round(buffer->bufferPreassure()));
preassureBar->setValue(value);
QColor color = progressColor(value);
progressPalette.setBrush(QPalette::Highlight, QBrush(color));
preassureBar->setPalette(progressPalette);
} }
void PylonRecorder::grabStillFromPylon() { void PylonRecorder::grabStillFromPylon() {

View File

@ -2,6 +2,7 @@
#define IMAGEVIEWER_H #define IMAGEVIEWER_H
#include <QMainWindow> #include <QMainWindow>
#include <QTimer>
#include "pylonwrapper.h" #include "pylonwrapper.h"
#include "imagebuffer.h" #include "imagebuffer.h"
#include "grabber.h" #include "grabber.h"
@ -12,6 +13,7 @@
# if QT_CONFIG(printer) # if QT_CONFIG(printer)
# include <QPrinter> # include <QPrinter>
#include <QProgressBar>
# endif # endif
#endif #endif
@ -51,12 +53,15 @@ private slots:
void startRecording(); void startRecording();
void stopRecording(); void stopRecording();
void quitApplication(); void quitApplication();
void displaySingleFrame();
void displayBufferPreassure();
private: private:
void createActions(); void createActions();
void createMenus(); void createMenus();
void createToolBar(); void createToolBar();
void updateActions(); void updateActions();
QColor progressColor(int value);
bool saveFile(const QString &fileName); bool saveFile(const QString &fileName);
void setImage(const QImage &newImage); void setImage(const QImage &newImage);
@ -64,13 +69,17 @@ private:
void adjustScrollBar(QScrollBar *scrollBar, double factor); void adjustScrollBar(QScrollBar *scrollBar, double factor);
QImage image; QImage image;
QTimer *frameTimer;
QTimer *preassureTimer;
QLabel *imageLabel; QLabel *imageLabel;
QProgressBar *preassureBar;
QScrollArea *scrollArea; QScrollArea *scrollArea;
double scaleFactor = 1; double scaleFactor = 1;
PylonWrapper *pylon; PylonWrapper *pylon;
ImageBuffer *buffer; ImageBuffer *buffer;
Grabber *grabber; Grabber *grabber;
bool grabbing; bool grabbing;
QPalette progressPalette;
#if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printer) #if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printer)
QPrinter printer; QPrinter printer;