网上有很多dalsa相机的MFC的例子。笔者最近做项目用到dalsa线阵相机。学习了好几天。直接上代码:
pro 文件:
#-------------------------------------------------
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Dalsa
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
CONFIG += c++11
SOURCES +=
main.cpp
mainwindow.cpp
HEADERS +=
mainwindow.h
FORMS +=
mainwindow.ui
qnx: target.path = /tmp/ T A R G E T / b i n e l s e : u n i x : ! a n d r o i d : t a r g e t . p a t h = / o p t / {TARGET}/bin else: unix:!android: target.path = /opt/ TARGET/binelse:unix:!android:target.path=/opt/{TARGET}/bin
!isEmpty(target.path): INSTALLS += target
INCLUDEPATH += “D:/Program Files/Teledyne DALSA/Sapera/Include”
INCLUDEPATH += “D:/Program Files/Teledyne DALSA/Sapera/Classes/Basic”
LIBS += “D:/Program Files/Teledyne DALSA/Sapera/Lib/Win64/corapi.lib”
LIBS += “D:/Program Files/Teledyne DALSA/Sapera/Lib/Win64/SapClassBasic.lib”
win32:CONFIG(release, debug|release): LIBS += -LC:/opencv/build/x64/vc14/lib/ -lopencv_world341
else:win32:CONFIG(debug, debug|release): LIBS += -LC:/opencv/build/x64/vc14/lib/ -lopencv_world341d
INCLUDEPATH += C:/opencv/build/include
DEPENDPATH += C:/opencv/build/include
中间的lib 和opencv的库使用了绝对地址,需要自己修改
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include “SapClassBasic.h”
#include “opencv2/opencv.hpp”
using namespace cv;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
static void XferCallbackLft(SapXferCallbackInfo *pInfoLft);
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
private:
bool InitCarmer();
void printout(QString str);
protected:
SapManager m_pManager;
SapAcqDevice *m_pCamera;
SapBufferWithTrash *m_pBuffer;
SapTransfer* m_pXfer;
SapView *m_pView;
SapFeature *m_pFeature;
private:
Ui::MainWindow *ui;
bool m_Flag = false;//设置标志位,以判定是否有设备
bool initstate=false;
int num;
QList l;
};
#endif // MAINWINDOW_H
cpp
#include “mainwindow.h”
#include “ui_mainwindow.h”
#include
static int m_num=0;
static int nownum=0;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
InitCarmer();
}
MainWindow::~MainWindow()
{
m_pXfer->Destroy();
m_pView->Destroy();
m_pBuffer->Destroy();
m_pCamera->Destroy();
delete ui;
}
void MainWindow::XferCallbackLft(SapXferCallbackInfo *pInfo)
{
// Display the last transferred frame
MainWindow *pView = (MainWindow *)pInfo->GetContext();
BYTE pData;
void pDataAddr = &pData;
BOOL success = pView->m_pBuffer->GetAddress(nownum,&pDataAddr);
if (nownum==0)
nownum=1;
else
nownum=0;
int width = pView->m_pBuffer->GetWidth();
int height = pView->m_pBuffer->GetHeight();
cv::Mat img = cv::Mat::zeros(cv::Size(width, height), CV_8U);
memcpy(img.data, pDataAddr, widthheight);
success = pView->m_pBuffer->ReleaseAddress(pDataAddr);
//cv:imshow(“image”, img);
QString q_PathName = QString::number(m_num) + “.bmp”;
std::string str = q_PathName.toStdString();
const char *m_PathName1 = str.c_str();
//printf(m_PathName);
qDebug() << m_PathName1;
cv::imwrite(m_PathName1, img); //可以存图
m_num += 1;
}
bool MainWindow::InitCarmer()
{
char m_SerName[100];
char m_ResName[100];
m_pManager.GetServerName(0, SapManager::ResourceAcqDevice, m_SerName);//CAMERA 相机名称
m_pManager.GetResourceName(1, SapManager::ResourceAcqDevice, m_ResName);//UESR ID,可自行设置
SapLocation loc(m_SerName,0);//默认值0,1会报错
m_pCamera = new SapAcqDevice(loc,“./T_Linea_C2048-7.ccf”);//创建相机对象
m_pBuffer = new SapBufferWithTrash(2,m_pCamera);//创建缓冲区对象
m_pView = new SapView(m_pBuffer);//创建显示区对象
m_pXfer = new SapAcqDeviceToBuf(m_pCamera,m_pBuffer,XferCallbackLft,this);//绑定相机与缓冲区
// m_pFeature = new SapFeature(m_pCamera->GetLocation());//创建Feature对象以获取参数信息
//创建对象资源
if(!m_pCamera->Create())
{
qDebug()<<“create SapAcqDevice error”;
return false;
}
if(!m_pBuffer->Create())
{
qDebug()<<“create SapBufferWithTrash error”;
return false;
}
if(!m_pView->Create())
{
qDebug()<<“create SapView error”;
return false;
}
// if(!m_pFeature->Create())
// {
// qDebug()<<“create SapFeature error”;
// return false;
// }
if(!m_pXfer->Create())
{
qDebug()<<“create SapAcqDeviceToBuf error”;
return false;
}
m_pXfer->Init();
double m_tmp=0;//初始化设备温度值
m_pCamera->GetFeatureValue("DeviceTemperature", &m_tmp);//不可用int型,否则数值不正确
qDebug() << "Device:" << m_SerName <
}
void MainWindow::printout(QString str)
{
qDebug() << str;
}
void MainWindow::on_pushButton_clicked()
{
bool success = m_pXfer->Grab();
}
void MainWindow::on_pushButton_2_clicked()
{
bool success = m_pXfer->Freeze();
}