#include "daheng_SingleCam_for_qt.h"
daheng_SingleCam_for_qt::daheng_SingleCam_for_qt(QWidget *parent)
: QMainWindow(parent)
, m_bIsOpen(false)
, m_bIsSnap(false)
{
ui.setupUi(this);
//控件初始化
m_pBtnOpenDevice = new QPushButton(this);
m_pBtnOpenDevice->setText("打开设备");
m_pBtnOpenDevice->move(0, 0);
m_pBtnCloseDevice = new QPushButton(this);
m_pBtnCloseDevice->setText("关闭设备");
m_pBtnCloseDevice->move(0, 50);
m_pBtnStartSnap = new QPushButton(this);
m_pBtnStartSnap->setText("开始采集");
m_pBtnStartSnap->move(0, 100);
m_pBtnStopSnap = new QPushButton(this);
m_pBtnStopSnap->setText("停止采集");
m_pBtnStopSnap->move(0, 150);
m_pBitmap = new CGXBitmap(this);
m_pBitmap->move(100, 0);
m_pBitmap->resize(800, 607);
m_pLabel = new QLabel(this);
m_pLabel->setText("欢迎使用大恒相机[for Qt]");
statusBar()->addWidget(m_pLabel);//将label控件添加到状态栏
//连接信号和槽函数
connect(m_pBtnOpenDevice, &QPushButton::clicked, this, &daheng_SingleCam_for_qt::on_Btn_OpenDevice_clicked);
connect(m_pBtnCloseDevice, &QPushButton::clicked, this, &daheng_SingleCam_for_qt::on_Btn_CloseDevice_clicked);
connect(m_pBtnStartSnap, &QPushButton::clicked, this, &daheng_SingleCam_for_qt::on_Btn_StartSnap_clicked);
connect(m_pBtnStopSnap, &QPushButton::clicked, this, &daheng_SingleCam_for_qt::on_Btn_StopSnap_clicked);
//大恒API库调用
try
{
//初始化设备
IGXFactory::GetInstance().Init();
m_pCaptureEventHandler = new CSampleCaptureEventHandler();
//更新界面
UpdateUI();
}
catch (CGalaxyException& e)
{
if (m_pCaptureEventHandler != NULL)
{
delete m_pCaptureEventHandler;
m_pCaptureEventHandler = NULL;
}
m_pLabel->setText(QString::fromStdString(e.what()));
return;
}
catch (std::exception& e)
{
if (m_pCaptureEventHandler != NULL)
{
delete m_pCaptureEventHandler;
m_pCaptureEventHandler = NULL;
}
m_pLabel->setText(QString::fromStdString(e.what()));
return;
}
}
//---------------------------------------------------------------------------------
/**
\brief 更新界面
\return 无
*/
//----------------------------------------------------------------------------------
void daheng_SingleCam_for_qt::UpdateUI()
{
m_pBtnOpenDevice->setEnabled(!m_bIsOpen);
m_pBtnCloseDevice->setEnabled(m_bIsOpen);
m_pBtnStartSnap->setEnabled(m_bIsOpen && !m_bIsSnap);
m_pBtnStopSnap->setEnabled(m_bIsOpen && m_bIsSnap);
}
void daheng_SingleCam_for_qt::on_Btn_OpenDevice_clicked()
{
bool bIsDeviceOpen = false; //设备是否打开标识
bool bIsStreamOpen = false; //设备流是否打开标识
try
{
//枚举设备
GxIAPICPP::gxdeviceinfo_vector vectorDeviceInfo;
IGXFactory::GetInstance().UpdateDeviceList(1000, vectorDeviceInfo);
if (vectorDeviceInfo.size() == 0)
{
m_bIsOpen = false;
bIsDeviceOpen = false;
m_pLabel->setText("无可用设备!");
return;
}
//打开第一个设备以及设备下面第一个流
m_ObjDevicePtr = IGXFactory::GetInstance().OpenDeviceBySN(vectorDeviceInfo[0].GetSN(), GX_ACCESS_EXCLUSIVE);//打开方式:独占
m_bIsOpen = true;
bIsDeviceOpen = true;
//获取远端设备属性控制器
m_ObjFeatureControlPtr = m_ObjDevicePtr->GetRemoteFeatureControl();
//为画图对象分配设备指针
m_pBitmap->addDevicePtr(m_ObjDevicePtr);
//判断设备流是否大于零,如果大于零则打开流
int nStreamCount = m_ObjDevicePtr->GetStreamCount();
if (nStreamCount)
{
m_ObjStreamPtr = m_ObjDevicePtr->OpenStream(0);
//获取流层属性控制器
m_ObjStreamFeatureControlPtr = m_ObjStreamPtr->GetFeatureControl();
bIsStreamOpen = true;
}
else
{
throw exception("未发现设备流!");
}
//提高网络相机的采集性能,设置方法如下(目前只有千兆网系列相机支持设置最优包长)
GX_DEVICE_CLASS_LIST ObjDeviceClass =
m_ObjDevicePtr->GetDeviceInfo().GetDeviceClass();
if (ObjDeviceClass == GX_DEVICE_CLASS_GEV)
{
//判断设备是否支持流通道数据包功能
if (m_ObjStreamFeatureControlPtr->IsImplemented("GevSCPSPacketSize"))
{
//获取当前网络环境的最优包长值
int nPacketSize = m_ObjStreamPtr->GetOptimalPacketSize();
//将最优包长值设置为当前设备的流通道包长值
m_ObjFeatureControlPtr->GetIntFeature(
"GevSCPSPacketSize")->SetValue(nPacketSize);
}
}
//更新界面
UpdateUI();
m_pLabel->setText(QString("打开设备成功!"));
}
catch (CGalaxyException& e)
{
//判断设备流是否已打开
if (bIsStreamOpen)
{
m_ObjStreamPtr->Close();
}
//判断设备是否已打开
if (bIsDeviceOpen)
{
m_ObjDevicePtr->Close();
}
m_pLabel->setText(QString::fromStdString(e.what()));
return;
}
catch (exception& e)
{
//判断设备流是否已打开
if (bIsStreamOpen)
{
m_ObjStreamPtr->Close();
}
//判断设备是否已打开
if (bIsDeviceOpen)
{
m_ObjDevicePtr->Close();
}
m_pLabel->setText(QString::fromStdString(e.what()));
return;
}
}
void daheng_SingleCam_for_qt::on_Btn_CloseDevice_clicked()
{
try
{
//判断是否已停止采集
if (m_bIsSnap)
{
//发送停采命令
m_ObjFeatureControlPtr->GetCommandFeature("AcquisitionStop")->Execute();
//关闭流层采集
m_ObjStreamPtr->StopGrab();
//注销采集回调
m_ObjStreamPtr->UnregisterCaptureCallback();
}
}
catch (CGalaxyException)
{
//do noting
}
try
{
//关闭流对象
m_ObjStreamPtr->Close();
}
catch (CGalaxyException)
{
//do noting
}
try
{
//关闭设备
m_ObjDevicePtr->Close();
m_pLabel->setText(QString("关闭设备成功!"));
}
catch (CGalaxyException)
{
//do noting
}
m_bIsOpen = false;
m_bIsSnap = false;
//更新界面
UpdateUI();
}
void daheng_SingleCam_for_qt::on_Btn_StartSnap_clicked()
{
// TODO: Add your control notification handler code here
try
{
try
{
//设置Buffer处理模式
m_ObjStreamFeatureControlPtr->GetEnumFeature("StreamBufferHandlingMode")->SetValue("OldestFirst");
}
catch (...)
{
}
//注册回调函数
m_ObjStreamPtr->RegisterCaptureCallback(m_pCaptureEventHandler, this);
//开启流层通道
m_ObjStreamPtr->StartGrab();
//发送开采命令
m_ObjFeatureControlPtr->GetCommandFeature("AcquisitionStart")->Execute();
m_bIsSnap = true;
//更新界面
UpdateUI();
m_pLabel->setText(QString("开始采集!"));
}
catch (CGalaxyException& e)
{
m_pLabel->setText(QString::fromStdString(e.what()));
return;
}
catch (std::exception& e)
{
m_pLabel->setText(QString::fromStdString(e.what()));
return;
}
}
void daheng_SingleCam_for_qt::on_Btn_StopSnap_clicked()
{
// TODO: Add your control notification handler code here
try
{
//发送停采命令
m_ObjFeatureControlPtr->GetCommandFeature("AcquisitionStop")->Execute();
//关闭流层通道
m_ObjStreamPtr->StopGrab();
//注销采集回调
m_ObjStreamPtr->UnregisterCaptureCallback();
m_bIsSnap = false;
//更新界面
UpdateUI();
m_pLabel->setText(QString("停止采集!"));
}
catch (CGalaxyException& e)
{
m_pLabel->setText(QString::fromStdString(e.what()));
return;
}
catch (std::exception& e)
{
m_pLabel->setText(QString::fromStdString(e.what()));
return;
}
}
void daheng_SingleCam_for_qt::closeEvent(QCloseEvent *event)
{
try
{
//判断是否停止采集
if (m_bIsSnap)
{
//发送停采命令
m_ObjFeatureControlPtr->GetCommandFeature("AcquisitionStop")->Execute();
//关闭流层通道
m_ObjStreamPtr->StopGrab();
//注销采集回调
m_ObjStreamPtr->UnregisterCaptureCallback();
m_bIsSnap = false;
}
}
catch (CGalaxyException& e)
{
}
catch (exception& e)
{
}
try
{
//判断是否关闭设备
if (m_bIsOpen)
{
//关闭流对象
m_ObjStreamPtr->Close();
//关闭设备
m_ObjDevicePtr->Close();
m_bIsOpen = false;
}
}
catch (CGalaxyException& e)
{
}
catch (exception& e)
{
}
try
{
//释放设备资源
IGXFactory::GetInstance().Uninit();
}
catch (CGalaxyException& e)
{
}
catch (exception& e)
{
}
if (m_pCaptureEventHandler != NULL)
{
delete m_pCaptureEventHandler;
m_pCaptureEventHandler = NULL;
}
event->accept();
}