#include "StdAfx.h"
|
#include "CameraControl_FlyCapture.h"
|
#include "CHCameraControls/CameraBuffer.h"
|
|
using namespace FlyCapture2;
|
|
CCameraControl_FlyCapture::CCameraControl_FlyCapture(int nViewIndex) : CCameraControl(nViewIndex)
|
{
|
m_pCamera = new FlyCapture2::Camera;
|
}
|
|
CCameraControl_FlyCapture::~CCameraControl_FlyCapture(void)
|
{
|
Disconnect();
|
|
if (m_pCamera)
|
{
|
delete m_pCamera;
|
m_pCamera = NULL;
|
}
|
}
|
|
int CCameraControl_FlyCapture::Connect(const CCameraControlInfo& controlInfo)
|
{
|
if (m_bConnected) return -4;
|
|
m_bConnected = FALSE;
|
|
m_nTotalIndex = controlInfo.GetIndex();
|
m_nFrameCount = controlInfo.GetFrameCount();
|
|
Error error;
|
|
BusManager busMgr;
|
unsigned int numCameras;
|
error = busMgr.GetNumOfCameras(&numCameras);
|
if (error!=PGRERROR_OK) return -3;
|
|
PGRGuid guid;
|
error = busMgr.GetCameraFromSerialNumber(_ttoi(controlInfo.GetConnectionPort()), &guid);
|
|
error = m_pCamera->Connect(&guid);
|
if (error!=PGRERROR_OK) return 0;
|
|
error = m_pCamera->StartCapture();
|
if (error==PGRERROR_OK)
|
{
|
// Get image info
|
Image tempImage;
|
error = m_pCamera->RetrieveBuffer(&tempImage);
|
m_pCamera->StopCapture();
|
|
if (error==PGRERROR_OK)
|
{
|
PixelFormat image_format = tempImage.GetPixelFormat();
|
|
m_nWidth = tempImage.GetCols();
|
m_nHeight = tempImage.GetRows();
|
m_nWidthStep = m_nWidth + (m_nWidth % 4);
|
|
switch(image_format)
|
{
|
case PIXEL_FORMAT_MONO8:// = 0x80000000, < 8 bits of mono information.
|
m_nChannels = 1;
|
break;
|
case PIXEL_FORMAT_BGR:// = 0x80000008, < 24 bit BGR.
|
m_nChannels = 3;
|
break;
|
case PIXEL_FORMAT_BGRU:// = 0x40000008, < 32 bit BGRU.
|
m_nChannels = 4;
|
break;
|
case PIXEL_FORMAT_RGB:// = PIXEL_FORMAT_RGB8, < 24 bit RGB.
|
m_nChannels = 3;
|
break;
|
case PIXEL_FORMAT_RGBU:// = 0x40000002, < 32 bit RGBU.
|
m_nChannels = 4;
|
break;
|
}
|
|
|
// alloc frame buffer
|
CString temp;
|
temp.Format(_T("FlyCapture_%02d"), m_nTotalIndex);
|
AllocateFrameBuffer(m_nWidth, m_nHeight, m_nChannels, m_nFrameCount, temp);
|
}
|
}
|
|
m_bConnected = TRUE;
|
|
return 1;
|
}
|
|
int CCameraControl_FlyCapture::Grab()
|
{
|
if (!m_pCamera->IsConnected()) return -1;
|
|
if (m_bGrabbing) return TRUE;
|
|
m_nFrameIndex = 0;
|
Error error = m_pCamera->StartCapture(CaptureCallbackFunc, this);
|
if (error!=PGRERROR_OK) return 0;
|
|
m_bGrabbing = TRUE;
|
|
return 1;
|
}
|
|
int CCameraControl_FlyCapture::Snap()
|
{
|
if (!m_pCamera->IsConnected()) return -4;
|
|
if (m_bGrabbing) return -3;
|
|
Error error = m_pCamera->StartCapture();
|
if (error!=PGRERROR_OK) return -2;
|
|
m_bGrabbing = TRUE;
|
|
Image rawImage;
|
error = m_pCamera->RetrieveBuffer( &rawImage );
|
if (error!=PGRERROR_OK) return -1;
|
|
m_nFrameIndex = 0;
|
CaptureCallback(&rawImage);
|
|
error = m_pCamera->StopCapture();
|
if (error!=PGRERROR_OK) return 0;
|
|
m_bGrabbing = FALSE;
|
|
return 1;
|
}
|
|
int CCameraControl_FlyCapture::Snap(int nCount)
|
{
|
if (!m_pCamera->IsConnected()) return -2;
|
|
if (m_bGrabbing) return -1;
|
|
m_nFrameIndex = 0;
|
|
Error error = m_pCamera->StartCapture(CaptureCallbackFunc, this);
|
if (error!=PGRERROR_OK) return 0;
|
|
m_bGrabbing = TRUE;
|
|
return 1;
|
}
|
|
int CCameraControl_FlyCapture::Snap(int nCount, UINT nWaitTime)
|
{
|
if (!m_pCamera->IsConnected()) return -2;
|
|
if (m_bGrabbing) return -1;
|
|
m_nFrameIndex = 0;
|
|
Error error = m_pCamera->StartCapture(CaptureCallbackFunc, this);
|
if (error!=PGRERROR_OK) return 0;
|
|
m_bGrabbing = TRUE;
|
|
return 1;
|
}
|
|
int CCameraControl_FlyCapture::Freeze()
|
{
|
if (!m_pCamera->IsConnected()) return 0;
|
|
if (!m_bGrabbing) return 0;
|
|
m_pCamera->StopCapture();
|
|
m_bGrabbing = FALSE;
|
|
return 1;
|
}
|
|
int CCameraControl_FlyCapture::Abort()
|
{
|
if (!m_pCamera->IsConnected()) return -1;
|
|
if (!m_bGrabbing) return -2;
|
|
m_pCamera->StopCapture();
|
|
m_bGrabbing = FALSE;
|
|
return 1;
|
}
|
|
int CCameraControl_FlyCapture::Disconnect()
|
{
|
if (!m_pCamera->IsConnected()) return -1;
|
|
Abort();
|
|
m_pCamera->Disconnect();
|
|
return 1;
|
}
|
|
void CCameraControl_FlyCapture::CaptureCallbackFunc(FlyCapture2::Image *pImage, const void *pCallbackData)
|
{
|
CCameraControl_FlyCapture* pParam = (CCameraControl_FlyCapture*) pCallbackData;
|
|
if (pCallbackData==NULL) return;
|
|
pParam->CaptureCallback(pImage);
|
|
return;
|
}
|
|
void CCameraControl_FlyCapture::CaptureCallback(FlyCapture2::Image *pImage)
|
{
|
if (pImage==NULL || m_pCameraFrameBuffer==NULL) return;
|
|
int nFrameIndex = (++m_nFrameIndex+1) % m_nFrameCount;
|
|
//m_nFrameIndex = (++m_nFrameIndex) % m_nFrameCount;
|
|
int nScanIndex = m_nScanIndex % MAX_BUFFER_SCAN_COUNT;
|
//BYTE *pFrameBuffData = m_pCameraFrameBuffer->GetBufferData(nScanIndex, m_nFrameIndex);
|
BYTE *pFrameBuffData = m_pCameraFrameBuffer->GetBufferData(nScanIndex, nFrameIndex);
|
if (pFrameBuffData==NULL) return;
|
|
if (m_nFlipMode!=FlipNone)
|
{
|
if (ImageFlip(m_nFlipMode, pImage->GetData(), pFrameBuffData, m_nWidth, m_nHeight, m_nChannels)==FALSE)
|
{
|
memcpy(pFrameBuffData, pImage->GetData(), m_curFrameImage.GetImageSize()); // ½ÇÆÐ½Ã, ±×³É º¹»ç
|
}
|
}
|
else
|
{
|
memcpy(pFrameBuffData, pImage->GetData(), m_curFrameImage.GetImageSize());
|
}
|
|
|
if (m_pCC2P)
|
{
|
m_pCC2P->ICC2P_FrameCaptured(m_nIndex, nFrameIndex, m_nFrameCount);// 2014.6.21 m_pCameraFrameBuffer Ãß°¡
|
}
|
}
|
|
void CCameraControl_FlyCapture::SetTriggerMode(int nMode)
|
{
|
m_nTriggerMode = nMode;
|
}
|
|
BOOL CCameraControl_FlyCapture::SetExposureTime(double dValue)
|
{
|
if (!m_pCamera->IsConnected()) return FALSE;
|
|
// Ä«¸Þ¶ó ÆÄ¶ó¸ÞÅÍ(¼ÅÅÍ) ȹµæ
|
Property prop;
|
prop.type=SHUTTER;
|
Error error = m_pCamera->GetProperty(&prop);
|
|
if (error!=PGRERROR_OK) return FALSE;
|
|
// ¼ÅÅÍ ¼³Á¤ ¹æ¹ý (Àý´ë°ªÀ¸·Î ¼³Á¤)
|
prop.absControl = true; // Àý´ë°ªÀ¸·Î ¼³Á¤
|
prop.absValue = float(dValue) / 1000.f; // °ª
|
prop.autoManualMode = false;
|
error = m_pCamera->SetProperty(&prop);
|
|
if (error!=PGRERROR_OK) return FALSE;
|
|
return TRUE;
|
|
}
|
|
BOOL CCameraControl_FlyCapture::GetExposureTime(double& dValue)
|
{
|
if (!m_pCamera->IsConnected()) return FALSE;
|
|
// Ä«¸Þ¶ó ÆÄ¶ó¸ÞÅÍ(¼ÅÅÍ) ȹµæ
|
Property prop;
|
prop.type=SHUTTER;
|
Error error = m_pCamera->GetProperty(&prop);
|
|
if (error!=PGRERROR_OK) return FALSE;
|
|
dValue = double(prop.absValue*1000.0f);
|
|
return TRUE;
|
}
|
|
BOOL CCameraControl_FlyCapture::SetExposureAuto(BOOL bAuto)
|
{
|
if (!m_pCamera->IsConnected()) return FALSE;
|
|
// Ä«¸Þ¶ó ÆÄ¶ó¸ÞÅÍ(¼ÅÅÍ) ȹµæ
|
Property prop;
|
prop.type=SHUTTER;
|
Error error = m_pCamera->GetProperty(&prop);
|
|
if (error!=PGRERROR_OK) return FALSE;
|
|
// ¼ÅÅÍ ¼³Á¤ ¹æ¹ý (Àý´ë°ªÀ¸·Î ¼³Á¤)
|
prop.absControl = true; // Àý´ë°ªÀ¸·Î ¼³Á¤
|
prop.autoManualMode = (bool)bAuto;
|
error = m_pCamera->SetProperty(&prop);
|
|
if (error!=PGRERROR_OK) return FALSE;
|
|
return TRUE;
|
|
|
FRAME_RATE;
|
}
|
|
BOOL CCameraControl_FlyCapture::SetFrameRate( double dRate )
|
{
|
if (!m_pCamera->IsConnected()) return FALSE;
|
|
Property prop;
|
prop.type=FRAME_RATE;
|
Error error = m_pCamera->GetProperty(&prop);
|
|
if (error!=PGRERROR_OK) return FALSE;
|
|
prop.absControl = true;
|
prop.absValue = float(dRate);
|
prop.autoManualMode = false;
|
error = m_pCamera->SetProperty(&prop);
|
|
if (error!=PGRERROR_OK) return FALSE;
|
|
return TRUE;
|
}
|
|
BOOL CCameraControl_FlyCapture::DisconnectCheck()
|
{
|
// Connect »óÅ ȮÀÎÇØº¸°í
|
if (!m_pCamera->IsConnected()) return FALSE;
|
|
// ¼ÅÅͽºÇǵ带 È®ÀÎÇÏ¸é¼ Ä«¸Þ¶ó Connect ¿©ºÎ¸¦ Ã¼Å©ÇØº»´Ù
|
Property prop;
|
prop.type=SHUTTER;
|
Error error = m_pCamera->GetProperty(&prop);
|
|
// ¿¬°áÀÌ ¾ÈµÇ¾î üũ°¡ ºÒ°¡´ÉÇÑ °æ¿ì Fail ó¸®
|
if (error==PGRERROR_NOT_CONNECTED) return FALSE;
|
|
return TRUE;
|
}
|