Basler相机MFC实例
时间: 2023-08-22 11:10:14 浏览: 175
basler相机的MFC框架demo
5星 · 资源好评率100%
好的,这是一个基于 MFC 的 Basler 相机应用程序的示例:
```c++
#include <pylon/PylonIncludes.h>
#include <pylon/gige/BaslerGigEInstantCamera.h>
#include <pylon/usb/BaslerUsbInstantCamera.h>
#include <pylon/ImageFormatConverter.h>
#include <pylon/opencv/OpenCVImage.h>
#include <pylon/Utils.h>
#include <opencv2/opencv.hpp>
using namespace Pylon;
using namespace cv;
class CBaslerCamera
{
public:
CBaslerCamera(HWND hWndDisplay);
virtual ~CBaslerCamera();
bool Open();
void Close();
bool Start();
void Stop();
bool IsOpen();
bool IsRunning();
void OnTimer();
void OnPaint();
protected:
void EnumerateCameras();
void GetDeviceInfo();
bool ConfigureCamera();
bool GrabLoop();
private:
HWND m_hWndDisplay;
CImageFormatConverter m_FormatConverter;
CPylonImage m_PylonImage;
CDeviceInfo m_DeviceInfo;
CGrabResultPtr m_PylonGrabResult;
CInstantCamera* m_pCamera;
bool m_bIsOpen;
bool m_bIsRunning;
};
CBaslerCamera::CBaslerCamera(HWND hWndDisplay)
: m_hWndDisplay(hWndDisplay)
, m_pCamera(NULL)
, m_bIsOpen(false)
, m_bIsRunning(false)
{
PylonInitialize();
}
CBaslerCamera::~CBaslerCamera()
{
PylonTerminate();
}
bool CBaslerCamera::Open()
{
if (m_bIsOpen) return true;
EnumerateCameras();
if (m_DeviceInfo.GetDeviceClass() == BaslerGigEDeviceClass)
{
m_pCamera = new CBaslerGigEInstantCamera(CTlFactory::GetInstance().CreateDevice(m_DeviceInfo));
}
else if (m_DeviceInfo.GetDeviceClass() == BaslerUsbDeviceClass)
{
m_pCamera = new CBaslerUsbInstantCamera(CTlFactory::GetInstance().CreateDevice(m_DeviceInfo));
}
else
{
return false;
}
GetDeviceInfo();
if (!ConfigureCamera())
{
delete m_pCamera;
m_pCamera = NULL;
return false;
}
m_bIsOpen = true;
return true;
}
void CBaslerCamera::Close()
{
if (!m_bIsOpen) return;
Stop();
if (m_pCamera)
{
m_pCamera->Close();
delete m_pCamera;
m_pCamera = NULL;
}
m_bIsOpen = false;
}
bool CBaslerCamera::Start()
{
if (!m_bIsOpen || m_bIsRunning) return false;
try
{
m_pCamera->StartGrabbing();
}
catch (const GenericException& e)
{
CString strMsg;
strMsg.Format(_T("Failed to start grabbing: %hs"), e.what());
AfxMessageBox(strMsg, MB_ICONERROR);
return false;
}
m_bIsRunning = true;
SetTimer(m_hWndDisplay, 1, 30, NULL);
return true;
}
void CBaslerCamera::Stop()
{
if (!m_bIsRunning) return;
KillTimer(m_hWndDisplay, 1);
try
{
m_pCamera->StopGrabbing();
}
catch (const GenericException& e)
{
CString strMsg;
strMsg.Format(_T("Failed to stop grabbing: %hs"), e.what());
AfxMessageBox(strMsg, MB_ICONERROR);
}
m_bIsRunning = false;
}
bool CBaslerCamera::IsOpen()
{
return m_bIsOpen;
}
bool CBaslerCamera::IsRunning()
{
return m_bIsRunning;
}
void CBaslerCamera::OnTimer()
{
if (!GrabLoop())
{
Stop();
}
}
void CBaslerCamera::OnPaint()
{
if (!m_bIsOpen || !m_bIsRunning) return;
CPaintDC dc(CWnd::FromHandle(m_hWndDisplay));
CRect rcClient;
GetClientRect(m_hWndDisplay, &rcClient);
if (m_PylonGrabResult.IsValid())
{
m_FormatConverter.Convert(m_PylonImage, m_PylonGrabResult);
Mat matImage = Mat(m_PylonImage.GetHeight(), m_PylonImage.GetWidth(), CV_8UC3, m_PylonImage.GetBuffer());
cvtColor(matImage, matImage, CV_RGB2BGR);
IplImage img = matImage;
cvShowImage("Display", &img);
}
}
void CBaslerCamera::EnumerateCameras()
{
CDeviceInfo info;
CTlFactory& tlFactory = CTlFactory::GetInstance();
DeviceInfoList_t lstDevices;
tlFactory.EnumerateDevices(lstDevices);
if (lstDevices.empty())
{
AfxMessageBox(_T("No camera present"), MB_ICONERROR);
return;
}
if (lstDevices.size() == 1)
{
m_DeviceInfo = lstDevices[0];
return;
}
CArray<CString, CString&> arrNames;
for (DeviceInfoList_t::iterator it = lstDevices.begin(); it != lstDevices.end(); ++it)
{
info.SetDeviceClass(it->GetDeviceClass());
info.SetSerialNumber(it->GetSerialNumber());
info.SetUserDefinedName(it->GetUserDefinedName());
CString strName = CString(info.GetFriendlyName().c_str());
arrNames.Add(strName);
}
CComboBox dlgComboBox(_T("Select Camera"), _T("Select a camera to use:"), arrNames, m_hWndDisplay);
if (dlgComboBox.DoModal() == IDOK)
{
m_DeviceInfo = lstDevices[dlgComboBox.GetSelectedIndex()];
}
}
void CBaslerCamera::GetDeviceInfo()
{
CString strMsg;
strMsg.Format(_T("Device Name: %ls, Serial Number: %ls"), m_DeviceInfo.GetFriendlyName().c_str(), m_DeviceInfo.GetSerialNumber().c_str());
AfxMessageBox(strMsg, MB_ICONINFORMATION);
}
bool CBaslerCamera::ConfigureCamera()
{
try
{
m_pCamera->Open();
}
catch (const GenericException& e)
{
CString strMsg;
strMsg.Format(_T("Failed to open camera: %hs"), e.what());
AfxMessageBox(strMsg, MB_ICONERROR);
return false;
}
if (m_pCamera->IsGigE())
{
CInstantCameraGigE& camera = dynamic_cast<CInstantCameraGigE&>(*m_pCamera);
camera.GevSCPSPacketSize.SetValue(1500);
}
if (m_pCamera->IsUsb())
{
CInstantCameraUsb& camera = dynamic_cast<CInstantCameraUsb&>(*m_pCamera);
camera.Granularity.SetValue(Granularity_Good);
}
m_pCamera->PixelFormat.SetValue(PixelFormat_BGR8);
m_pCamera->AcquisitionFrameRateEnable.SetValue(true);
m_pCamera->AcquisitionFrameRateAbs.SetValue(30);
m_pCamera->TriggerMode.SetValue(TriggerMode_Off);
m_pCamera->ExposureAuto.SetValue(ExposureAuto_Off);
m_pCamera->ExposureTimeAbs.SetValue(20000);
m_pCamera->GainAuto.SetValue(GainAuto_Off);
m_pCamera->GainRaw.SetValue(0);
return true;
}
bool CBaslerCamera::GrabLoop()
{
if (!m_pCamera->IsGrabbing()) return false;
try
{
m_PylonGrabResult = m_pCamera->RetrieveResult(5000, TimeoutHandling_ThrowException);
if (m_PylonGrabResult.IsValid())
{
m_bIsRunning = true;
InvalidateRect(m_hWndDisplay, NULL, FALSE);
return true;
}
}
catch (const GenericException& e)
{
CString strMsg;
strMsg.Format(_T("Failed to retrieve result: %hs"), e.what());
AfxMessageBox(strMsg, MB_ICONERROR);
}
return false;
}
```
这个示例代码提供了以下功能:
- 枚举相机并在多个可用相机中进行选择
- 打开相机并配置其参数
- 开始和停止从相机抓取图像
- 在 MFC 窗口上显示抓取的图像
你可以根据自己的需求进行修改。
阅读全文