0.程序运行效果
1.工程文件
#-------------------------------------------------
#
# Project created by QtCreator 2022-10-27T11:46:55
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = SaperaGigEVisionCameraDemo
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
CONFIG += c++11
SOURCES += \
dalsacamera.cpp \
main.cpp \
dalsacamerawidget.cpp
HEADERS += \
dalsacamera.h \
dalsacamerawidget.h
FORMS += \
dalsacamerawidget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
RESOURCES += \
images.qrc
INCLUDEPATH += "C:/Program Files/Teledyne DALSA/Sapera/Include"
INCLUDEPATH += "C:/Program Files/Teledyne DALSA/Sapera/Classes/Basic"
LIBS += "C:/Program Files/Teledyne DALSA/Sapera/Lib/Win64/corapi.lib"
LIBS += "C:/Program Files/Teledyne DALSA/Sapera/Lib/Win64/SapClassBasic.lib"
INCLUDEPATH += C:\opencv\build\include
QMAKE_LIBDIR += "C:/opencv/build/x64/vc15/lib"
win32:LIBS += "C:/opencv/build/x64/vc15/lib/opencv_world453d.lib" \
"C:/opencv/build/x64/vc15/lib/opencv_world453.lib"
2. 相机类文件
//dalsacamera.h
#ifndef DALSACAMERA_H
#define DALSACAMERA_H
#include
#include "SapClassBasic.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#if (CV_MAJOR_VERSION >= 3)
#include "opencv2/videoio/videoio.hpp"
#endif
#include
using namespace cv;
typedef struct
{
double dGain;
double dExpoureTime;
double dLineRate;
BOOL bTriggerMode;
UINT32 nWidth;
UINT32 nHeight;
UINT32 nOffsetX;
} CONSTANTLY_CHANGING_PARAMS;
class DalsaCamera : public QObject
{
Q_OBJECT
public:
explicit DalsaCamera(QObject *parent = nullptr, QString servername = QString("Linea_M4096-7um_1"));
~DalsaCamera();
BOOL CreateObjects();
BOOL DestroyObjects();
void OnGrab();
void OnSnap();
void OnFreeze();
static void XferCallback(SapXferCallbackInfo *pInfo);
protected:
//dalsa commonly used data structure
SapAcqDevice *m_AcqDevice;
SapBuffer *m_Buffers;
SapTransfer *m_Xfer;
//dalsa camera some feature and status
bool m_isconnected;
QString m_SN;
long m_frameId;
QString m_servername;
//dalsa configure file releted
bool m_bUseConfigFile;
QString m_configureFileName;
QString m_configureFileDirectory;
//constantly changing parameters, will show in GUI
CONSTANTLY_CHANGING_PARAMS m_objParameters;
signals:
void canShowImages(cv::Mat im);
void signalSettingsChanged(CONSTANTLY_CHANGING_PARAMS camerafeatures);
public slots:
//methods to support parameter operator
void saveSettings(CONSTANTLY_CHANGING_PARAMS camerafeatures);
public:
void initCamera();
void destroyCamera();
//methods to support parameter operator
void loadSettings();
};
#endif // DALSACAMERA_H
//dalsacamera.cpp
#include "dalsacamera.h"
#include
#include
#include
#include
DalsaCamera::DalsaCamera(QObject *parent, QString servername) : QObject(parent)
,m_AcqDevice(nullptr)
,m_Buffers(nullptr)
,m_Xfer(nullptr)
,m_isconnected(false)
,m_SN("")
,m_frameId(0)
,m_servername(servername)
{
initCamera();
}
DalsaCamera::~DalsaCamera()
{
destroyCamera();
}
BOOL DalsaCamera::CreateObjects()
{
// Create acquisition object
if (m_AcqDevice && !*m_AcqDevice && !m_AcqDevice->Create())
{
DestroyObjects();
return FALSE;
}
// Create buffer object
if (m_Buffers && !*m_Buffers)
{
if( !m_Buffers->Create())
{
DestroyObjects();
return FALSE;
}
// Clear all buffers
m_Buffers->Clear();
}
// Set next empty with trash cycle mode for transfer
if (m_Xfer && m_Xfer->GetPair(0))
{
if (!m_Xfer->GetPair(0)->SetCycleMode(SapXferPair::CycleNextWithTrash))
{
DestroyObjects();
return FALSE;
}
}
// Create transfer object
if (m_Xfer && !*m_Xfer && !m_Xfer->Create())
{
DestroyObjects();
return FALSE;
}
return TRUE;
}
BOOL DalsaCamera::DestroyObjects()
{
// Destroy transfer object
if (m_Xfer && *m_Xfer) m_Xfer->Destroy();
// Destroy buffer object
if (m_Buffers && *m_Buffers) m_Buffers->Destroy();
// Destroy acquisition object
if (m_AcqDevice && *m_AcqDevice) m_AcqDevice->Destroy();
return TRUE;
}
void DalsaCamera::XferCallback(SapXferCallbackInfo *pInfo)
{
qDebug() << "Call back function called successfully!" << endl;
DalsaCamera *pDlg = (DalsaCamera *)pInfo->GetContext();
pDlg->m_frameId++;
BYTE byteData;
void *pData = &byteData;
bool success=pDlg->m_Buffers->GetAddress(&pData);
if (!success)
{
return;
}
int width = pDlg->m_Buffers->GetWidth();
int height = pDlg->m_Buffers->GetHeight();
cv::Mat im=cv::Mat(height,width,CV_8UC1,(uchar*)pData).clone();
cv::GaussianBlur(im,im,cv::Size(3,3),1);
//show image or send image to handle
emit pDlg->canShowImages(im);
pDlg->m_Buffers->ReleaseAddress((void*)pData);
pDlg->m_Buffers->Clear(pDlg->m_Buffers->GetIndex());
}
void DalsaCamera::initCamera()
{
SapLocation loc(m_servername.toLatin1().data(), 0);
m_AcqDevice = new SapAcqDevice(loc, FALSE);
m_Buffers = new SapBufferWithTrash(3, m_AcqDevice);
m_Xfer = new SapAcqDeviceToBuf(m_AcqDevice,m_Buffers,XferCallback,this);
if (CreateObjects() == true){
qDebug() << "Camera connected successfully!" << endl;
char featureStringValue[64];
m_AcqDevice->GetFeatureValue("DeviceSerialNumber",featureStringValue,sizeof(featureStringValue));
QString str(featureStringValue);
m_SN=str;
m_isconnected=true;
}
}
void DalsaCamera::destroyCamera()
{
if(m_Xfer->IsGrabbing())
m_Xfer->Freeze();
DestroyObjects();
if (m_Xfer) delete m_Xfer;
if (m_Buffers) delete m_Buffers;
if (m_AcqDevice) delete m_AcqDevice;
}
void DalsaCamera::OnGrab()
{
if(m_Xfer){
m_Xfer->Grab();
}
}
void DalsaCamera::OnSnap()
{
if(m_Xfer)
{
m_Xfer->Snap();
}
}
void DalsaCamera::OnFreeze()
{
if(m_Xfer){
m_Xfer->Freeze();
}
}
void DalsaCamera::loadSettings()
{
if (m_AcqDevice)
{
m_AcqDevice->GetFeatureValue("Gain",&m_objParameters.dGain);
m_AcqDevice->GetFeatureValue("ExposureTime",&m_objParameters.dExpoureTime);
m_AcqDevice->GetFeatureValue("AcquisitionLineRate",&m_objParameters.dLineRate);
m_AcqDevice->GetFeatureValue("TriggerMode",&m_objParameters.bTriggerMode);
m_AcqDevice->GetFeatureValue("Width",&m_objParameters.nWidth);
m_AcqDevice->GetFeatureValue("Height",&m_objParameters.nHeight);
m_AcqDevice->GetFeatureValue("OffsetX",&m_objParameters.nOffsetX);
emit signalSettingsChanged(m_objParameters);
}
}
void DalsaCamera::saveSettings(CONSTANTLY_CHANGING_PARAMS camerafeatures)
{
std::memcpy(&m_objParameters,&camerafeatures, sizeof(CONSTANTLY_CHANGING_PARAMS));
if (m_AcqDevice)
{
m_AcqDevice->SetUpdateFeatureMode(SapAcqDevice::UpdateFeatureManual);
m_AcqDevice->SetFeatureValue("Gain",m_objParameters.dGain);
m_AcqDevice->SetFeatureValue("ExposureTime",m_objParameters.dExpoureTime);
m_AcqDevice->SetFeatureValue("AcquisitionLineRate",m_objParameters.dLineRate);
m_AcqDevice->SetFeatureValue("TriggerMode",m_objParameters.bTriggerMode);
m_AcqDevice->SetFeatureValue("Width",m_objParameters.nWidth);
m_AcqDevice->SetFeatureValue("Height",m_objParameters.nHeight);
m_AcqDevice->SetFeatureValue("OffsetX",m_objParameters.nOffsetX);
m_AcqDevice->UpdateFeaturesToDevice();
m_AcqDevice->SetUpdateFeatureMode(SapAcqDevice::UpdateFeatureAuto);
}
}
//窗口主程序
#ifndef DALSACAMERAWIDGET_H
#define DALSACAMERAWIDGET_H
#include
#include "dalsacamera.h"
namespace Ui {
class DalsaCameraWidget;
}
class DalsaCameraWidget : public QWidget
{
Q_OBJECT
enum ServerCategory
{
ServerAll,
ServerAcq,
ServerAcqDevice
};
public:
explicit DalsaCameraWidget(QWidget *parent = nullptr);
~DalsaCameraWidget();
private slots:
void on_pushButton_Exit_clicked();
void on_pushButton_snap_clicked();
void on_pushButton_grab_clicked();
void on_pushButton_freeze_clicked();
void showImages(cv::Mat im);
void updateFeaturesOnGUI(CONSTANTLY_CHANGING_PARAMS camerafeatures);
void on_pushButton_OK_clicked();
void on_comboBoxServer_currentIndexChanged(int index);
void on_comboBox_trigmode_currentIndexChanged(int index);
private:
Ui::DalsaCameraWidget *ui;
ServerCategory m_serverCategory;
bool m_bShowCameraLinkServers;
int m_resourceIndex;
bool bCameraChanged;
QString m_serverName;
void InitServerCombo();
void InitResourceCombo();
void loadSettings();
void saveSettings(CONSTANTLY_CHANGING_PARAMS camerafeatures);
DalsaCamera* dcam;
// DalsaCamera dcam;
};
#endif // DALSACAMERAWIDGET_H
//dalsawidget.cpp
#include "dalsacamerawidget.h"
#include "ui_dalsacamerawidget.h"
#include
DalsaCameraWidget::DalsaCameraWidget(QWidget *parent) :
QWidget(parent)
,ui(new Ui::DalsaCameraWidget)
,m_serverCategory(ServerAcqDevice)
,m_bShowCameraLinkServers(false)
,m_resourceIndex(-1)
,bCameraChanged(false)
,dcam(nullptr)
{
ui->setupUi(this);
InitServerCombo();
InitResourceCombo();
dcam = new DalsaCamera(NULL, m_serverName);
qRegisterMetaType("cv::Mat");
qRegisterMetaType("constantly_changing_params");
connect(dcam, SIGNAL(canShowImages(cv::Mat)),this, SLOT(showImages(cv::Mat)));
connect(dcam, SIGNAL(signalSettingsChanged(CONSTANTLY_CHANGING_PARAMS)),this, SLOT(updateFeaturesOnGUI(CONSTANTLY_CHANGING_PARAMS)));
loadSettings();
}
DalsaCameraWidget::~DalsaCameraWidget()
{
delete ui;
}
void DalsaCameraWidget::on_pushButton_Exit_clicked()
{
this->close();
}
void DalsaCameraWidget::on_pushButton_snap_clicked()
{
qDebug() << "snap clicked!" << endl;
if (dcam != NULL)
dcam->OnSnap();
// dcam.OnSnap();
}
void DalsaCameraWidget::on_pushButton_grab_clicked()
{
qDebug() << "grab clicked!" << endl;
if (dcam != NULL)
dcam->OnGrab();
// dcam.OnGrab();
}
void DalsaCameraWidget::on_pushButton_freeze_clicked()
{
qDebug() << "freeze clicked!" << endl;
if (dcam != NULL)
dcam->OnFreeze();
// dcam.OnFreeze();
}
void DalsaCameraWidget::showImages(Mat im)
{
QImage img = QImage((const unsigned char*)(im.data),
im.cols, im.rows, QImage::Format_Mono);
img = img.scaled(ui->label_image->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
ui->label_image->setPixmap(QPixmap::fromImage(img));
}
void DalsaCameraWidget::InitServerCombo()
{
for (int i = 0; i < SapManager::GetServerCount(); i++)
{
if (!SapManager::IsServerAccessible(i))
continue;
// Does this server support "Acq" (frame-grabber) or "AcqDevice" (camera)?
bool bAcq = (m_serverCategory == ServerAcq || m_serverCategory == ServerAll)
&& (SapManager::GetResourceCount(i, SapManager::ResourceAcq) > 0);
// when m_serverCategory is ServerAcqDevice, show only servers that have only one resource that is an acqdevice and no acq
bool bAcqDevice = (m_serverCategory == ServerAcqDevice || m_serverCategory == ServerAll)
&& (SapManager::GetResourceCount(i, SapManager::ResourceAcqDevice) == 1) && (SapManager::GetResourceCount(i, SapManager::ResourceAcq) == 0);
if (bAcq || bAcqDevice || m_bShowCameraLinkServers)
{
char name[128];
if (SapManager::GetServerName(i, name, sizeof(name)))
{
if (strstr(name, "CameraLink_") != NULL)
{
if (bAcq || m_bShowCameraLinkServers)
ui->comboBoxServer->addItem(name);
}
// Exclude Z-Trak from the server list
else if (strstr(name, "LP1") == NULL && strstr(name, "LP2") == NULL)
{
// Add server name to combo
ui->comboBoxServer->addItem(name);
}
}
}
}
// At least one server must be available
if (ui->comboBoxServer->count() <= 0)
{
return;
}
// Select server name
if(ui->comboBoxServer->findText(m_serverName) == -1)
{
// If not found, select first one
ui->comboBoxServer->setCurrentIndex(0);
m_serverName = ui->comboBoxServer->currentText();
}
}
void DalsaCameraWidget::InitResourceCombo()
{
int i = 0;
// Clear combo content
ui->comboBoxResource->clear();
// Check if the server name is present on the system
if (SapManager::GetServerIndex(m_serverName.toLatin1().data()) != SapLocation::ServerUnknown)
{
// Add "Acq" resources (frame-grabbers) to combo
for (i = 0; i < SapManager::GetResourceCount(m_serverName.toLatin1().data(), SapManager::ResourceAcq); i++)
{
char name[128];
if (SapManager::GetResourceName(m_serverName.toLatin1().data(), SapManager::ResourceAcq, i, name, sizeof(name)))
if (SapManager::IsResourceAvailable(m_serverName.toLatin1().data(), SapManager::ResourceAcq, i) == FALSE)
CorSnprintf(name, sizeof(name), "%s", "Not Available - Resource In Use");
ui->comboBoxResource->addItem(name);
}
// Add "AcqDevice" resources (cameras) to combo only if there are no frame grabber resources
if (SapManager::GetResourceCount(m_serverName.toLatin1().data(), SapManager::ResourceAcq) == 0)
{
for (i = 0; i < SapManager::GetResourceCount(m_serverName.toLatin1().data(), SapManager::ResourceAcqDevice); i++)
{
char name[128];
if (SapManager::GetResourceName(m_serverName.toLatin1().data(), SapManager::ResourceAcqDevice, i, name, sizeof(name)))
ui->comboBoxResource->addItem(name);
}
}
}
// Select resource index
if(m_resourceIndex < ui->comboBoxResource->count() && m_resourceIndex >=0)
{
ui->comboBoxResource->setCurrentIndex(m_resourceIndex);
} else
{
// If not found, select first one
ui->comboBoxResource->setCurrentIndex(0);
m_resourceIndex = 0;
}
}
void DalsaCameraWidget::on_pushButton_OK_clicked()
{
CONSTANTLY_CHANGING_PARAMS camerafeatures;
camerafeatures.dGain = ui->lineEdit_gain->text().toDouble();
camerafeatures.dExpoureTime = ui->lineEdit_exposure->text().toDouble();
camerafeatures.dLineRate = ui->lineEdit_lineRate->text().toDouble();
if(ui->comboBox_trigmode->currentIndex() == 0)
{
camerafeatures.bTriggerMode = false;
} else{
camerafeatures.bTriggerMode = true;
}
camerafeatures.nWidth = ui->lineEdit_width->text().toUInt();
camerafeatures.nHeight = ui->lineEdit_height->text().toUInt();
camerafeatures.nOffsetX = ui->lineEdit_xoffset->text().toUInt();
saveSettings(camerafeatures);
}
void DalsaCameraWidget::on_comboBoxServer_currentIndexChanged(int index)
{
ui->comboBoxServer->setCurrentIndex(index);
QString tmpServerName = ui->comboBoxServer->currentText();
if (m_serverName != tmpServerName)
{
m_serverName = tmpServerName;
if (dcam != NULL) {
disconnect(dcam, SIGNAL(canShowImages(cv::Mat)),this, SLOT(showImages(cv::Mat)));
disconnect(dcam, SIGNAL(signalSettingsChanged(CONSTANTLY_CHANGING_PARAMS)),this, SLOT(updateFeaturesOnGUI(CONSTANTLY_CHANGING_PARAMS)));
delete dcam;
dcam = new DalsaCamera(NULL, m_serverName);
connect(dcam, SIGNAL(canShowImages(cv::Mat)),this, SLOT(showImages(cv::Mat)));
connect(dcam, SIGNAL(signalSettingsChanged(CONSTANTLY_CHANGING_PARAMS)),this, SLOT(updateFeaturesOnGUI(CONSTANTLY_CHANGING_PARAMS)));
}
}
InitResourceCombo();
loadSettings();
}
void DalsaCameraWidget::on_comboBox_trigmode_currentIndexChanged(int index)
{
ui->comboBox_trigmode->setCurrentIndex(index);
}
void DalsaCameraWidget::loadSettings()
{
if (dcam != NULL){
dcam->loadSettings();
}
}
void DalsaCameraWidget::saveSettings(CONSTANTLY_CHANGING_PARAMS camerafeatures)
{
if (dcam != NULL){
dcam->saveSettings(camerafeatures);
}
}
void DalsaCameraWidget::updateFeaturesOnGUI(CONSTANTLY_CHANGING_PARAMS camerafeatures)
{
ui->lineEdit_gain->setText(QString::number(camerafeatures.dGain));
ui->lineEdit_exposure->setText(QString::number(camerafeatures.dExpoureTime));
ui->lineEdit_lineRate->setText(QString::number(camerafeatures.dLineRate));
if(camerafeatures.bTriggerMode == true)
{
ui->comboBox_trigmode->setCurrentIndex(1);
} else{
ui->comboBox_trigmode->setCurrentIndex(0);
}
ui->lineEdit_width->setText(QString::number(camerafeatures.nWidth));
ui->lineEdit_height->setText(QString::number(camerafeatures.nHeight));
ui->lineEdit_xoffset->setText(QString::number(camerafeatures.nOffsetX));
}
4. 主程序
#include "dalsacamerawidget.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
DalsaCameraWidget w;
w.show();
return a.exec();
}
5. 界面文件xml格式
DalsaCameraWidget
0
0
741
505
0
0
Sapera GigE-Vision Camera Demo
-
:/image/images/DalsaBanner.bmp
-
-
QLayout::SetDefaultConstraint
-
0
0
Acquisition Control
-
Snap
-
Grab
-
Freeze
-
Qt::Horizontal
68
20
-
OK
-
Exit
-
-
-
Acquistion Option
-
-
-
General Settings
-
Gain
-
1
-
Exposure
-
50
-
lineRate
-
TrigMode
-
-
off
-
on
-
Width
-
Hight
-
1024
-
Xoffset
-
0
-
3333.33
-
4096
-
-
-