基于Qt的图像采集系统
硬件
Point Gray Camera
型号:FL3-U3-13S2C-CS
参数
Sony IMX035 CMOS, 1/3", 3.63 µm
Rolling Shutter
1328x1048 at 120 FPS
USB3.0
系统及环境
Windows 7 64bit
Qt 5.1
驱动:FlyCapture v2.5 Release 4 - Windows 64bit
硬件连接
将相机直接接到电脑的USB3.0接口上就可以了。
程序功能
调用摄像头拍照,并在窗口中显示结果拍照结果。
开发的过程中主要是参考官方的文档,在sdk安装的文件夹里就有。
代码清单
代码结构
很简单,就一个类。
首先要在.pro文件中包含头文件和库
INCLUDEPATH += "C:/Program Files/Point Grey Research/FlyCapture2/include" LIBS += "C:\Program Files\Point Grey Research\FlyCapture2\lib64\FlyCapture2.lib"
main.cpp
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
初始化一个MainWindow,然后显示,没什么好说的。
mainwindows.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <FlyCapture2.h> #include <iostream> #include <QLabel> #include <QAction> #include <QStatusBar> #include <QMessageBox> #include <QMenu> #include <QMenuBar> using namespace std; using namespace FlyCapture2; class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = 0); void printCameraInfo(); ~MainWindow(); private: QLabel *imageLabel; QMenu *operationMenu; QMenu *aboutMenu; QMenu *cameraMenu; QAction *startAction; QAction *stopAction; QAction *aboutAction; QAction *cameraInfoAction; QLabel *msgLabel; QLabel *about; void createActions(); void createMenus(); void printError(Error e); void getCameraInfo(); PGRGuid guid; Error error; Camera cam; CameraInfo camInfo; public slots: void slotAbout(); int slotStart(); void slotStop(); void slotShowCameraInfo(); }; #endif // MAINWINDOW_H
首先要包含 FlyCapture2.h 这个头文件,然后定义好窗体的menu,action等等,还有操作相机的几个相关的私有成员。
main.cpp
#include "mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { this->setGeometry(100,100,1000,768); this->setWindowTitle(tr("FlyCapture View")); //状态栏设定 msgLabel=new QLabel; msgLabel->setMinimumSize(msgLabel->sizeHint()); this->statusBar()->addWidget(msgLabel); this->createActions(); this->createMenus(); imageLabel = new QLabel; imageLabel->setBackgroundRole(QPalette::Base); imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); imageLabel->setScaledContents(true); QPixmap image(":/images/images/monster.png"); imageLabel->setPixmap(image); this->setCentralWidget(imageLabel); //Initialization of camera BusManager busMgr; error = busMgr.GetCameraFromIndex(0, &guid); if (error != PGRERROR_OK) { printError( error ); //return -1; } const int k_numImages = 10; // Connect to a camera error = cam.Connect(&guid); if (error != PGRERROR_OK) { printError( error ); // return -1; } // Get the camera information error = cam.GetCameraInfo(&camInfo); if (error != PGRERROR_OK) { printError( error ); //return -1; } } MainWindow::~MainWindow() { } void MainWindow::createActions() { this->aboutAction = new QAction(tr("&About"), this); this->aboutAction->setStatusTip(tr("About author.")); this->connect(aboutAction, SIGNAL(triggered()), this, SLOT(slotAbout())); this->startAction = new QAction(tr("&Start"), this); this->startAction->setStatusTip(tr("Start capture.")); this->connect(startAction, SIGNAL(triggered()), this, SLOT(slotStart())); this->stopAction = new QAction(tr("&Stop"), this); this->stopAction->setStatusTip(tr("Stop capture.")); this->connect(stopAction, SIGNAL(triggered()), this, SLOT(slotStop())); this->cameraInfoAction = new QAction(tr("&CameraInfo"), this); this->cameraInfoAction->setStatusTip(tr("Camera Information.")); this->connect(cameraInfoAction, SIGNAL(triggered()), this, SLOT(slotShowCameraInfo())); } void MainWindow::createMenus() { this->operationMenu = this->menuBar()->addMenu(tr("&Operation")); this->operationMenu->addAction(startAction); this->operationMenu->addAction(stopAction); this->aboutMenu = this->menuBar()->addMenu(tr("&About")); this->aboutMenu->addAction(aboutAction); this->cameraMenu = this->menuBar()->addMenu(tr("&Camera")); this->cameraMenu->addAction(cameraInfoAction); } void MainWindow::slotAbout() { QMessageBox msg(this); msg.setWindowTitle("About"); msg.setText(tr("Version:1.0 Author:silangquan@gmail.com")); msg.exec(); } int MainWindow::slotStart() { // Start capturing images error = cam.StartCapture(); if (error != PGRERROR_OK) { printError( error ); return -1; } Image rawImage; error = cam.RetrieveBuffer( &rawImage ); if (error != PGRERROR_OK) { printError( error ); // continue; } // Create a converted image Image convertedImage; // Convert the raw image error = rawImage.Convert( PIXEL_FORMAT_RGB, &convertedImage ); if (error != PGRERROR_OK) { printError( error ); return -1; } // Save the image. If a file format is not passed in, then the file // extension is parsed to attempt to determine the file format. error = convertedImage.Save( "Test.jpg"); if (error != PGRERROR_OK) { printError( error ); return -1; } // } // Stop capturing images error = cam.StopCapture(); if (error != PGRERROR_OK) { printError( error ); return -1; } // Disconnect the camera error = cam.Disconnect(); if (error != PGRERROR_OK) { printError( error ); return -1; } QPixmap image("Test.jpg"); imageLabel->setPixmap(image); cout<<"Captured!"<<endl; return 0; } void MainWindow::slotStop() { cout<<"Stop!"<<endl; } void MainWindow::slotShowCameraInfo() { QMessageBox msg(this); msg.setWindowTitle("Camera Informations"); msg.setText(QString("Serial number - %1\nCamera model - %2\nCamera vendor - %3\nSensor - %4\nResolution - %5\nFirmware version - %6\nFirmware build time - %7\n\n") .arg(camInfo.serialNumber).arg(camInfo.modelName).arg(camInfo.vendorName).arg(camInfo.sensorInfo).arg(camInfo.sensorResolution) .arg(camInfo.firmwareVersion).arg(camInfo.firmwareBuildTime)); msg.exec(); } void MainWindow::printError(Error e) { e.PrintErrorTrace(); }
主要是函数,槽,界面布局的实现。
最终效果: