213 lines
5.4 KiB
C++
213 lines
5.4 KiB
C++
#include "hd_dalsacamera_module.h"
|
|||
|
|
|
||
|
|
//0106 断连提示
|
||
|
|
void _stdcall ExceptionCB(unsigned int nMesgType, void* pUser) {
|
||
|
|
mv_Status = "disconnect";
|
||
|
|
qDebug() << "[Error] " << " LMI camera disconnects!";
|
||
|
|
}
|
||
|
|
|
||
|
|
Hd_DalsaCamera_module* create()
|
||
|
|
{
|
||
|
|
qDebug() << "[INFO] " << "Hd_DalsaCamera_module create success!" ;
|
||
|
|
return new Hd_DalsaCamera_module;
|
||
|
|
}
|
||
|
|
|
||
|
|
void destory(Hd_DalsaCamera_module * ptr)
|
||
|
|
{
|
||
|
|
if(ptr)
|
||
|
|
{
|
||
|
|
delete ptr;
|
||
|
|
ptr = nullptr;
|
||
|
|
}
|
||
|
|
qDebug() << "[INFO] " << "Hd_DalsaCamera_module destory success!";
|
||
|
|
}
|
||
|
|
void XferCallback(SapXferCallbackInfo* pInfo)
|
||
|
|
{
|
||
|
|
// Display the last transferred frame
|
||
|
|
|
||
|
|
//DalsaImg* pView = (DalsaImg*)pInfo->GetContext();
|
||
|
|
//SapBuffer* m_Buffers = (SapBuffer*)pInfo->GetContext();;
|
||
|
|
BYTE pData;
|
||
|
|
void* pDataAddr = &pData;
|
||
|
|
BOOL success =m_Buffers->GetAddress(&pDataAddr);
|
||
|
|
if(success!=true)
|
||
|
|
{
|
||
|
|
qDebug() << "[Error] " << "Hd_DalsaCamera_module grab failed!" ;
|
||
|
|
}
|
||
|
|
int width = m_Buffers->GetWidth();
|
||
|
|
int height = m_Buffers->GetHeight();
|
||
|
|
srcImage = cv::Mat::zeros(cv::Size(width, height), CV_8U);
|
||
|
|
memcpy(srcImage.data, pDataAddr, width * height); //将相机的数据流转 Mat
|
||
|
|
if(!srcImage.empty())
|
||
|
|
{
|
||
|
|
m_flag = true;
|
||
|
|
cv::imwrite("dalsa.bmp", srcImage);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
void getDalsaDeviceList()
|
||
|
|
{
|
||
|
|
SapManager m_SapManager;
|
||
|
|
QString qtServername;
|
||
|
|
//ServerCount指采集卡数+主板自带网口连接的相机数
|
||
|
|
if (m_SapManager.GetServerCount() == 1)
|
||
|
|
{
|
||
|
|
qDebug() << "[Error] " << "Device List NULL";
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
for (int i = 1; i < m_SapManager.GetServerCount(); i++)
|
||
|
|
{
|
||
|
|
// Does this server support "Acq" (frame-grabber) or "AcqDevice" (camera)?
|
||
|
|
char name[128];
|
||
|
|
char device[128];
|
||
|
|
if (m_SapManager.GetServerName(i, name, sizeof(name)))
|
||
|
|
{
|
||
|
|
qtServername = QString(QLatin1String(name));
|
||
|
|
qtDeviceList << qtServername;
|
||
|
|
//ResourceCount指当前采集卡/主板自带网口下的相机数
|
||
|
|
for (int j = 0; j < m_SapManager.GetResourceCount(name, SapManager::ResourceAcqDevice); j++)
|
||
|
|
{
|
||
|
|
//返回相机名给name
|
||
|
|
m_SapManager.GetResourceName(name, SapManager::ResourceAcqDevice, j, device, sizeof(device));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (qtDeviceList.length() <= 0)
|
||
|
|
{
|
||
|
|
qDebug() << "[Error] " << "Device List NULL";
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
Hd_DalsaCamera_module::Hd_DalsaCamera_module()
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
void Hd_DalsaCamera_module::CloseDevice()
|
||
|
|
{
|
||
|
|
bool success = m_Xfer->Freeze();
|
||
|
|
success = m_Xfer->Wait(5000);
|
||
|
|
getchar();
|
||
|
|
// Release resources for all objects
|
||
|
|
success = m_Xfer->Destroy();
|
||
|
|
success = m_View->Destroy();
|
||
|
|
success = m_Buffers->Destroy();
|
||
|
|
success = m_AcqDevice->Destroy();
|
||
|
|
ExceptionCB(-1, NULL);
|
||
|
|
}
|
||
|
|
Hd_DalsaCamera_module::~Hd_DalsaCamera_module()
|
||
|
|
{
|
||
|
|
CloseDevice();
|
||
|
|
qDebug() << "[INFO] " << " delete success!";
|
||
|
|
}
|
||
|
|
|
||
|
|
bool Hd_DalsaCamera_module::sendData(std::vector<cv::Mat> &mat,QByteArray &data)
|
||
|
|
{
|
||
|
|
if (data == "connect")
|
||
|
|
{
|
||
|
|
data = mv_Status;
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
if(srcImage.empty())
|
||
|
|
{
|
||
|
|
// qCritical() << __FUNCTION__ << " line:" <<__LINE__ << " srcImage is null";
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
if(m_flag)
|
||
|
|
mat.push_back(srcImage);
|
||
|
|
m_flag = false;
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
bool Hd_DalsaCamera_module::reciveData(std::vector<cv::Mat> &mat,QByteArray &data)
|
||
|
|
{
|
||
|
|
//qDebug() << "[info] "<< __FUNCTION__ << " line:" <<__LINE__ << " start"<<"-------------UserName:"<<data;
|
||
|
|
m_flag = false;
|
||
|
|
Username= data.toStdString();
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool Hd_DalsaCamera_module::initParas(QByteArray& byte)
|
||
|
|
{
|
||
|
|
qDebug() << "[INFO] " << " -------- init start";
|
||
|
|
getDalsaDeviceList();
|
||
|
|
string userName;
|
||
|
|
string behand = ".ccf";
|
||
|
|
string myname ;
|
||
|
|
|
||
|
|
if (Username=="camera8" )
|
||
|
|
{
|
||
|
|
userName = "Linea_M8192-7um_8";
|
||
|
|
myname = "camera8.ccf";
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
userName = "Linea_M8192-7um_2";
|
||
|
|
myname = "camera7.ccf";
|
||
|
|
}
|
||
|
|
//根据相机名称和同名的配置文件初始化相机
|
||
|
|
|
||
|
|
string cdeviceName = userName;
|
||
|
|
//cv::Mat hh()
|
||
|
|
//创建相机对象
|
||
|
|
SapXferCallbackInfo* pInfo;
|
||
|
|
m_AcqDevice = new SapAcqDevice(SapLocation(cdeviceName.c_str()), myname.c_str());
|
||
|
|
m_Buffers = new SapBuffer(15, m_AcqDevice);
|
||
|
|
m_View = new SapView(m_Buffers);
|
||
|
|
m_Xfer = new SapAcqDeviceToBuf(m_AcqDevice, m_Buffers, XferCallback, this);
|
||
|
|
|
||
|
|
bool success = m_AcqDevice->Create();
|
||
|
|
if(success!=true)
|
||
|
|
{
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
success = m_Buffers->Create();
|
||
|
|
if(success!=true)
|
||
|
|
{
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
success = m_View->Create();
|
||
|
|
if(success!=true)
|
||
|
|
{
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
success = m_Xfer->Create();
|
||
|
|
if(success!=true)
|
||
|
|
{
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
success=m_Xfer->Init();
|
||
|
|
if(success!=true)
|
||
|
|
{
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 硬触发
|
||
|
|
m_Xfer->Grab();
|
||
|
|
qDebug() << "[INFO] " << " -------- init end";
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool Hd_DalsaCamera_module::run()
|
||
|
|
{
|
||
|
|
qDebug() << "[INFO] " << " run start" <<QString::fromStdString(Username);
|
||
|
|
try
|
||
|
|
{
|
||
|
|
m_Xfer->Grab();
|
||
|
|
while (true)
|
||
|
|
{
|
||
|
|
if(m_flag)
|
||
|
|
{
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
Sleep(1);
|
||
|
|
}
|
||
|
|
m_flag = false;
|
||
|
|
}
|
||
|
|
catch (QString ev)
|
||
|
|
{
|
||
|
|
qDebug() << "[Error] " << " ev:" << ev ;
|
||
|
|
}
|
||
|
|
//qDebug() << "[info]"<< __FUNCTION__ << " line:" << __LINE__ << " run end" ;
|
||
|
|
return true;
|
||
|
|
}
|