巴斯勒相机在VS中如何进行连接调用呢(软触发调用)
1.查找巴斯勒相机网口;
2.调用巴斯勒相机提供的接口,连接巴斯勒相机;
3.初始化相机;
(安装巴斯勒相机软件)
#pragma once
#include
#include "CameraConfiguration.h"
#include "BitmapControl.h"
#include "utilities.h"
#define MAX_CAMERA 10
using namespace std;
class CPylonCamera:
public CImageEventHandler // For receiving grabbed images.
, public CConfigurationEventHandler // For getting notified about device removal.
{
public:
CPylonCamera(void);
~CPylonCamera(void);
public:
// Device list.
Pylon::DeviceInfoList_t m_devices;
// Index of currently used camera.
int m_ixCamera;
// The CInstant camera.
CInstantCamera m_camera[MAX_CAMERA];
// The TransportLayer.
Pylon::CTlFactory *m_pTlFactory;
// Camera Node Map.
GenApi::INodeMap *m_pCameraNodeMap;
// The current pixel type.
Pylon::PixelType m_PixelFormat;
// To save images.
bool m_bSaveImage;
// Pointer for the last grabbed image, which will be displayed.
CGrabResultPtr m_LastGrabbedImage;
CBitmapControl m_ImageControl[MAX_CAMERA];
// Will be used to lock the access.
GenApi::CLock m_lock;
// Used to measure the frame rate.
CStopWatch m_StopWatch;
// Frame rate
CMovingAvg<double> m_Fps;
virtual void OnImageGrabbed(CInstantCamera& camera, const CGrabResultPtr& ptrGrabResult);
//virtual void OnGrabError( CInstantCamera& camera, const char* errorMessage);
//virtual void OnCameraDeviceRemoved( CInstantCamera& camera);
// Set up the camera
void SetupCamera(int index);
void SetTriggerFunction();
void SetCamera(int index);
bool InitCamera(int index,int w=1280,int h=1024);
bool FindCamera();
bool Connect(char *szID,int index);
void StartLive(int index);
void StopLive(int index);
void GetOne(int index);
bool IsGrabbing(int index);
//设置相机参数
bool SetFrame(int index,int nValue); // 设置采集帧率
bool SetGain(int index,int nValue); // 设置增益
bool SetExposureTime(int index,int nValue); // 设置曝光时间
bool SetReverseX(int index,bool bEnable); // 设置X方向镜像
//相机属性
static DeviceInfoList_t GetCameraInfoList();
CString GetDeviceSN(int index);
CString GetDeviceUserID(int index);
void GetCameraPrm(int index);
int m_iExp[MAX_CAMERA];
int m_iGain[MAX_CAMERA];
int m_iWidth[MAX_CAMERA];
int m_iHeight[MAX_CAMERA];
int m_DevDir[MAX_CAMERA];
int m_iFrame[MAX_CAMERA];
bool m_bFrame[MAX_CAMERA];
CString m_strSerialNumber[MAX_CAMERA];
int m_CameraList[MAX_CAMERA];
int m_CurCameraIndex;
int m_CameraNum;
private:
int64_t Adjust(int64_t val, int64_t min,int64_t max,int64_t inc);
public:
long m_ImgBufLen[MAX_CAMERA];
BYTE *m_pGraBuffer[MAX_CAMERA];
BITMAPINFO *m_pBmpInfo[MAX_CAMERA];
};
#include "StdAfx.h"
#include "PylonCamera.h"
using namespace GenApi;
using namespace Pylon;
CPylonCamera::CPylonCamera(void)
{
for(int i=0;i<MAX_CAMERA;i++)
{
m_iGain[i] = 25;
m_iWidth[i] = CAMERA_SIZE_X;
m_iHeight[i] = CAMERA_SIZE_Y;
m_pGraBuffer[i] = NULL;
m_camera[i].RegisterConfiguration(new CAcquireContinuousConfiguration, RegistrationMode_ReplaceAll, Ownership_TakeOwnership);
m_camera[i].RegisterConfiguration( new CameraConfiguration, RegistrationMode_Append, Ownership_TakeOwnership);
m_camera[i].RegisterConfiguration( this, RegistrationMode_Append, Ownership_ExternalOwnership);
m_camera[i].RegisterImageEventHandler( this, RegistrationMode_Append, Ownership_ExternalOwnership);
m_pBmpInfo[i] = new BITMAPINFO;
}
m_CurCameraIndex = 0;
}
CPylonCamera::~CPylonCamera(void)
{
for(int i=0;i<MAX_CAMERA;i++)
{
m_camera[i].DestroyDevice();
delete m_pBmpInfo[i];
// 回收图像缓冲区
if(m_pGraBuffer[i]!=NULL) delete []m_pGraBuffer[i];
}
}
bool CPylonCamera::FindCamera()
{
CString str;
m_pTlFactory = &CTlFactory::GetInstance ();
m_devices.clear ();
m_pTlFactory->EnumerateDevices (m_devices);
int nSize = m_devices.size();
if(m_devices.empty ())
{
AfxMessageBox("没有安装相机!");
return false;
}
else
{
for(int i = 0; i < nSize; i++)
{
str.Format("相机[%d] : %s", i + 1, m_devices[i].GetFullName().c_str());
}
}
return true;
}
bool CPylonCamera::InitCamera(int index,int w,int h)
{
m_iWidth[index] = w;
m_iHeight[index] = h;
m_ImgBufLen[index] = m_iWidth[index] * m_iHeight[index] * 3;
/*
分配Bayer转换后图像数据缓冲
*/
m_pGraBuffer[index] = new BYTE[(m_iWidth[index]+2) * (m_iHeight[index]+2)];
ASSERT(m_pGraBuffer);
// Reset the viewer.
m_LastGrabbedImage.Release();
m_ImageControl[index].SetImage(m_LastGrabbedImage);
// Force CBitmapControl to repaint its client area.
if(m_ImageControl[index].m_hWnd != NULL) m_ImageControl[index].Invalidate();
// 初始化BITMAPINFO 结构,此结构在保存bmp文件、显示采集图像时使用
m_pBmpInfo[index]->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
// 图像宽度,一般为输出窗口宽度
m_pBmpInfo[index]->bmiHeader.biWidth = m_iWidth[index];
// 图像宽度,一般为输出窗口高度
m_pBmpInfo[index]->bmiHeader.biHeight = m_iHeight[index];
/*
* 以下设置一般相同,
* 对于低于8位的位图,还应设置相应的位图调色板
*/
m_pBmpInfo[index]->bmiHeader.biPlanes = 1;
m_pBmpInfo[index]->bmiHeader.biBitCount = 24;
m_pBmpInfo[index]->bmiHeader.biCompression = BI_RGB;
m_pBmpInfo[index]->bmiHeader.biSizeImage = 0;
m_pBmpInfo[index]->bmiHeader.biXPelsPerMeter = 0;
m_pBmpInfo[index]->bmiHeader.biYPelsPerMeter = 0;
m_pBmpInfo[index]->bmiHeader.biClrUsed = 0;
m_pBmpInfo[index]->bmiHeader.biClrImportant = 0;
return true;
}
bool CPylonCamera::Connect(char *szID,int index)
{
if(index<0 || index>=m_CameraNum) return false;
CString str;
int nSize = m_devices.size();
int tIndex = -1;
for(int i=0;i<nSize;i++)
{
str.Format(m_devices[i].GetSerialNumber());
if(str.Compare(szID)==0)
{
tIndex = i;
m_CameraList[index] = i;
m_strSerialNumber[index] = str;
}
}
if(tIndex<0)
{
str.Format("没有找到相机[%s]",szID);
AfxMessageBox(str);
return false;
}
SetupCamera(index);
return true;
}
void CPylonCamera::SetupCamera(int index)
{
CString str;
int n = m_devices.size();
if(n<index)
return;
try
{
m_camera[index].DestroyDevice();
m_camera[index].Attach(m_pTlFactory->CreateDevice(m_devices[m_CameraList[index]]),Cleanup_None);
if(m_camera[index].IsOpen())
m_camera[index].Close();
m_camera[index].Open();
}
catch (GenICam::GenericException& e)
{
CString str;
str.Format(_T("设置相机[%s] error: %s"),m_devices[m_CameraList[index]].GetSerialNumber().c_str(),e.GetDescription());
TRACE(str);
}
}
void CPylonCamera::StartLive(int index)
{
if(m_camera[index].IsGrabbing()) return;
if (m_camera[index].IsPylonDeviceAttached()) {
m_camera[index].StartGrabbing(GrabStrategy_OneByOne, GrabLoop_ProvidedByInstantCamera);
}
m_StopWatch.Start();
// 设置采集参数
//if(m_iFrame[index]<0) m_iFrame[index]=0;
//if(m_iFrame[index]>4) m_iFrame[index]=4;
//SetFrame(index,m_iFrame[index]*10);
//SetExposureTime(index,m_iExp[index]);
//SetReverseX(index,m_DevDir[index]==TRUE);
//SetGain(index,m_iGain[index]);
}
void CPylonCamera::GetOne(int index)
{
if(m_camera[index].IsGrabbing())
{
m_camera[index].StopGrabbing();
Sleep(200);
}
CGrabResultPtr ptrGrabResult;
m_camera[index].GrabOne(200,ptrGrabResult);
m_ImageControl[index].SetImage(ptrGrabResult);
}
void CPylonCamera::StopLive(int index)
{
m_camera[index].StopGrabbing();
}
DeviceInfoList_t CPylonCamera::GetCameraInfoList()
{
Pylon::DeviceInfoList_t deviceInfoList;
try
{
Pylon::CTlFactory& tlFactory = CTlFactory::GetInstance();
tlFactory.EnumerateDevices(deviceInfoList);
}
catch (GenICam::GenericException& e)
{
CString str;
str.Format(_T("InitAll() error: %s"),e.GetDescription());
TRACE(str);
}
return deviceInfoList;
}
CString CPylonCamera::GetDeviceSN(int index)
{
if(!m_camera[index].IsPylonDeviceAttached()) return "NULL";
CString strCameraSN = "";
DeviceInfoList_t cameraInfoList = CPylonCamera::GetCameraInfoList();
DeviceInfoList_t::iterator it = cameraInfoList.begin();
strCameraSN = (*it).GetSerialNumber().c_str();
return strCameraSN;
}
CString CPylonCamera::GetDeviceUserID(int index)
{
if(!m_camera[index].IsPylonDeviceAttached()) return "NULL";
CString strCameraUserID = "";
DeviceInfoList_t cameraInfoList = CPylonCamera::GetCameraInfoList();
DeviceInfoList_t::iterator it = cameraInfoList.begin();
strCameraUserID = (*it).GetUserDefinedName().c_str();
return strCameraUserID;
}
void CPylonCamera::GetCameraPrm(int index)
{
m_pCameraNodeMap= &m_camera[index].GetNodeMap();
CIntegerPtr ptrExp = m_pCameraNodeMap->GetNode ("ExposureTimeRaw");
m_iExp[index] = (int)ptrExp->GetValue();
CIntegerPtr ptrGain = m_pCameraNodeMap->GetNode ("GainRaw");
m_iGain[index] = (int)ptrGain->GetValue();
CBooleanPtr ptrFrameRateEnable = m_pCameraNodeMap->GetNode("AcquisitionFrameRateEnable");
m_bFrame[index] = ptrFrameRateEnable->GetValue();
if(m_bFrame[index] == true)
{
CIntegerPtr ptrFrame = m_pCameraNodeMap->GetNode ("AcquisitionFrameRateAbs");
if(ptrFrame.IsValid())
{
m_iFrame[index] = (int)ptrFrame->GetValue();
}
}
CIntegerPtr ptrWidth = m_pCameraNodeMap->GetNode ("Width");
m_iWidth[index] = (int)ptrWidth->GetValue();
CIntegerPtr ptrHeight = m_pCameraNodeMap->GetNode ("Height");
m_iHeight[index] = (int)ptrHeight->GetValue();
}
void CPylonCamera::SetTriggerFunction()
{
static bool trigger = false;
if(!trigger)
{
CEnumerationPtr ptrTrigger = m_pCameraNodeMap->GetNode ("TriggerMode");
ptrTrigger->SetIntValue(1);
trigger = true ;
}
else
{
CEnumerationPtr ptrTrigger = m_pCameraNodeMap->GetNode ("TriggerMode");
ptrTrigger->SetIntValue(0);
trigger = false ;
}
}
void CPylonCamera::SetCamera(int index)
{
m_pCameraNodeMap= &m_camera[index].GetNodeMap();
CIntegerPtr ptrExp = m_pCameraNodeMap->GetNode ("ExposureTimeRaw");
ptrExp->SetValue(m_iExp[index]);
CIntegerPtr ptrGain = m_pCameraNodeMap->GetNode ("GainRaw");
ptrGain->SetValue(m_iGain[index]);
CIntegerPtr ptrWidth = m_pCameraNodeMap->GetNode ("Width");
ptrWidth->SetValue(m_iWidth[index]);
CIntegerPtr ptrHeight = m_pCameraNodeMap->GetNode ("Height");
ptrHeight->SetValue(m_iHeight[index]);
}
bool CPylonCamera::SetExposureTime(int index,int nValue)
{
bool ret = false;
try
{
m_pCameraNodeMap = &m_camera[index].GetNodeMap();
CEnumerationPtr ptrExposureMode(m_pCameraNodeMap->GetNode("ExposureMode"));
if(IsWritable(ptrExposureMode))
{
ptrExposureMode->FromString("Timed");
}
CIntegerPtr ptrExposureTimeRaw(m_pCameraNodeMap->GetNode("ExposureTimeRaw"));
if(ptrExposureTimeRaw.IsValid())
{
int64_t newExposureTimeRaw = Adjust(nValue, ptrExposureTimeRaw->GetMin(), ptrExposureTimeRaw->GetMax(), ptrExposureTimeRaw->GetInc());
ptrExposureTimeRaw->SetValue(newExposureTimeRaw);
ret = true;
}
}
catch(GenICam::GenericException &e)
{
CString str;
str.Format(_T("SetExposureTimeRaw() error: %s"),e.GetDescription());
TRACE(str);
}
return ret;
}
bool CPylonCamera::SetGain(int index,int nValue)
{
bool ret = false;
try
{
m_pCameraNodeMap = &m_camera[index].GetNodeMap();
CEnumerationPtr ptrGainAuto(m_pCameraNodeMap->GetNode("GainAuto"));
if(IsWritable(ptrGainAuto))
{
ptrGainAuto->FromString("Off");
}
CEnumerationPtr ptrGainSelector(m_pCameraNodeMap->GetNode("GainSelector"));
if(IsWritable(ptrGainSelector))
{
ptrGainSelector->FromString("DigitalAll");
}
CIntegerPtr ptrGainRaw(m_pCameraNodeMap->GetNode("GainRaw"));
if(ptrGainRaw.IsValid())
{
int64_t newGainRaw = Adjust(nValue, ptrGainRaw->GetMin(), ptrGainRaw->GetMax(), ptrGainRaw->GetInc());
ptrGainRaw->SetValue(newGainRaw);
ret = true;
}
}
catch(GenICam::GenericException &e)
{
CString str;
str.Format(_T("SetGainRaw() error: %s"),e.GetDescription());
TRACE(str);
}
return ret;
}
bool CPylonCamera::SetFrame(int index,int nValue)
{
// nValue 帧率, <=0表示无限制
bool ret = false;
try
{
m_pCameraNodeMap = &m_camera[index].GetNodeMap();
CBooleanPtr ptrFrameRateEnable(m_pCameraNodeMap->GetNode("AcquisitionFrameRateEnable"));
if(IsWritable(ptrFrameRateEnable))
{
if(nValue<=0) ptrFrameRateEnable->SetValue(false);
else ptrFrameRateEnable->SetValue(true);
}
CIntegerPtr ptrFrameRateAbs(m_pCameraNodeMap->GetNode("AcquisitionFrameRateAbs"));
if(ptrFrameRateAbs.IsValid())
{
int64_t newFrameRateAbs = Adjust(nValue, ptrFrameRateAbs->GetMin(), ptrFrameRateAbs->GetMax(), ptrFrameRateAbs->GetInc());
ptrFrameRateAbs->SetValue(newFrameRateAbs);
ret = true;
}
}
catch(GenICam::GenericException &e)
{
CString str;
str.Format(_T("SetFrame() error: %s"),e.GetDescription());
TRACE(str);
}
return ret;
}
bool CPylonCamera::SetReverseX(int index,bool bEnable)
{
bool ret = false;
try
{
m_pCameraNodeMap = &m_camera[index].GetNodeMap();
CBooleanPtr ptrFrameRateEnable(m_pCameraNodeMap->GetNode("ReverseX"));
if(IsWritable(ptrFrameRateEnable))
{
ptrFrameRateEnable->SetValue(bEnable);
}
}
catch(GenICam::GenericException &e)
{
CString str;
str.Format(_T("SetReverseX() error: %s"),e.GetDescription());
TRACE(str);
}
return ret;
}
void CPylonCamera::OnImageGrabbed(CInstantCamera& camera, const CGrabResultPtr& ptrGrabResult)
{
if(ptrGrabResult->GrabSucceeded())
{
CString str;
for(int i=0;i<MAX_CAMERA;i++)
{
str = camera.GetDeviceInfo().GetSerialNumber();
if(str.Compare(m_strSerialNumber[i])==0)
{
m_ImageControl[i].SetImage(ptrGrabResult);
return;
}
}
}
}
int64_t CPylonCamera::Adjust(int64_t val, int64_t min,int64_t max,int64_t inc)
{
if(inc <= 0)
{
throw LOGICAL_ERROR_EXCEPTION("Unexpected increment %d",inc);
}
if(min > max)
{
throw LOGICAL_ERROR_EXCEPTION("minimum bigger than maximum.");
}
if(val < min)
{
return min;
}
if(val > max)
{
return max;
}
if(inc == 1)
{
return val;
}
return min + ( ((val - min) / inc) * inc);
}
bool CPylonCamera::IsGrabbing(int index)
{
return m_camera[index].IsGrabbing();
};
其他引用第三方库以及对应头文件
链接:https://pan.quark.cn/s/71bb1b3e0dad
提取码:JfM9