renaming
This commit is contained in:
parent
ee912d70ab
commit
ac9607a915
373
imageviewer.cpp
373
imageviewer.cpp
@ -1,373 +0,0 @@
|
|||||||
#include "imageviewer.h"
|
|
||||||
|
|
||||||
#include <QApplication>
|
|
||||||
#include <QClipboard>
|
|
||||||
//nclude <QColorSpace>
|
|
||||||
#include <QDir>
|
|
||||||
#include <QFileDialog>
|
|
||||||
#include <QImageReader>
|
|
||||||
#include <QImageWriter>
|
|
||||||
#include <QLabel>
|
|
||||||
#include <QMenuBar>
|
|
||||||
#include <QMessageBox>
|
|
||||||
#include <QMimeData>
|
|
||||||
#include <QPainter>
|
|
||||||
#include <QScreen>
|
|
||||||
#include <QScrollArea>
|
|
||||||
#include <QScrollBar>
|
|
||||||
#include <QStandardPaths>
|
|
||||||
#include <QStatusBar>
|
|
||||||
#include <QToolBar>
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
#if defined(QT_PRINTSUPPORT_LIB)
|
|
||||||
# include <QtPrintSupport/qtprintsupportglobal.h>
|
|
||||||
|
|
||||||
# if QT_CONFIG(printdialog)
|
|
||||||
# include <QPrintDialog>
|
|
||||||
# endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
ImageViewer::ImageViewer(QWidget *parent)
|
|
||||||
: QMainWindow(parent), imageLabel(new QLabel), scrollArea(new QScrollArea)
|
|
||||||
{
|
|
||||||
imageLabel->setBackgroundRole(QPalette::Base);
|
|
||||||
imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
|
|
||||||
imageLabel->setScaledContents(true);
|
|
||||||
|
|
||||||
scrollArea->setBackgroundRole(QPalette::Dark);
|
|
||||||
scrollArea->setWidget(imageLabel);
|
|
||||||
scrollArea->setVisible(false);
|
|
||||||
setCentralWidget(scrollArea);
|
|
||||||
pylon = new PylonRecorder();
|
|
||||||
createActions();
|
|
||||||
updateActions();
|
|
||||||
resize(QGuiApplication::primaryScreen()->availableSize() * 3 / 5);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
ImageViewer::~ImageViewer(){
|
|
||||||
delete pylon;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ImageViewer::loadFile(const QString &fileName) {
|
|
||||||
QImageReader reader(fileName);
|
|
||||||
reader.setAutoTransform(true);
|
|
||||||
const QImage newImage = reader.read();
|
|
||||||
if (newImage.isNull()) {
|
|
||||||
QMessageBox::information(this, QGuiApplication::applicationDisplayName(),
|
|
||||||
tr("Cannot load %1: %2")
|
|
||||||
.arg(QDir::toNativeSeparators(fileName), reader.errorString()));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
setImage(newImage);
|
|
||||||
|
|
||||||
setWindowFilePath(fileName);
|
|
||||||
|
|
||||||
const QString message = tr("Opened \"%1\", %2x%3, Depth: %4")
|
|
||||||
.arg(QDir::toNativeSeparators(fileName)).arg(image.width()).arg(image.height()).arg(image.depth());
|
|
||||||
statusBar()->showMessage(message);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ImageViewer::setImage(const QImage &newImage) {
|
|
||||||
image = newImage;
|
|
||||||
// (image.colorSpace().isValid())
|
|
||||||
// image.convertToColorSpace(QColorSpace::SRgb);
|
|
||||||
imageLabel->setPixmap(QPixmap::fromImage(image));
|
|
||||||
scaleFactor = 1.0;
|
|
||||||
|
|
||||||
scrollArea->setVisible(true);
|
|
||||||
printAct->setEnabled(true);
|
|
||||||
fitToWindowAct->setEnabled(true);
|
|
||||||
updateActions();
|
|
||||||
|
|
||||||
if (!fitToWindowAct->isChecked())
|
|
||||||
imageLabel->adjustSize();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ImageViewer::saveFile(const QString &fileName) {
|
|
||||||
QImageWriter writer(fileName);
|
|
||||||
|
|
||||||
if (!writer.write(image)) {
|
|
||||||
QMessageBox::information(this, QGuiApplication::applicationDisplayName(),
|
|
||||||
tr("Cannot write %1: %2")
|
|
||||||
.arg(QDir::toNativeSeparators(fileName)), writer.errorString());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
const QString message = tr("Wrote \"%1\"").arg(QDir::toNativeSeparators(fileName));
|
|
||||||
statusBar()->showMessage(message);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void initializeImageFileDialog(QFileDialog &dialog, QFileDialog::AcceptMode acceptMode) {
|
|
||||||
static bool firstDialog = true;
|
|
||||||
|
|
||||||
if (firstDialog) {
|
|
||||||
firstDialog = false;
|
|
||||||
const QStringList picturesLocations = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation);
|
|
||||||
dialog.setDirectory(picturesLocations.isEmpty() ? QDir::currentPath() : picturesLocations.last());
|
|
||||||
}
|
|
||||||
|
|
||||||
QStringList mimeTypeFilters;
|
|
||||||
const QByteArrayList supportedMimeTypes = acceptMode == QFileDialog::AcceptOpen
|
|
||||||
? QImageReader::supportedMimeTypes() : QImageWriter::supportedMimeTypes();
|
|
||||||
for (const QByteArray &mimeTypeName : supportedMimeTypes)
|
|
||||||
mimeTypeFilters.append(mimeTypeName);
|
|
||||||
mimeTypeFilters.sort();
|
|
||||||
dialog.setMimeTypeFilters(mimeTypeFilters);
|
|
||||||
dialog.selectMimeTypeFilter("image/jpeg");
|
|
||||||
if (acceptMode == QFileDialog::AcceptSave)
|
|
||||||
dialog.setDefaultSuffix("jpg");
|
|
||||||
}
|
|
||||||
|
|
||||||
void ImageViewer::open() {
|
|
||||||
QFileDialog dialog(this, tr("Open File"));
|
|
||||||
initializeImageFileDialog(dialog, QFileDialog::AcceptOpen);
|
|
||||||
|
|
||||||
while (dialog.exec() == QDialog::Accepted && !loadFile(dialog.selectedFiles().first())) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ImageViewer::saveAs() {
|
|
||||||
QFileDialog dialog(this, tr("Save File As"));
|
|
||||||
initializeImageFileDialog(dialog, QFileDialog::AcceptSave);
|
|
||||||
|
|
||||||
while (dialog.exec() == QDialog::Accepted && !saveFile(dialog.selectedFiles().first())) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ImageViewer::print() {
|
|
||||||
Q_ASSERT(imageLabel->pixmap());
|
|
||||||
#if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printdialog)
|
|
||||||
|
|
||||||
QPrintDialog dialog(&printer, this);
|
|
||||||
|
|
||||||
if (dialog.exec()) {
|
|
||||||
QPainter painter(&printer);
|
|
||||||
QRect rect = painter.viewport();
|
|
||||||
QSize size = imageLabel->pixmap()->size();
|
|
||||||
size.scale(rect.size(), Qt::KeepAspectRatio);
|
|
||||||
painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
|
|
||||||
painter.setWindow(imageLabel->pixmap()->rect());
|
|
||||||
painter.drawPixmap(0, 0, *imageLabel->pixmap());
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
void ImageViewer::copy() {
|
|
||||||
#ifndef QT_NO_CLIPBOARD
|
|
||||||
QGuiApplication::clipboard()->setImage(image);
|
|
||||||
#endif // !QT_NO_CLIPBOARD
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifndef QT_NO_CLIPBOARD
|
|
||||||
static QImage clipboardImage() {
|
|
||||||
if (const QMimeData *mimeData = QGuiApplication::clipboard()->mimeData()) {
|
|
||||||
if (mimeData->hasImage()) {
|
|
||||||
const QImage image = qvariant_cast<QImage>(mimeData->imageData());
|
|
||||||
if (!image.isNull())
|
|
||||||
return image;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return QImage();
|
|
||||||
}
|
|
||||||
#endif // !QT_NO_CLIPBOARD
|
|
||||||
|
|
||||||
void ImageViewer::paste() {
|
|
||||||
#ifndef QT_NO_CLIPBOARD
|
|
||||||
const QImage newImage = clipboardImage();
|
|
||||||
if (newImage.isNull()) {
|
|
||||||
statusBar()->showMessage(tr("No image in clipboard"));
|
|
||||||
} else {
|
|
||||||
setImage(newImage);
|
|
||||||
setWindowFilePath(QString());
|
|
||||||
const QString message = tr("Obtained image from clipboard, %1x%2, Depth: %3")
|
|
||||||
.arg(newImage.width()).arg(newImage.height()).arg(newImage.depth());
|
|
||||||
statusBar()->showMessage(message);
|
|
||||||
}
|
|
||||||
#endif // !QT_NO_CLIPBOARD
|
|
||||||
}
|
|
||||||
|
|
||||||
void ImageViewer::zoomIn() {
|
|
||||||
scaleImage(1.25);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ImageViewer::zoomOut()
|
|
||||||
{
|
|
||||||
scaleImage(0.8);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ImageViewer::normalSize() {
|
|
||||||
imageLabel->adjustSize();
|
|
||||||
scaleFactor = 1.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ImageViewer::fitToWindow() {
|
|
||||||
bool fitToWindow = fitToWindowAct->isChecked();
|
|
||||||
scrollArea->setWidgetResizable(fitToWindow);
|
|
||||||
if (!fitToWindow)
|
|
||||||
normalSize();
|
|
||||||
updateActions();
|
|
||||||
}
|
|
||||||
|
|
||||||
void ImageViewer::about() {
|
|
||||||
QMessageBox::about(this, tr("About Image Viewer"),
|
|
||||||
tr("<p>The <b>Image Viewer</b> example shows how to combine QLabel "
|
|
||||||
"and QScrollArea to display an image. QLabel is typically used "
|
|
||||||
"for displaying a text, but it can also display an image. "
|
|
||||||
"QScrollArea provides a scrolling view around another widget. "
|
|
||||||
"If the child widget exceeds the size of the frame, QScrollArea "
|
|
||||||
"automatically provides scroll bars. </p><p>The example "
|
|
||||||
"demonstrates how QLabel's ability to scale its contents "
|
|
||||||
"(QLabel::scaledContents), and QScrollArea's ability to "
|
|
||||||
"automatically resize its contents "
|
|
||||||
"(QScrollArea::widgetResizable), can be used to implement "
|
|
||||||
"zooming and scaling features. </p><p>In addition the example "
|
|
||||||
"shows how to use QPainter to print an image.</p>"));
|
|
||||||
}
|
|
||||||
|
|
||||||
void ImageViewer::createActions() {
|
|
||||||
const QIcon connect_icon(":/images/connect.png");
|
|
||||||
const QIcon disconnect_icon(":/images/disconnect.png");
|
|
||||||
|
|
||||||
QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
|
|
||||||
|
|
||||||
QAction *openAct = fileMenu->addAction(tr("&Open..."), this, &ImageViewer::open);
|
|
||||||
openAct->setShortcut(QKeySequence::Open);
|
|
||||||
|
|
||||||
saveAsAct = fileMenu->addAction(tr("&Save As..."), this, &ImageViewer::saveAs);
|
|
||||||
saveAsAct->setEnabled(false);
|
|
||||||
|
|
||||||
printAct = fileMenu->addAction(tr("&Print..."), this, &ImageViewer::print);
|
|
||||||
printAct->setShortcut(QKeySequence::Print);
|
|
||||||
printAct->setEnabled(false);
|
|
||||||
|
|
||||||
fileMenu->addSeparator();
|
|
||||||
|
|
||||||
QAction *exitAct = fileMenu->addAction(tr("E&xit"), this, &ImageViewer::quitApplication);
|
|
||||||
exitAct->setShortcut(tr("Ctrl+Q"));
|
|
||||||
|
|
||||||
QMenu *editMenu = menuBar()->addMenu(tr("&Edit"));
|
|
||||||
|
|
||||||
copyAct = editMenu->addAction(tr("&Copy"), this, &ImageViewer::copy);
|
|
||||||
copyAct->setShortcut(QKeySequence::Copy);
|
|
||||||
copyAct->setEnabled(false);
|
|
||||||
|
|
||||||
QAction *pasteAct = editMenu->addAction(tr("&Paste"), this, &ImageViewer::paste);
|
|
||||||
pasteAct->setShortcut(QKeySequence::Paste);
|
|
||||||
|
|
||||||
QMenu *viewMenu = menuBar()->addMenu(tr("&View"));
|
|
||||||
|
|
||||||
zoomInAct = viewMenu->addAction(tr("Zoom &In (25%)"), this, &ImageViewer::zoomIn);
|
|
||||||
zoomInAct->setShortcut(QKeySequence::ZoomIn);
|
|
||||||
zoomInAct->setEnabled(false);
|
|
||||||
|
|
||||||
zoomOutAct = viewMenu->addAction(tr("Zoom &Out (25%)"), this, &ImageViewer::zoomOut);
|
|
||||||
zoomOutAct->setShortcut(QKeySequence::ZoomOut);
|
|
||||||
zoomOutAct->setEnabled(false);
|
|
||||||
|
|
||||||
normalSizeAct = viewMenu->addAction(tr("&Normal Size"), this, &ImageViewer::normalSize);
|
|
||||||
normalSizeAct->setShortcut(tr("Ctrl+S"));
|
|
||||||
normalSizeAct->setEnabled(false);
|
|
||||||
|
|
||||||
viewMenu->addSeparator();
|
|
||||||
|
|
||||||
fitToWindowAct = viewMenu->addAction(tr("&Fit to Window"), this, &ImageViewer::fitToWindow);
|
|
||||||
fitToWindowAct->setEnabled(false);
|
|
||||||
fitToWindowAct->setCheckable(true);
|
|
||||||
fitToWindowAct->setShortcut(tr("Ctrl+F"));
|
|
||||||
|
|
||||||
QMenu *camera_menu = menuBar()->addMenu(tr("&Camera"));
|
|
||||||
connect_camera_action = camera_menu->addAction(connect_icon, tr("&Connect"), this, &ImageViewer::connectCamera);
|
|
||||||
connect_camera_action->setStatusTip(tr("Connect to to camera and open device"));
|
|
||||||
disconnect_camera_action = camera_menu->addAction(disconnect_icon, tr("&Disconnect"), this, &ImageViewer::disconnectCamera);
|
|
||||||
disconnect_camera_action->setStatusTip(tr("Disconnect from the camera device"));
|
|
||||||
camera_menu->addSeparator();
|
|
||||||
grab_still_action = camera_menu->addAction(tr("&Grab still"), this, &ImageViewer::grabStillFromPylon);
|
|
||||||
grab_still_action->setStatusTip(tr("Grab single image from Pylon camera"));
|
|
||||||
grab_still_action->setShortcut(tr("Ctrl+ "));
|
|
||||||
grab_continuous_action = camera_menu->addAction(tr("&grab continuous"), this, &ImageViewer::startRecording);
|
|
||||||
grab_continuous_action->setShortcut(tr("Ctrl+Enter"));
|
|
||||||
|
|
||||||
QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
|
|
||||||
helpMenu->addAction(tr("&About"), this, &ImageViewer::about);
|
|
||||||
helpMenu->addAction(tr("About &Qt"), &QApplication::aboutQt);
|
|
||||||
|
|
||||||
QToolBar *toolbar = addToolBar("main toolbar");
|
|
||||||
toolbar->addSeparator();
|
|
||||||
toolbar->addAction(connect_camera_action);
|
|
||||||
toolbar->addAction(disconnect_camera_action);
|
|
||||||
toolbar->addSeparator();
|
|
||||||
toolbar->addAction(grab_still_action);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ImageViewer::updateActions() {
|
|
||||||
saveAsAct->setEnabled(!image.isNull());
|
|
||||||
copyAct->setEnabled(!image.isNull());
|
|
||||||
zoomInAct->setEnabled(!fitToWindowAct->isChecked());
|
|
||||||
zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
|
|
||||||
normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
|
|
||||||
disconnect_camera_action->setEnabled(pylon->isOpen());
|
|
||||||
connect_camera_action->setEnabled(!pylon->isOpen());
|
|
||||||
grab_still_action->setEnabled(pylon->isOpen());
|
|
||||||
grab_continuous_action->setEnabled(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ImageViewer::scaleImage(double factor) {
|
|
||||||
Q_ASSERT(imageLabel->pixmap());
|
|
||||||
scaleFactor *= factor;
|
|
||||||
imageLabel->resize(scaleFactor * imageLabel->pixmap()->size());
|
|
||||||
|
|
||||||
adjustScrollBar(scrollArea->horizontalScrollBar(), factor);
|
|
||||||
adjustScrollBar(scrollArea->verticalScrollBar(), factor);
|
|
||||||
|
|
||||||
zoomInAct->setEnabled(scaleFactor < 3.0);
|
|
||||||
zoomOutAct->setEnabled(scaleFactor > 0.333);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ImageViewer::quitApplication() {
|
|
||||||
if (pylon->isOpen()) {
|
|
||||||
pylon->closeCamera();
|
|
||||||
}
|
|
||||||
//pylon->terminate();
|
|
||||||
this->close();
|
|
||||||
}
|
|
||||||
|
|
||||||
void ImageViewer::adjustScrollBar(QScrollBar *scrollBar, double factor) {
|
|
||||||
scrollBar->setValue(int(factor * scrollBar->value()
|
|
||||||
+ ((factor - 1) * scrollBar->pageStep()/2)));
|
|
||||||
}
|
|
||||||
|
|
||||||
void ImageViewer::connectCamera() {
|
|
||||||
std::string message;
|
|
||||||
pylon->openCamera(message);
|
|
||||||
statusBar()->showMessage(QString::fromStdString(message));
|
|
||||||
updateActions();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void ImageViewer::disconnectCamera() {
|
|
||||||
pylon->closeCamera();
|
|
||||||
statusBar()->showMessage(tr("Camera closed!"));
|
|
||||||
updateActions();
|
|
||||||
}
|
|
||||||
|
|
||||||
void ImageViewer::startRecording() {}
|
|
||||||
|
|
||||||
void ImageViewer::stopRecording() {}
|
|
||||||
|
|
||||||
void ImageViewer::grabStillFromPylon() {
|
|
||||||
if (pylon->isOpen()) {
|
|
||||||
ImageSettings settings = pylon->getImageSettings();
|
|
||||||
Pylon::CGrabResultPtr image_ptr = pylon->grabFrame();
|
|
||||||
if (image_ptr.IsValid() && image_ptr->GrabSucceeded()) {
|
|
||||||
size_t stride;
|
|
||||||
image_ptr->GetStride(stride);
|
|
||||||
QImage img(static_cast<uchar *>(image_ptr->GetBuffer()), static_cast<int>(settings.width), static_cast<int>(settings.height),
|
|
||||||
QImage::Format::Format_Grayscale8);
|
|
||||||
setImage(img);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
statusBar()->showMessage(tr("Camera is not open! Connect to camera first!"));
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,86 +0,0 @@
|
|||||||
#ifndef IMAGEVIEWER_H
|
|
||||||
#define IMAGEVIEWER_H
|
|
||||||
|
|
||||||
#include <QMainWindow>
|
|
||||||
#include "pylonrecorder.h"
|
|
||||||
#include <QImage>
|
|
||||||
#if defined(QT_PRINTSUPPORT_LIB)
|
|
||||||
# include <QtPrintSupport/qtprintsupportglobal.h>
|
|
||||||
|
|
||||||
# if QT_CONFIG(printer)
|
|
||||||
# include <QPrinter>
|
|
||||||
# endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
|
||||||
class QAction;
|
|
||||||
class QLabel;
|
|
||||||
class QMenu;
|
|
||||||
class QScrollArea;
|
|
||||||
class QScrollBar;
|
|
||||||
QT_END_NAMESPACE
|
|
||||||
|
|
||||||
//! [0]
|
|
||||||
class ImageViewer : public QMainWindow
|
|
||||||
{
|
|
||||||
Q_OBJECT
|
|
||||||
|
|
||||||
public:
|
|
||||||
ImageViewer(QWidget *parent = nullptr);
|
|
||||||
~ImageViewer();
|
|
||||||
|
|
||||||
bool loadFile(const QString &);
|
|
||||||
|
|
||||||
private slots:
|
|
||||||
void grabStillFromPylon();
|
|
||||||
void open();
|
|
||||||
void saveAs();
|
|
||||||
void print();
|
|
||||||
void copy();
|
|
||||||
void paste();
|
|
||||||
void zoomIn();
|
|
||||||
void zoomOut();
|
|
||||||
void normalSize();
|
|
||||||
void fitToWindow();
|
|
||||||
void about();
|
|
||||||
void connectCamera();
|
|
||||||
void disconnectCamera();
|
|
||||||
void startRecording();
|
|
||||||
void stopRecording();
|
|
||||||
void quitApplication();
|
|
||||||
|
|
||||||
private:
|
|
||||||
void createActions();
|
|
||||||
void createMenus();
|
|
||||||
void createToolBar();
|
|
||||||
void updateActions();
|
|
||||||
|
|
||||||
bool saveFile(const QString &fileName);
|
|
||||||
void setImage(const QImage &newImage);
|
|
||||||
void scaleImage(double factor);
|
|
||||||
void adjustScrollBar(QScrollBar *scrollBar, double factor);
|
|
||||||
|
|
||||||
QImage image;
|
|
||||||
QLabel *imageLabel;
|
|
||||||
QScrollArea *scrollArea;
|
|
||||||
double scaleFactor = 1;
|
|
||||||
PylonRecorder *pylon;
|
|
||||||
|
|
||||||
#if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printer)
|
|
||||||
QPrinter printer;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
QAction *saveAsAct;
|
|
||||||
QAction *printAct;
|
|
||||||
QAction *copyAct;
|
|
||||||
QAction *zoomInAct;
|
|
||||||
QAction *zoomOutAct;
|
|
||||||
QAction *normalSizeAct;
|
|
||||||
QAction *fitToWindowAct;
|
|
||||||
QAction *grab_still_action;
|
|
||||||
QAction *grab_continuous_action;
|
|
||||||
QAction *connect_camera_action;
|
|
||||||
QAction *disconnect_camera_action;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
8
main.cpp
8
main.cpp
@ -1,4 +1,4 @@
|
|||||||
#include "imageviewer.h"
|
#include "pylonrecorder.h"
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QCommandLineParser>
|
#include <QCommandLineParser>
|
||||||
#include <pylon/PylonIncludes.h>
|
#include <pylon/PylonIncludes.h>
|
||||||
@ -6,12 +6,12 @@
|
|||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
QApplication app(argc, argv);
|
QApplication app(argc, argv);
|
||||||
QGuiApplication::setApplicationDisplayName(ImageViewer::tr("Image Viewer"));
|
QGuiApplication::setApplicationDisplayName(PylonRecorder::tr("Image Viewer"));
|
||||||
QCommandLineParser commandLineParser;
|
QCommandLineParser commandLineParser;
|
||||||
commandLineParser.addHelpOption();
|
commandLineParser.addHelpOption();
|
||||||
commandLineParser.addPositionalArgument(ImageViewer::tr("[file]"), ImageViewer::tr("Image file to open."));
|
commandLineParser.addPositionalArgument(PylonRecorder::tr("[file]"), PylonRecorder::tr("Image file to open."));
|
||||||
commandLineParser.process(QCoreApplication::arguments());
|
commandLineParser.process(QCoreApplication::arguments());
|
||||||
ImageViewer imageViewer;
|
PylonRecorder imageViewer;
|
||||||
if (!commandLineParser.positionalArguments().isEmpty()
|
if (!commandLineParser.positionalArguments().isEmpty()
|
||||||
&& !imageViewer.loadFile(commandLineParser.positionalArguments().front())) {
|
&& !imageViewer.loadFile(commandLineParser.positionalArguments().front())) {
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -1,71 +1,373 @@
|
|||||||
#include "pylonrecorder.h"
|
#include "pylonrecorder.h"
|
||||||
|
|
||||||
PylonRecorder::PylonRecorder():
|
#include <QApplication>
|
||||||
valid(false) {
|
#include <QClipboard>
|
||||||
Pylon::PylonInitialize();
|
//nclude <QColorSpace>
|
||||||
camera = new Pylon::CInstantCamera();
|
#include <QDir>
|
||||||
|
#include <QFileDialog>
|
||||||
|
#include <QImageReader>
|
||||||
|
#include <QImageWriter>
|
||||||
|
#include <QLabel>
|
||||||
|
#include <QMenuBar>
|
||||||
|
#include <QMessageBox>
|
||||||
|
#include <QMimeData>
|
||||||
|
#include <QPainter>
|
||||||
|
#include <QScreen>
|
||||||
|
#include <QScrollArea>
|
||||||
|
#include <QScrollBar>
|
||||||
|
#include <QStandardPaths>
|
||||||
|
#include <QStatusBar>
|
||||||
|
#include <QToolBar>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#if defined(QT_PRINTSUPPORT_LIB)
|
||||||
|
# include <QtPrintSupport/qtprintsupportglobal.h>
|
||||||
|
|
||||||
|
# if QT_CONFIG(printdialog)
|
||||||
|
# include <QPrintDialog>
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
PylonRecorder::PylonRecorder(QWidget *parent)
|
||||||
|
: QMainWindow(parent), imageLabel(new QLabel), scrollArea(new QScrollArea)
|
||||||
|
{
|
||||||
|
imageLabel->setBackgroundRole(QPalette::Base);
|
||||||
|
imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
|
||||||
|
imageLabel->setScaledContents(true);
|
||||||
|
|
||||||
|
scrollArea->setBackgroundRole(QPalette::Dark);
|
||||||
|
scrollArea->setWidget(imageLabel);
|
||||||
|
scrollArea->setVisible(false);
|
||||||
|
setCentralWidget(scrollArea);
|
||||||
|
pylon = new PylonWrapper();
|
||||||
|
createActions();
|
||||||
|
updateActions();
|
||||||
|
resize(QGuiApplication::primaryScreen()->availableSize() * 3 / 5);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
PylonRecorder::~PylonRecorder(){
|
||||||
|
delete pylon;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool PylonRecorder::loadFile(const QString &fileName) {
|
||||||
|
QImageReader reader(fileName);
|
||||||
|
reader.setAutoTransform(true);
|
||||||
|
const QImage newImage = reader.read();
|
||||||
|
if (newImage.isNull()) {
|
||||||
|
QMessageBox::information(this, QGuiApplication::applicationDisplayName(),
|
||||||
|
tr("Cannot load %1: %2")
|
||||||
|
.arg(QDir::toNativeSeparators(fileName), reader.errorString()));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
setImage(newImage);
|
||||||
|
|
||||||
|
setWindowFilePath(fileName);
|
||||||
|
|
||||||
|
const QString message = tr("Opened \"%1\", %2x%3, Depth: %4")
|
||||||
|
.arg(QDir::toNativeSeparators(fileName)).arg(image.width()).arg(image.height()).arg(image.depth());
|
||||||
|
statusBar()->showMessage(message);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PylonRecorder::setImage(const QImage &newImage) {
|
||||||
|
image = newImage;
|
||||||
|
// (image.colorSpace().isValid())
|
||||||
|
// image.convertToColorSpace(QColorSpace::SRgb);
|
||||||
|
imageLabel->setPixmap(QPixmap::fromImage(image));
|
||||||
|
scaleFactor = 1.0;
|
||||||
|
|
||||||
|
scrollArea->setVisible(true);
|
||||||
|
printAct->setEnabled(true);
|
||||||
|
fitToWindowAct->setEnabled(true);
|
||||||
|
updateActions();
|
||||||
|
|
||||||
|
if (!fitToWindowAct->isChecked())
|
||||||
|
imageLabel->adjustSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool PylonRecorder::saveFile(const QString &fileName) {
|
||||||
|
QImageWriter writer(fileName);
|
||||||
|
|
||||||
|
if (!writer.write(image)) {
|
||||||
|
QMessageBox::information(this, QGuiApplication::applicationDisplayName(),
|
||||||
|
tr("Cannot write %1: %2")
|
||||||
|
.arg(QDir::toNativeSeparators(fileName)), writer.errorString());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const QString message = tr("Wrote \"%1\"").arg(QDir::toNativeSeparators(fileName));
|
||||||
|
statusBar()->showMessage(message);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void initializeImageFileDialog(QFileDialog &dialog, QFileDialog::AcceptMode acceptMode) {
|
||||||
|
static bool firstDialog = true;
|
||||||
|
|
||||||
|
if (firstDialog) {
|
||||||
|
firstDialog = false;
|
||||||
|
const QStringList picturesLocations = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation);
|
||||||
|
dialog.setDirectory(picturesLocations.isEmpty() ? QDir::currentPath() : picturesLocations.last());
|
||||||
|
}
|
||||||
|
|
||||||
|
QStringList mimeTypeFilters;
|
||||||
|
const QByteArrayList supportedMimeTypes = acceptMode == QFileDialog::AcceptOpen
|
||||||
|
? QImageReader::supportedMimeTypes() : QImageWriter::supportedMimeTypes();
|
||||||
|
for (const QByteArray &mimeTypeName : supportedMimeTypes)
|
||||||
|
mimeTypeFilters.append(mimeTypeName);
|
||||||
|
mimeTypeFilters.sort();
|
||||||
|
dialog.setMimeTypeFilters(mimeTypeFilters);
|
||||||
|
dialog.selectMimeTypeFilter("image/jpeg");
|
||||||
|
if (acceptMode == QFileDialog::AcceptSave)
|
||||||
|
dialog.setDefaultSuffix("jpg");
|
||||||
}
|
}
|
||||||
|
|
||||||
PylonRecorder::~PylonRecorder() {
|
void PylonRecorder::open() {
|
||||||
Pylon::PylonTerminate();
|
QFileDialog dialog(this, tr("Open File"));
|
||||||
|
initializeImageFileDialog(dialog, QFileDialog::AcceptOpen);
|
||||||
|
|
||||||
|
while (dialog.exec() == QDialog::Accepted && !loadFile(dialog.selectedFiles().first())) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
void PylonRecorder::saveAs() {
|
||||||
|
QFileDialog dialog(this, tr("Save File As"));
|
||||||
|
initializeImageFileDialog(dialog, QFileDialog::AcceptSave);
|
||||||
|
|
||||||
|
while (dialog.exec() == QDialog::Accepted && !saveFile(dialog.selectedFiles().first())) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PylonRecorder::terminate() {
|
void PylonRecorder::print() {
|
||||||
try {
|
Q_ASSERT(imageLabel->pixmap());
|
||||||
//Pylon::PylonTerminate();
|
#if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printdialog)
|
||||||
} catch (const Pylon::GenericException &e) {
|
|
||||||
std::cerr << e.GetDescription() << std::endl;
|
QPrintDialog dialog(&printer, this);
|
||||||
|
|
||||||
|
if (dialog.exec()) {
|
||||||
|
QPainter painter(&printer);
|
||||||
|
QRect rect = painter.viewport();
|
||||||
|
QSize size = imageLabel->pixmap()->size();
|
||||||
|
size.scale(rect.size(), Qt::KeepAspectRatio);
|
||||||
|
painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
|
||||||
|
painter.setWindow(imageLabel->pixmap()->rect());
|
||||||
|
painter.drawPixmap(0, 0, *imageLabel->pixmap());
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PylonRecorder::isOpen() {
|
void PylonRecorder::copy() {
|
||||||
return valid;
|
#ifndef QT_NO_CLIPBOARD
|
||||||
|
QGuiApplication::clipboard()->setImage(image);
|
||||||
|
#endif // !QT_NO_CLIPBOARD
|
||||||
}
|
}
|
||||||
|
|
||||||
ImageSettings PylonRecorder::getImageSettings() {
|
#ifndef QT_NO_CLIPBOARD
|
||||||
ImageSettings settings;
|
static QImage clipboardImage() {
|
||||||
if (valid) {
|
if (const QMimeData *mimeData = QGuiApplication::clipboard()->mimeData()) {
|
||||||
Pylon::CIntegerParameter width( camera->GetNodeMap(), "Width");
|
if (mimeData->hasImage()) {
|
||||||
Pylon::CIntegerParameter height( camera->GetNodeMap(), "Height");
|
const QImage image = qvariant_cast<QImage>(mimeData->imageData());
|
||||||
Pylon::CEnumParameter pixelFormat( camera->GetNodeMap(), "PixelFormat");
|
if (!image.isNull())
|
||||||
settings.width = width.GetValue();
|
return image;
|
||||||
settings.height = height.GetValue();
|
}
|
||||||
}
|
}
|
||||||
return settings;
|
return QImage();
|
||||||
}
|
}
|
||||||
|
#endif // !QT_NO_CLIPBOARD
|
||||||
|
|
||||||
Pylon::CGrabResultPtr PylonRecorder::grabFrame() {
|
void PylonRecorder::paste() {
|
||||||
Pylon::CPylonImage img;
|
#ifndef QT_NO_CLIPBOARD
|
||||||
Pylon::CGrabResultPtr ptrGrabResult;
|
const QImage newImage = clipboardImage();
|
||||||
if (valid) {
|
if (newImage.isNull()) {
|
||||||
camera->StartGrabbing();
|
statusBar()->showMessage(tr("No image in clipboard"));
|
||||||
camera->RetrieveResult( 5000, ptrGrabResult, Pylon::TimeoutHandling_ThrowException);
|
} else {
|
||||||
camera->StopGrabbing();
|
setImage(newImage);
|
||||||
|
setWindowFilePath(QString());
|
||||||
|
const QString message = tr("Obtained image from clipboard, %1x%2, Depth: %3")
|
||||||
|
.arg(newImage.width()).arg(newImage.height()).arg(newImage.depth());
|
||||||
|
statusBar()->showMessage(message);
|
||||||
}
|
}
|
||||||
return ptrGrabResult;
|
#endif // !QT_NO_CLIPBOARD
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PylonRecorder::openCamera(std::string &message) {
|
void PylonRecorder::zoomIn() {
|
||||||
try {
|
scaleImage(1.25);
|
||||||
camera->Attach(Pylon::CTlFactory::GetInstance().CreateFirstDevice());
|
}
|
||||||
camera->Open();
|
|
||||||
valid = true;
|
void PylonRecorder::zoomOut()
|
||||||
message = "Successfully opened camera!";
|
{
|
||||||
} catch (const Pylon::GenericException &e) {
|
scaleImage(0.8);
|
||||||
message = e.GetDescription();
|
}
|
||||||
std::cerr << "An exception occurred." << std::endl << e.GetDescription() << std::endl;
|
|
||||||
valid = false;
|
void PylonRecorder::normalSize() {
|
||||||
|
imageLabel->adjustSize();
|
||||||
|
scaleFactor = 1.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PylonRecorder::fitToWindow() {
|
||||||
|
bool fitToWindow = fitToWindowAct->isChecked();
|
||||||
|
scrollArea->setWidgetResizable(fitToWindow);
|
||||||
|
if (!fitToWindow)
|
||||||
|
normalSize();
|
||||||
|
updateActions();
|
||||||
|
}
|
||||||
|
|
||||||
|
void PylonRecorder::about() {
|
||||||
|
QMessageBox::about(this, tr("About Image Viewer"),
|
||||||
|
tr("<p>The <b>Image Viewer</b> example shows how to combine QLabel "
|
||||||
|
"and QScrollArea to display an image. QLabel is typically used "
|
||||||
|
"for displaying a text, but it can also display an image. "
|
||||||
|
"QScrollArea provides a scrolling view around another widget. "
|
||||||
|
"If the child widget exceeds the size of the frame, QScrollArea "
|
||||||
|
"automatically provides scroll bars. </p><p>The example "
|
||||||
|
"demonstrates how QLabel's ability to scale its contents "
|
||||||
|
"(QLabel::scaledContents), and QScrollArea's ability to "
|
||||||
|
"automatically resize its contents "
|
||||||
|
"(QScrollArea::widgetResizable), can be used to implement "
|
||||||
|
"zooming and scaling features. </p><p>In addition the example "
|
||||||
|
"shows how to use QPainter to print an image.</p>"));
|
||||||
|
}
|
||||||
|
|
||||||
|
void PylonRecorder::createActions() {
|
||||||
|
const QIcon connect_icon(":/images/connect.png");
|
||||||
|
const QIcon disconnect_icon(":/images/disconnect.png");
|
||||||
|
|
||||||
|
QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
|
||||||
|
|
||||||
|
QAction *openAct = fileMenu->addAction(tr("&Open..."), this, &PylonRecorder::open);
|
||||||
|
openAct->setShortcut(QKeySequence::Open);
|
||||||
|
|
||||||
|
saveAsAct = fileMenu->addAction(tr("&Save As..."), this, &PylonRecorder::saveAs);
|
||||||
|
saveAsAct->setEnabled(false);
|
||||||
|
|
||||||
|
printAct = fileMenu->addAction(tr("&Print..."), this, &PylonRecorder::print);
|
||||||
|
printAct->setShortcut(QKeySequence::Print);
|
||||||
|
printAct->setEnabled(false);
|
||||||
|
|
||||||
|
fileMenu->addSeparator();
|
||||||
|
|
||||||
|
QAction *exitAct = fileMenu->addAction(tr("E&xit"), this, &PylonRecorder::quitApplication);
|
||||||
|
exitAct->setShortcut(tr("Ctrl+Q"));
|
||||||
|
|
||||||
|
QMenu *editMenu = menuBar()->addMenu(tr("&Edit"));
|
||||||
|
|
||||||
|
copyAct = editMenu->addAction(tr("&Copy"), this, &PylonRecorder::copy);
|
||||||
|
copyAct->setShortcut(QKeySequence::Copy);
|
||||||
|
copyAct->setEnabled(false);
|
||||||
|
|
||||||
|
QAction *pasteAct = editMenu->addAction(tr("&Paste"), this, &PylonRecorder::paste);
|
||||||
|
pasteAct->setShortcut(QKeySequence::Paste);
|
||||||
|
|
||||||
|
QMenu *viewMenu = menuBar()->addMenu(tr("&View"));
|
||||||
|
|
||||||
|
zoomInAct = viewMenu->addAction(tr("Zoom &In (25%)"), this, &PylonRecorder::zoomIn);
|
||||||
|
zoomInAct->setShortcut(QKeySequence::ZoomIn);
|
||||||
|
zoomInAct->setEnabled(false);
|
||||||
|
|
||||||
|
zoomOutAct = viewMenu->addAction(tr("Zoom &Out (25%)"), this, &PylonRecorder::zoomOut);
|
||||||
|
zoomOutAct->setShortcut(QKeySequence::ZoomOut);
|
||||||
|
zoomOutAct->setEnabled(false);
|
||||||
|
|
||||||
|
normalSizeAct = viewMenu->addAction(tr("&Normal Size"), this, &PylonRecorder::normalSize);
|
||||||
|
normalSizeAct->setShortcut(tr("Ctrl+S"));
|
||||||
|
normalSizeAct->setEnabled(false);
|
||||||
|
|
||||||
|
viewMenu->addSeparator();
|
||||||
|
|
||||||
|
fitToWindowAct = viewMenu->addAction(tr("&Fit to Window"), this, &PylonRecorder::fitToWindow);
|
||||||
|
fitToWindowAct->setEnabled(false);
|
||||||
|
fitToWindowAct->setCheckable(true);
|
||||||
|
fitToWindowAct->setShortcut(tr("Ctrl+F"));
|
||||||
|
|
||||||
|
QMenu *camera_menu = menuBar()->addMenu(tr("&Camera"));
|
||||||
|
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"));
|
||||||
|
disconnect_camera_action = camera_menu->addAction(disconnect_icon, tr("&Disconnect"), this, &PylonRecorder::disconnectCamera);
|
||||||
|
disconnect_camera_action->setStatusTip(tr("Disconnect from the camera device"));
|
||||||
|
camera_menu->addSeparator();
|
||||||
|
grab_still_action = camera_menu->addAction(tr("&Grab still"), this, &PylonRecorder::grabStillFromPylon);
|
||||||
|
grab_still_action->setStatusTip(tr("Grab single image from Pylon camera"));
|
||||||
|
grab_still_action->setShortcut(tr("Ctrl+ "));
|
||||||
|
grab_continuous_action = camera_menu->addAction(tr("&grab continuous"), this, &PylonRecorder::startRecording);
|
||||||
|
grab_continuous_action->setShortcut(tr("Ctrl+Enter"));
|
||||||
|
|
||||||
|
QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
|
||||||
|
helpMenu->addAction(tr("&About"), this, &PylonRecorder::about);
|
||||||
|
helpMenu->addAction(tr("About &Qt"), &QApplication::aboutQt);
|
||||||
|
|
||||||
|
QToolBar *toolbar = addToolBar("main toolbar");
|
||||||
|
toolbar->addSeparator();
|
||||||
|
toolbar->addAction(connect_camera_action);
|
||||||
|
toolbar->addAction(disconnect_camera_action);
|
||||||
|
toolbar->addSeparator();
|
||||||
|
toolbar->addAction(grab_still_action);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PylonRecorder::updateActions() {
|
||||||
|
saveAsAct->setEnabled(!image.isNull());
|
||||||
|
copyAct->setEnabled(!image.isNull());
|
||||||
|
zoomInAct->setEnabled(!fitToWindowAct->isChecked());
|
||||||
|
zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
|
||||||
|
normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
|
||||||
|
disconnect_camera_action->setEnabled(pylon->isOpen());
|
||||||
|
connect_camera_action->setEnabled(!pylon->isOpen());
|
||||||
|
grab_still_action->setEnabled(pylon->isOpen());
|
||||||
|
grab_continuous_action->setEnabled(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PylonRecorder::scaleImage(double factor) {
|
||||||
|
Q_ASSERT(imageLabel->pixmap());
|
||||||
|
scaleFactor *= factor;
|
||||||
|
imageLabel->resize(scaleFactor * imageLabel->pixmap()->size());
|
||||||
|
|
||||||
|
adjustScrollBar(scrollArea->horizontalScrollBar(), factor);
|
||||||
|
adjustScrollBar(scrollArea->verticalScrollBar(), factor);
|
||||||
|
|
||||||
|
zoomInAct->setEnabled(scaleFactor < 3.0);
|
||||||
|
zoomOutAct->setEnabled(scaleFactor > 0.333);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PylonRecorder::quitApplication() {
|
||||||
|
if (pylon->isOpen()) {
|
||||||
|
pylon->closeCamera();
|
||||||
}
|
}
|
||||||
return valid;
|
//pylon->terminate();
|
||||||
|
this->close();
|
||||||
|
}
|
||||||
|
|
||||||
|
void PylonRecorder::adjustScrollBar(QScrollBar *scrollBar, double factor) {
|
||||||
|
scrollBar->setValue(int(factor * scrollBar->value()
|
||||||
|
+ ((factor - 1) * scrollBar->pageStep()/2)));
|
||||||
|
}
|
||||||
|
|
||||||
|
void PylonRecorder::connectCamera() {
|
||||||
|
std::string message;
|
||||||
|
pylon->openCamera(message);
|
||||||
|
statusBar()->showMessage(QString::fromStdString(message));
|
||||||
|
updateActions();
|
||||||
}
|
}
|
||||||
|
|
||||||
void PylonRecorder::closeCamera() {
|
|
||||||
if (camera->IsOpen()) {
|
void PylonRecorder::disconnectCamera() {
|
||||||
try {
|
pylon->closeCamera();
|
||||||
camera->Close();
|
statusBar()->showMessage(tr("Camera closed!"));
|
||||||
valid = false;
|
updateActions();
|
||||||
} catch (const Pylon::GenericException &e) {
|
}
|
||||||
std::cerr << "An exception occurred." << std::endl << e.GetDescription() << std::endl;
|
|
||||||
|
void PylonRecorder::startRecording() {}
|
||||||
|
|
||||||
|
void PylonRecorder::stopRecording() {}
|
||||||
|
|
||||||
|
void PylonRecorder::grabStillFromPylon() {
|
||||||
|
if (pylon->isOpen()) {
|
||||||
|
ImageSettings settings = pylon->getImageSettings();
|
||||||
|
Pylon::CGrabResultPtr image_ptr = pylon->grabFrame();
|
||||||
|
if (image_ptr.IsValid() && image_ptr->GrabSucceeded()) {
|
||||||
|
size_t stride;
|
||||||
|
image_ptr->GetStride(stride);
|
||||||
|
QImage img(static_cast<uchar *>(image_ptr->GetBuffer()), static_cast<int>(settings.width), static_cast<int>(settings.height),
|
||||||
|
QImage::Format::Format_Grayscale8);
|
||||||
|
setImage(img);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
statusBar()->showMessage(tr("Camera is not open! Connect to camera first!"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,33 +1,86 @@
|
|||||||
#ifndef PYLONRECORDER_H
|
#ifndef IMAGEVIEWER_H
|
||||||
#define PYLONRECORDER_H
|
#define IMAGEVIEWER_H
|
||||||
|
|
||||||
#include <pylon/PylonIncludes.h>
|
#include <QMainWindow>
|
||||||
#include <pylon/BaslerUniversalInstantCamera.h>
|
#include "pylonwrapper.h"
|
||||||
|
#include <QImage>
|
||||||
|
#if defined(QT_PRINTSUPPORT_LIB)
|
||||||
|
# include <QtPrintSupport/qtprintsupportglobal.h>
|
||||||
|
|
||||||
struct ImageSettings {
|
# if QT_CONFIG(printer)
|
||||||
int64_t width = 0;
|
# include <QPrinter>
|
||||||
int64_t height = 0;
|
# endif
|
||||||
};
|
#endif
|
||||||
|
|
||||||
|
QT_BEGIN_NAMESPACE
|
||||||
|
class QAction;
|
||||||
|
class QLabel;
|
||||||
|
class QMenu;
|
||||||
|
class QScrollArea;
|
||||||
|
class QScrollBar;
|
||||||
|
QT_END_NAMESPACE
|
||||||
|
|
||||||
class PylonRecorder
|
//! [0]
|
||||||
|
class PylonRecorder : public QMainWindow
|
||||||
{
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
PylonRecorder();
|
PylonRecorder(QWidget *parent = nullptr);
|
||||||
~PylonRecorder();
|
~PylonRecorder();
|
||||||
|
|
||||||
ImageSettings getImageSettings();
|
bool loadFile(const QString &);
|
||||||
bool isOpen();
|
|
||||||
void terminate();
|
private slots:
|
||||||
bool openCamera(std::string &message);
|
void grabStillFromPylon();
|
||||||
void closeCamera();
|
void open();
|
||||||
Pylon::CGrabResultPtr grabFrame();
|
void saveAs();
|
||||||
|
void print();
|
||||||
|
void copy();
|
||||||
|
void paste();
|
||||||
|
void zoomIn();
|
||||||
|
void zoomOut();
|
||||||
|
void normalSize();
|
||||||
|
void fitToWindow();
|
||||||
|
void about();
|
||||||
|
void connectCamera();
|
||||||
|
void disconnectCamera();
|
||||||
|
void startRecording();
|
||||||
|
void stopRecording();
|
||||||
|
void quitApplication();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Pylon::CInstantCamera *camera;
|
void createActions();
|
||||||
bool valid;
|
void createMenus();
|
||||||
|
void createToolBar();
|
||||||
|
void updateActions();
|
||||||
|
|
||||||
|
bool saveFile(const QString &fileName);
|
||||||
|
void setImage(const QImage &newImage);
|
||||||
|
void scaleImage(double factor);
|
||||||
|
void adjustScrollBar(QScrollBar *scrollBar, double factor);
|
||||||
|
|
||||||
|
QImage image;
|
||||||
|
QLabel *imageLabel;
|
||||||
|
QScrollArea *scrollArea;
|
||||||
|
double scaleFactor = 1;
|
||||||
|
PylonWrapper *pylon;
|
||||||
|
|
||||||
|
#if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printer)
|
||||||
|
QPrinter printer;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
QAction *saveAsAct;
|
||||||
|
QAction *printAct;
|
||||||
|
QAction *copyAct;
|
||||||
|
QAction *zoomInAct;
|
||||||
|
QAction *zoomOutAct;
|
||||||
|
QAction *normalSizeAct;
|
||||||
|
QAction *fitToWindowAct;
|
||||||
|
QAction *grab_still_action;
|
||||||
|
QAction *grab_continuous_action;
|
||||||
|
QAction *connect_camera_action;
|
||||||
|
QAction *disconnect_camera_action;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // PYLONRECORDER_H
|
#endif
|
||||||
|
71
pylonwrapper.cpp
Normal file
71
pylonwrapper.cpp
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
#include "pylonwrapper.h"
|
||||||
|
|
||||||
|
PylonWrapper::PylonWrapper():
|
||||||
|
valid(false) {
|
||||||
|
Pylon::PylonInitialize();
|
||||||
|
camera = new Pylon::CInstantCamera();
|
||||||
|
}
|
||||||
|
|
||||||
|
PylonWrapper::~PylonWrapper() {
|
||||||
|
Pylon::PylonTerminate();
|
||||||
|
}
|
||||||
|
|
||||||
|
void PylonWrapper::terminate() {
|
||||||
|
try {
|
||||||
|
//Pylon::PylonTerminate();
|
||||||
|
} catch (const Pylon::GenericException &e) {
|
||||||
|
std::cerr << e.GetDescription() << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool PylonWrapper::isOpen() {
|
||||||
|
return valid;
|
||||||
|
}
|
||||||
|
|
||||||
|
ImageSettings PylonWrapper::getImageSettings() {
|
||||||
|
ImageSettings settings;
|
||||||
|
if (valid) {
|
||||||
|
Pylon::CIntegerParameter width( camera->GetNodeMap(), "Width");
|
||||||
|
Pylon::CIntegerParameter height( camera->GetNodeMap(), "Height");
|
||||||
|
Pylon::CEnumParameter pixelFormat( camera->GetNodeMap(), "PixelFormat");
|
||||||
|
settings.width = width.GetValue();
|
||||||
|
settings.height = height.GetValue();
|
||||||
|
}
|
||||||
|
return settings;
|
||||||
|
}
|
||||||
|
|
||||||
|
Pylon::CGrabResultPtr PylonWrapper::grabFrame() {
|
||||||
|
Pylon::CPylonImage img;
|
||||||
|
Pylon::CGrabResultPtr ptrGrabResult;
|
||||||
|
if (valid) {
|
||||||
|
camera->StartGrabbing();
|
||||||
|
camera->RetrieveResult( 5000, ptrGrabResult, Pylon::TimeoutHandling_ThrowException);
|
||||||
|
camera->StopGrabbing();
|
||||||
|
}
|
||||||
|
return ptrGrabResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool PylonWrapper::openCamera(std::string &message) {
|
||||||
|
try {
|
||||||
|
camera->Attach(Pylon::CTlFactory::GetInstance().CreateFirstDevice());
|
||||||
|
camera->Open();
|
||||||
|
valid = true;
|
||||||
|
message = "Successfully opened camera!";
|
||||||
|
} catch (const Pylon::GenericException &e) {
|
||||||
|
message = e.GetDescription();
|
||||||
|
std::cerr << "An exception occurred." << std::endl << e.GetDescription() << std::endl;
|
||||||
|
valid = false;
|
||||||
|
}
|
||||||
|
return valid;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PylonWrapper::closeCamera() {
|
||||||
|
if (camera->IsOpen()) {
|
||||||
|
try {
|
||||||
|
camera->Close();
|
||||||
|
valid = false;
|
||||||
|
} catch (const Pylon::GenericException &e) {
|
||||||
|
std::cerr << "An exception occurred." << std::endl << e.GetDescription() << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
33
pylonwrapper.h
Normal file
33
pylonwrapper.h
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#ifndef PYLONRECORDER_H
|
||||||
|
#define PYLONRECORDER_H
|
||||||
|
|
||||||
|
#include <pylon/PylonIncludes.h>
|
||||||
|
#include <pylon/BaslerUniversalInstantCamera.h>
|
||||||
|
|
||||||
|
struct ImageSettings {
|
||||||
|
int64_t width = 0;
|
||||||
|
int64_t height = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
class PylonWrapper
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
PylonWrapper();
|
||||||
|
~PylonWrapper();
|
||||||
|
|
||||||
|
ImageSettings getImageSettings();
|
||||||
|
bool isOpen();
|
||||||
|
void terminate();
|
||||||
|
bool openCamera(std::string &message);
|
||||||
|
void closeCamera();
|
||||||
|
Pylon::CGrabResultPtr grabFrame();
|
||||||
|
|
||||||
|
private:
|
||||||
|
Pylon::CInstantCamera *camera;
|
||||||
|
bool valid;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // PYLONRECORDER_H
|
12
recorder.pro
12
recorder.pro
@ -2,11 +2,13 @@ QT += widgets
|
|||||||
requires(qtConfig(filedialog))
|
requires(qtConfig(filedialog))
|
||||||
qtHaveModule(printsupport): QT += gui printsupport
|
qtHaveModule(printsupport): QT += gui printsupport
|
||||||
|
|
||||||
HEADERS = imageviewer.h \
|
HEADERS = \
|
||||||
pylonrecorder.h
|
pylonrecorder.h \
|
||||||
SOURCES = imageviewer.cpp \
|
pylonwrapper.h
|
||||||
main.cpp \
|
SOURCES = \
|
||||||
pylonrecorder.cpp
|
pylonrecorder.cpp \
|
||||||
|
pylonwrapper.cpp \
|
||||||
|
main.cpp
|
||||||
|
|
||||||
# install
|
# install
|
||||||
target.path = $$[QT_INSTALL_EXAMPLES]/widgets/widgets/imageviewer
|
target.path = $$[QT_INSTALL_EXAMPLES]/widgets/widgets/imageviewer
|
||||||
|
Loading…
Reference in New Issue
Block a user