#include "stdafx.h" #include "MVCamera2.h" #include "../ImageFunc/ImageFunc.h" volatile int MVCamera2::LabInited=0; //ÊÇ·ñÒѾ­³õʼ»¯ volatile int MVCamera2::TotalCameraCount=0; //Ïà»úÊýÁ¿ #ifdef _WIN64 #pragma comment(lib,"../Mylib/camera/MV_SDK64/mvGige.lib") #else #pragma comment(lib,"../Mylib/camera/MV_SDK/mvGige.lib") #endif #pragma comment(lib,"gdiplus.lib") MVCamera2::MVCamera2(void) { if (0==LabInited++) { MVInitLib(); } m_IsCameraOpened=0; Idx=0; TrigeredCount=0; BackCallCount=0; }; MVCamera2::~MVCamera2(void) { MVStopGrab(m_hCam); if( m_hCam != NULL ) { MVCloseCam(m_hCam); m_hCam = NULL; } if (!m_Raw.IsNull()) {m_Raw.Destroy();} if (!m_image.IsNull()) {m_image.Destroy();} if (0==--LabInited) { MVTerminateLib(); } }; int MVCamera2::InitLib(void) { CString s1; int nCams = 0; MVGetNumOfCameras(&nCams); TotalCameraCount=nCams; if( nCams == 0 ) { return 0; } return nCams; } int MVCamera2::GetCameraInfo(int index, MyCameraInfo * pMyCameraInfo) { MVCamInfo info1; MVGetCameraInfo(index,&info1); memcpy(pMyCameraInfo->camIp,info1.mIpAddr,4); memcpy(pMyCameraInfo->camMac,info1.mEthernetAddr,6); memcpy(pMyCameraInfo->mMfgName,info1.mMfgName,32); memcpy(pMyCameraInfo->mModelName,info1.mModelName,32); memcpy(pMyCameraInfo->mSerialNumber,info1.mSerialNumber,16); memcpy(pMyCameraInfo->mUserDefinedName,info1.mUserDefinedName,4); memcpy(pMyCameraInfo->ifIp,info1.m_IfIp,4); memcpy(pMyCameraInfo->ifMac,info1.m_IfMAC,4); CStringA s1A; CString s1; s1A=pMyCameraInfo->mMfgName; AnsiToT(s1A,s1); pMyCameraInfo->sMfgName=s1; s1A=pMyCameraInfo->mModelName; AnsiToT(s1A,s1); pMyCameraInfo->sModelName=s1; s1A=pMyCameraInfo->mSerialNumber; AnsiToT(s1A,s1); pMyCameraInfo->sSerialNumber=s1; s1A=pMyCameraInfo->mUserDefinedName; AnsiToT(s1A,s1); pMyCameraInfo->sUserDefinedName=s1; return CameraOk; } int MVCamera2::OpenCamera(int Index) { Idx=Index; MVSTATUS_CODES r = MVOpenCamByIndex(Idx,&m_hCam); if( m_hCam == NULL ) { if( r == MVST_ACCESS_DENIED ) { ResultStr=_T("ÎÞ·¨´ò¿ªMVÏà»ú£¬¿ÉÄÜÕý±»±ðµÄÈí¼þ¿ØÖÆ"); return CameraError; } else { ResultStr.Format(_T("ÎÞ·¨´ò¿ªMVÏà»ú%p£¬·¢Éú´íÎó %d"),this,r); return CameraError; } } m_IsCameraOpened=1; MVGetWidth(m_hCam,&m_nWidth); MVGetHeight(m_hCam,&m_nHeight); MV_PixelFormatEnums format; MVGetPixelFormat(m_hCam,&format); m_PixelFormat = format; MVSetTriggerMode(m_hCam, ::TriggerMode_On); MVSetTriggerSource(m_hCam, ::TriggerSource_Software); MVSetStrobeSource(m_hCam,::LineSource_ExposureActive); MVSetStrobeInvert(m_hCam,TRUE); UpdateFps(); UpdateImage(); #ifdef _WIN64 MVStartGrab(m_hCam,StreamCB,(long long)this); //ÉèÖÃÅÄÕջص÷º¯Êý #else MVStartGrab(m_hCam,StreamCB,(long)this); //ÉèÖÃÅÄÕջص÷º¯Êý #endif ResultStr.Format(_T("´ò¿ªMVÏà»ú%d[%p]³É¹¦"),Index,this); return CameraOk; } int MVCamera2::CloseCamera() { return MVCloseCam(m_hCam); } int MVCamera2::GetExposureTime(double *ExposureTime) { MVGetExposureTime(m_hCam,ExposureTime); return CameraOk; } int MVCamera2::SetExposureTime(double ExposeTime) { m_fExpo=ExposeTime; if (!m_IsCameraOpened) { ResultStr.Format(_T("Ïà»úδ´ò¿ª")); return CameraError; } int Res=MVSetExposureTime(m_hCam,m_fExpo*1000.0); if (Res==MVSTATUS_CODES::MVST_SUCCESS) { ResultStr.Format(_T("ÉèÖÃÆعâʱ¼ä%.1f³É¹¦"),m_fExpo); return CameraOk; }else { ResultStr.Format(_T("ÉèÖÃÆعâʱ¼ä%.1fʧ°Ü Err %d "),m_fExpo,Res); return CameraError; } } #ifdef _WIN64 int MVCamera2::StreamCB(MV_IMAGE_INFO *pInfo,long long nUserVal) { MVCamera2 *pDlg = (MVCamera2 *)(nUserVal|0x100000000); return ( pDlg->OnStreamCB(pInfo) ); } #else int MVCamera2::StreamCB(MV_IMAGE_INFO *pInfo,long nUserVal) { MVCamera2 *pDlg = (MVCamera2 *)(nUserVal); return ( pDlg->OnStreamCB(pInfo) ); } #endif int MVCamera2::OnStreamCB( MV_IMAGE_INFO *pInfo) { CString str; BackCallCount++; int w = pInfo->nSizeX; int h = pInfo->nSizeY; int pixeltype=pInfo->nPixelType; ResultStr.AppendFormat(_T("X %d Y %d Type %X P %p "),w,h,pixeltype,pInfo->pImageBuffer); if( m_PixelFormat == ::PixelFormat_Mono8 ) { Image1.CopyFromBuf(pInfo->pImageBuffer,w,h,w,MyImage::MyPixelType_Mono8); ResultStr.AppendFormat(_T("Mono8 \r\n")); } else { Image1.CopyFromBuf(pInfo->pImageBuffer,w,h,w,MyImage::MyPixelType_BayerGR8); ResultStr.AppendFormat(_T("BayerGR8 \r\n")); } Grabok=1; return 0; } int MVCamera2::UpdateFps() { //ͼÏñ¿í¸ß¸Ä±äºó£¬×î´óÖ¡ÂÊ»áËæÖ®±ä»¯ double fFpsMin,fFpsMax; MVGetFrameRateRange(m_hCam,&fFpsMin,&fFpsMax); //ÉèÖõ½×î´óÖ¡ÂÊ return MVSetFrameRate(m_hCam,fFpsMax); } int MVCamera2::UpdateImage() { if (!m_Raw.IsNull()) {m_Raw.Destroy();} if (!m_image.IsNull()) {m_image.Destroy(); } if( m_PixelFormat == ::PixelFormat_Mono8 ) { m_Raw.Create(m_nWidth,m_nHeight,8); m_image.Create(m_nWidth,m_nHeight,8); } else { m_Raw.Create(m_nWidth,m_nHeight,8); m_image.Create(m_nWidth,m_nHeight,24); } return 0; } int MVCamera2::StartGrab() { Grabok=0; int j; j=-1; if (m_hCam!=NULL) { j=MVTriggerSoftware(m_hCam); TrigeredCount++; } if (j!=MVST_SUCCESS) {return CameraError;} return CameraOk; } int MVCamera2::WaitForGrabFinish(int TimeOutmS) { int i; for (i=0;i