#include "StdAfx.h" #include "BaslerCamera.h" #include "../ImageFunc/ImageFunc.h" #pragma comment(lib,"PylonGigE_MD_VC100_TL.lib") volatile int BaslerCamera::Inited=0; //ÊÇ·ñÒѾ­³õʼ»¯ int BaslerCamera::TotalCameraCount=0; //Ïà»úÊýÁ¿ using namespace std; BaslerCamera::BaslerCamera(void) { m_IsCameraOpened=0; ExposeTimemS=35; } BaslerCamera::~BaslerCamera(void) { if (m_IsCameraOpened) { this->CloseCamera(); } } int BaslerCamera::InitLib() { if (0==Inited++) { Pylon::DeviceInfoList list; Pylon::DeviceInfoList filter; TotalCameraCount=Pylon::CTlFactory::GetInstance().EnumerateDevices(list,filter); } return TotalCameraCount; } int BaslerCamera::GetCameraInfo(int index, MyCameraInfo * pMyCameraInfo) { CString s1; CString results[10]; ResultStr.Empty(); Pylon::CBaslerGigEDeviceInfo info = camera.GetDeviceInfo(); if (info.IsVendorNameAvailable()) { pMyCameraInfo->sMfgName = info.GetVendorName(); } if (info.IsModelNameAvailable()) { pMyCameraInfo->sModelName = info.GetModelName(); } if (info.IsSerialNumberAvailable()) { pMyCameraInfo->sSerialNumber = info.GetSerialNumber(); } if (info.IsUserDefinedNameAvailable()) { pMyCameraInfo->sUserDefinedName = info.GetUserDefinedName(); } if (info.IsIpAddressAvailable()) { s1 = info.GetIpAddress(); Split(s1,_T("."),results); pMyCameraInfo->camIp[0] = _tstoi(results[0]); pMyCameraInfo->camIp[1] = _tstoi(results[1]); pMyCameraInfo->camIp[2] = _tstoi(results[2]); pMyCameraInfo->camIp[3] = _tstoi(results[3]); } if (info.IsMacAddressAvailable()) { s1 = info.GetMacAddress(); Split(s1,_T("-"),results); pMyCameraInfo->camMac[0] = _tstoi(results[0]); pMyCameraInfo->camMac[1] = _tstoi(results[1]); pMyCameraInfo->camMac[2] = _tstoi(results[2]); pMyCameraInfo->camMac[3] = _tstoi(results[3]); pMyCameraInfo->camMac[4] = _tstoi(results[4]); pMyCameraInfo->camMac[5] = _tstoi(results[5]); } if (info.IsInterfaceAvailable()) { s1 = info.GetInterface(); Split(s1,_T("."),results); pMyCameraInfo->ifIp[0] = _tstoi(results[0]); pMyCameraInfo->ifIp[1] = _tstoi(results[1]); pMyCameraInfo->ifIp[2] = _tstoi(results[2]); pMyCameraInfo->ifIp[3] = _tstoi(results[3]); } return CameraOk; } int BaslerCamera::OpenCamera(int Index) { CString s1; s1.Format(_T("¿ªÊ¼³õʼ»¯GigeVisionÏà»ú ....\r\n")); if (m_IsCameraOpened) {this->CloseCamera();} try { Pylon::IPylonDevice *pdevice1; Pylon::DeviceInfoList list; Pylon::DeviceInfoList filter; Pylon::CDeviceInfo di; int j; j=Pylon::CTlFactory::GetInstance().EnumerateDevices(list,filter); if (j<=Index) {return -1;} di=list.at(Index); pdevice1=Pylon::CTlFactory::GetInstance().CreateDevice(di); camera.Attach( pdevice1); camera.Open(); camera.PixelFormat.SetValue(Basler_GigECamera::PixelFormat_BayerGB8); camera.TriggerSelector.SetValue(Basler_GigECamera::TriggerSelector_FrameStart,true); camera.TriggerMode.SetValue(Basler_GigECamera::TriggerMode_On,true); camera.TriggerSource.SetValue(Basler_GigECamera::TriggerSource_Software,true); m_IsCameraOpened=1; s1.Format(_T("´ò¿ªBaslerÏà»ú%d³É¹¦ \r\n"),Index); ResultStr=s1; } catch (GenICam::GenericException &e) { // Error handling cerr << "An exception occurred." << endl << e.GetDescription() << endl; CStringA s1A; CString s1T; s1A=e.GetDescription(); AnsiToT(s1A,s1T); s1.Format(_T("Ïà»ú³õʼ»¯Ê§°Ü ¾ßÌåÐÅϢΪ %s \r\n"),s1T); ResultStr=s1; m_IsCameraOpened=0; return CameraError; } catch (...) { return CameraError; } return CameraOk; } int BaslerCamera::CloseCamera() { camera.Close(); camera.DetachDevice(); this->m_IsCameraOpened=0; return CameraOk; } int BaslerCamera::SetExposureTime(double ExposeTime) { try { ExposeTimemS=ExposeTime; if (m_IsCameraOpened) camera.ExposureTimeAbs.SetValue(ExposeTimemS*1000.0,true); } catch (GenICam::GenericException &e) { // Error handling CString s1; cerr << "An exception occurred." << endl << e.GetDescription() << endl; CStringA s1A; CString s1T; s1A=e.GetDescription(); AnsiToT(s1A,s1T); s1.Format(_T("ÉèÖÃÏà»ú²ÎÊýʧ°Ü ¾ßÌåÐÅϢΪ %s \r\n"),s1T); ResultStr=s1; return CameraError; } return CameraOk; } int BaslerCamera::StartGrab() { CString s1; try { camera.ExposureTimeAbs.SetValue(ExposeTimemS*1000.0,true); camera.StartGrabbing(1,Pylon::GrabStrategy_OneByOne,Pylon::GrabLoop_ProvidedByUser); if (camera.WaitForFrameTriggerReady(TimeOutmS)) { camera.ExecuteSoftwareTrigger(); }else { return CameraError; } } catch (GenICam::GenericException &e) { //Error handling cerr << "An exception occurred." << endl << e.GetDescription() << endl; CStringA s1A; CString s1T; s1A=e.GetDescription(); AnsiToT(s1A,s1T); s1.Format(_T(" %s \r\n"),s1T); ResultStr=s1; camera.StopGrabbing(); return CameraError; } return CameraOk; } int BaslerCamera::WaitForGrabFinish(int TimeOutmS) { CString s1; try { if (camera.RetrieveResult(TimeOutmS,ptrGrabResult,Pylon::TimeoutHandling_ThrowException)) return CameraOk; else return CameraError; } catch (GenICam::GenericException &e) { // Error handling cerr << "An exception occurred." << endl << e.GetDescription() << endl; CStringA s1A; CString s1T; s1A=e.GetDescription(); AnsiToT(s1A,s1T); s1.Format(_T(" %s \r\n"),s1T); ResultStr=s1; camera.StopGrabbing(); return CameraError; } return CameraOk; } int BaslerCamera::FetchImageData() { CString s1; if (ptrGrabResult==NULL) { return CameraError; } if (ptrGrabResult->GrabSucceeded()) { int w,h; size_t stride; Pylon::PixelType pixeltype; pixeltype=ptrGrabResult->GetPixelType(); ptrGrabResult->GetStride(stride); w=ptrGrabResult->GetWidth(); h=ptrGrabResult->GetHeight(); UCHAR *srcpixel=(UCHAR *)(ptrGrabResult->GetBuffer()); if (pixeltype==Pylon::PixelType_BayerGB8) { Image1.CopyFromBuf(srcpixel,w,h,stride,MyImage::MyPixelType_BayerGB8); } s1.Format(_T("ÅÄÕճɹ¦ pixeltype %X w %d h %d stride %d \r\n"),pixeltype,w,h,stride); ResultStr=s1; } else { CStringA s1A; s1A=ptrGrabResult->GetErrorDescription().c_str(); CString s1T; AnsiToT(s1A,s1T); s1.Format(_T("ÅÄÕÕʧ°Ü %0X %s\r\n"),ptrGrabResult->GetErrorCode(),s1T); ResultStr=s1; return CameraError; } return CameraOk; } int BaslerCamera::SetPixelFormat(Basler_GigECamera::PixelFormatEnums newPixelFormat) { try { camera.PixelFormat.SetValue(newPixelFormat); } catch (GenICam::GenericException &e) { // Error handling CString s1; cerr << "An exception occurred." << endl << e.GetDescription() << endl; CStringA s1A; CString s1T; s1A=e.GetDescription(); AnsiToT(s1A,s1T); s1.Format(_T("ÉèÖÃÏà»ú²ÎÊýʧ°Ü ¾ßÌåÐÅϢΪ %s \r\n"),s1T); ResultStr=s1; return CameraError; } return CameraOk; }