#include "StdAfx.h"
|
#include "CameraControl_Pylon.h"
|
#include "CHCameraControls/CameraBuffer.h"
|
|
CCameraControl_Pylon::CCameraControl_Pylon(int nViewIndex) : CCameraControl(nViewIndex)
|
{
|
m_nSnapFrameCount = 0;
|
m_pCamera = NULL;
|
m_bConnected = FALSE;
|
m_Converter.OutputPixelFormat = PixelType_BGR8packed; // color convert
|
}
|
|
|
CCameraControl_Pylon::~CCameraControl_Pylon(void)
|
{
|
//Disconnect();
|
|
//if (m_pCamera!=NULL)
|
//{
|
// delete m_pCamera;
|
// m_pCamera = NULL;
|
//}
|
}
|
|
int CCameraControl_Pylon::Connect(const CCameraControlInfo& controlInfo)
|
{
|
if (m_pCamera != NULL) return 0;
|
if (m_bConnected) return 0;
|
|
m_nTotalIndex = controlInfo.GetIndex();
|
m_nGrabCount = controlInfo.GetFrameCount();
|
m_nFrameCount = controlInfo.GetFrameCount();
|
CString strTemp = controlInfo.GetConnectionPort();
|
char * str_serialNum = NULL;
|
|
#ifdef _UNICODE
|
int length = WideCharToMultiByte(CP_ACP, 0, strTemp.GetBuffer(), -1, NULL, 0, NULL, NULL);
|
str_serialNum = new char[length];
|
WideCharToMultiByte(CP_ACP, 0, strTemp.GetBuffer(), -1, str_serialNum, length, NULL, NULL);
|
#else
|
int length = strTemp.GetLength();
|
str_serialNum = new char[length + 1];
|
memcpy(str_serialNum, strTemp.GetBuffer(), sizeof(char)*length);
|
str_serialNum[length] = NULL;
|
#endif
|
|
if (str_serialNum == NULL) return 0;
|
|
Pylon::CBaslerGigEDeviceInfo di;
|
//di.SetIpAddress(str_serialNum); // ip
|
di.SetSerialNumber(str_serialNum); // sn
|
|
delete[] str_serialNum;
|
|
try
|
{
|
CTlFactory& tlFactory = CTlFactory::GetInstance();
|
IPylonDevice *pDevice = tlFactory.CreateDevice(di);
|
|
m_pCamera = new Camera_t(pDevice);
|
|
if (m_pCamera)
|
{
|
//SetHeartbeatTimeout(10000); // set heartbeat
|
//if (controlInfo.GetCameraReverseX() == TRUE)//210614���Ϸи� ������ ��� ����
|
//{
|
// m_pCamera->ReverseX = TRUE;
|
//}
|
//else
|
//{
|
// m_pCamera->ReverseX = FALSE;
|
//}
|
|
m_pCamera->SetCameraContext(m_nTotalIndex);///m_nCameraIndex); // set context
|
|
m_pCamera->MaxNumBuffer = m_nGrabCount;
|
m_pCamera->RegisterImageEventHandler(this, RegistrationMode_Append, Ownership_TakeOwnership);
|
m_pCamera->RegisterConfiguration(this, RegistrationMode_ReplaceAll, Cleanup_None);
|
m_pCamera->Open();
|
|
// set buffer info
|
m_nWidth = (int)m_pCamera->Width.GetValue();
|
m_nWidthStep = m_nWidth * 3;
|
m_nHeight = (int)m_pCamera->Height.GetValue();
|
m_nChannels = 3;
|
m_nDepth = 8;
|
|
CString strTemp;
|
strTemp.Format(_T("Pylon_%02d"), m_nTotalIndex);
|
AllocateFrameBuffer(m_nWidth, m_nHeight, m_nChannels, m_nFrameCount, strTemp);
|
|
SetConnected(TRUE);
|
return m_pCamera->IsOpen();
|
}
|
}
|
catch (...)
|
{
|
Disconnect();
|
}
|
|
return 0;
|
}
|
|
int CCameraControl_Pylon::Grab()
|
{
|
if (m_pCamera == NULL) return 0;
|
|
if (!m_pCamera->IsOpen()) return -1;
|
|
if (m_pCamera->IsGrabbing()) return -2;
|
|
try
|
{
|
m_pCamera->StartGrabbing(GrabStrategy_OneByOne, GrabLoop_ProvidedByInstantCamera);
|
}
|
catch (...)
|
{
|
Disconnect();
|
}
|
|
return 1;
|
}
|
|
int CCameraControl_Pylon::Snap()
|
{
|
if (m_pCamera == NULL) return 0;
|
|
if (!m_pCamera->IsOpen()) return -1;
|
|
if (m_pCamera->IsGrabbing()) return -2;
|
|
//m_pCamera->StartGrabbing(m_nFrameCount, GrabStrategy_OneByOne, GrabLoop_ProvidedByInstantCamera);
|
try
|
{
|
m_pCamera->StartGrabbing(m_nFrameCount, GrabStrategy_OneByOne, GrabLoop_ProvidedByInstantCamera);
|
return 1;
|
}
|
catch (GenICam::GenericException ex)
|
{
|
TRACE(_T("Error <CGigECamera::Grab> %s"), ex.GetDescription());
|
}
|
catch (...)//catch (CException* e)
|
{
|
TRACE(_T("Error <CGigECamera::Grab> failed : StartGrabbing"));
|
}
|
|
return 1;
|
}
|
|
int CCameraControl_Pylon::Snap(int nCount)
|
{
|
if (m_pCamera == NULL) return 0;
|
|
if (!m_pCamera->IsOpen()) return -1;
|
|
if (m_pCamera->IsGrabbing()) return -2;
|
|
//m_pCamera->StartGrabbing(nCount, GrabStrategy_OneByOne, GrabLoop_ProvidedByInstantCamera);
|
try
|
{
|
m_pCamera->StartGrabbing(nCount, GrabStrategy_OneByOne, GrabLoop_ProvidedByInstantCamera);
|
return 1;
|
}
|
catch (GenICam::GenericException ex)
|
{
|
TRACE(_T("Error <CGigECamera::Grab> %s"), ex.GetDescription());
|
}
|
catch (...)//catch (CException* e)
|
{
|
TRACE(_T("Error <CGigECamera::Grab> failed : StartGrabbing"));
|
}
|
|
return 1;
|
}
|
|
int CCameraControl_Pylon::Snap(int nCount, UINT nWaitTime)
|
{
|
if (m_pCamera == NULL) return 0;
|
|
if (!m_pCamera->IsOpen()) return -1;
|
|
if (m_pCamera->IsGrabbing()) return -2;
|
|
//m_pCamera->StartGrabbing(nCount, GrabStrategy_OneByOne, GrabLoop_ProvidedByInstantCamera);
|
|
try
|
{
|
m_pCamera->StartGrabbing(nCount, GrabStrategy_OneByOne, GrabLoop_ProvidedByInstantCamera);
|
return 1;
|
}
|
catch (GenICam::GenericException ex)
|
{
|
TRACE(_T("Error <CGigECamera::Grab> %s"), ex.GetDescription());
|
}
|
catch (...)//catch (CException* e)
|
{
|
TRACE(_T("Error <CGigECamera::Grab> failed : StartGrabbing"));
|
}
|
return 1;
|
}
|
|
int CCameraControl_Pylon::Freeze()
|
{
|
if (m_pCamera == NULL) return 0;
|
|
if (!m_pCamera->IsOpen()) return -1;
|
|
if (!m_pCamera->IsGrabbing()) return -2;
|
|
//m_pCamera->StopGrabbing();
|
//Sleep(500);
|
try
|
{
|
if (GetGrabbing())
|
{
|
m_pCamera->StopGrabbing();
|
}
|
}
|
catch (...)
|
{
|
TRACE(_T("Error <CGigECamera::Stop> failed : StopGrabbing"));
|
}
|
|
return 1;
|
}
|
|
int CCameraControl_Pylon::Abort()
|
{
|
int ret = -1;
|
|
ret = Freeze();
|
|
return ret;
|
}
|
|
int CCameraControl_Pylon::Disconnect()
|
{
|
if (m_pCamera == NULL) return -1;
|
|
if (!m_pCamera->IsOpen()) return -1;
|
|
if (m_pCamera->IsGrabbing())
|
{
|
m_pCamera->StopGrabbing();
|
//Sleep(10);
|
Sleep(500);
|
}
|
m_pCamera->Close();
|
|
//m_pCamera->DetachDevice();
|
m_pCamera->DestroyDevice();
|
|
return 1;
|
}
|
|
BOOL CCameraControl_Pylon::SetExposureTime(double dValue)
|
{
|
if (m_pCamera == NULL) return FALSE;
|
|
m_pCamera->ExposureTimeRaw.SetValue(dValue);
|
|
return TRUE;
|
}
|
|
BOOL CCameraControl_Pylon::GetExposureTime(double& dValue)
|
{
|
if (m_pCamera == NULL) return FALSE;
|
|
dValue = m_pCamera->ExposureTimeRaw.GetValue();
|
|
return TRUE;
|
}
|
|
BOOL CCameraControl_Pylon::SetExposureAuto(BOOL bAuto)
|
{
|
return FALSE;
|
}
|
|
void CCameraControl_Pylon::SetTriggerMode(int nMode, int nCount/*=1*/)
|
{
|
if (nMode == ModeExternal)
|
{
|
m_pCamera->TriggerMode.SetValue(Basler_GigECamera::TriggerMode_On);
|
}
|
else if (nMode = ModeInternal)
|
{
|
m_pCamera->TriggerMode.SetValue(Basler_GigECamera::TriggerMode_Off);
|
}
|
else
|
{
|
;
|
}
|
}
|
|
BOOL CCameraControl_Pylon::GetConnected() const
|
{
|
if (m_pCamera == NULL) return FALSE;
|
|
return m_pCamera->IsOpen();
|
}
|
|
BOOL CCameraControl_Pylon::GetGrabbing() const
|
{
|
if (m_pCamera == NULL) return FALSE;
|
|
if (!m_pCamera->IsOpen()) return FALSE;
|
|
return m_pCamera->IsGrabbing();
|
}
|
|
void CCameraControl_Pylon::SetHeartbeatTimeout(UINT nTimeout)
|
{
|
char strTemp[100];
|
sprintf_s(strTemp, "PYLON_GIGE_HEARTBEAT=%d", nTimeout);
|
putenv(strTemp);
|
}
|
|
void CCameraControl_Pylon::ImageCallbackFunc(const CGrabResultPtr& ptrGrabResult)
|
{
|
if (m_pCameraFrameBuffer == NULL) return;
|
|
m_nFrameIndex = (++m_nFrameIndex) % m_nFrameCount;
|
int nScanIndex = m_nScanIndex % MAX_BUFFER_SCAN_COUNT;
|
BYTE *pFrameBuffData = m_pCameraFrameBuffer->GetBufferData(nScanIndex, m_nFrameIndex);
|
BYTE *pBuffData = m_pCameraFrameBuffer->GetBufferData(nScanIndex, m_nFrameIndex);
|
|
m_Converter.Convert(pFrameBuffData, m_curFrameImage.GetImageSize(), ptrGrabResult);
|
|
|
if (m_pCC2P)
|
{
|
m_pCC2P->ICC2P_FrameCaptured(m_nTotalIndex, m_nFrameIndex, m_nFrameCount);// 2014.6.21 m_pCameraFrameBuffer �߰�
|
}
|
}
|
|
void CCameraControl_Pylon::OnImageGrabbed(CInstantCamera& camera, const CGrabResultPtr& ptrGrabResult)
|
{
|
this->ImageCallbackFunc(ptrGrabResult);
|
}
|