#include "stdafx.h" #include "MVSCamera.h" #include "../ImageFunc/ImageFunc.h" volatile int MVSCamera::TotalCameraCount=0; //Ïà»úÊýÁ¿ #ifdef _WIN64 #pragma comment(lib,"../Mylib/camera/MVS_SDK/win64/MvCameraControl.lib") #else #pragma comment(lib,"../Mylib/camera/MVS_SDK/win32/MvCameraControl.lib") #endif #pragma comment(lib,"gdiplus.lib") MVSCamera::MVSCamera(void) { Idx=0; m_IsCameraOpened=0; SnapCount=0; TrigeredCount=0; BackCallCount=0; pImageBuf = NULL; }; MVSCamera::~MVSCamera(void) { if (pImageBuf!=NULL) { delete[] pImageBuf; pImageBuf = NULL; } int nRet; //Í£Ö¹²É¼¯Í¼Ïñ nRet = MV_CC_StopGrabbing(m_hCam); if (MV_OK != nRet) { return; } //¹Ø±ÕÉ豸£¬ÊÍ·Å×ÊÔ´ nRet = MV_CC_CloseDevice(m_hCam); if (MV_OK != nRet) { return; } //Ïú»Ù¾ä±ú£¬ÊÍ·Å×ÊÔ´ nRet = MV_CC_DestroyHandle(m_hCam); if (MV_OK != nRet) { return; } }; int MVSCamera::InitLib(void) { CString s1; int nRet = -1; //ö¾Ù×ÓÍøÄÚÖ¸¶¨µÄ´«ÊäЭÒé¶ÔÓ¦µÄËùÓÐÉ豸 MV_CC_DEVICE_INFO_LIST m_stDevList = {0}; nRet = MV_CC_EnumDevices(MV_GIGE_DEVICE, &m_stDevList); if (MV_OK != nRet) { return 0; } TotalCameraCount=m_stDevList.nDeviceNum; return TotalCameraCount; } int MVSCamera::GetCameraInfo(int index, MyCameraInfo * pMyCameraInfo) { CStringA s1A; CString s1; ResultStr.Empty(); MV_CC_DEVICE_INFO info1; int nRet = MV_CC_GetDeviceInfo(m_hCam,&info1); if (MV_OK != nRet) { ResultStr.Format(_T("»ñÈ¡Ïà»úÐÅϢʧ°Ü: GetDeviceInfo [%x]"), nRet); return CameraError; } memcpy(pMyCameraInfo->mMfgName,info1.SpecialInfo.stGigEInfo.chManufacturerName,32); s1A=pMyCameraInfo->mMfgName; AnsiToT(s1A,s1); pMyCameraInfo->sMfgName=s1; memcpy(pMyCameraInfo->mModelName,info1.SpecialInfo.stGigEInfo.chModelName,32); s1A=pMyCameraInfo->mModelName; AnsiToT(s1A,s1); pMyCameraInfo->sModelName=s1; memcpy(pMyCameraInfo->mSerialNumber,info1.SpecialInfo.stGigEInfo.chSerialNumber,16); s1A=pMyCameraInfo->mSerialNumber; AnsiToT(s1A,s1); pMyCameraInfo->sSerialNumber=s1; memcpy(pMyCameraInfo->mUserDefinedName,info1.SpecialInfo.stGigEInfo.chUserDefinedName,16); s1A=pMyCameraInfo->mUserDefinedName; AnsiToT(s1A,s1); pMyCameraInfo->sUserDefinedName=s1; pMyCameraInfo->camIp[0] = (info1.SpecialInfo.stGigEInfo.nCurrentIp >> 3*8) & 0xff; pMyCameraInfo->camIp[1] = (info1.SpecialInfo.stGigEInfo.nCurrentIp >> 2*8) & 0xff; pMyCameraInfo->camIp[2] = (info1.SpecialInfo.stGigEInfo.nCurrentIp >> 1*8) & 0xff; pMyCameraInfo->camIp[3] = (info1.SpecialInfo.stGigEInfo.nCurrentIp >> 0*8) & 0xff; pMyCameraInfo->camMac[0] = (info1.nMacAddrHigh >> 1*8) & 0xff; pMyCameraInfo->camMac[1] = (info1.nMacAddrHigh >> 0*8) & 0xff; pMyCameraInfo->camMac[2] = (info1.nMacAddrLow >> 3*8) & 0xff; pMyCameraInfo->camMac[3] = (info1.nMacAddrLow >> 2*8) & 0xff; pMyCameraInfo->camMac[4] = (info1.nMacAddrLow >> 1*8) & 0xff; pMyCameraInfo->camMac[5] = (info1.nMacAddrLow >> 0*8) & 0xff; pMyCameraInfo->ifIp[0] = (info1.SpecialInfo.stGigEInfo.nNetExport >> 3*8) & 0xff; pMyCameraInfo->ifIp[1] = (info1.SpecialInfo.stGigEInfo.nNetExport >> 2*8) & 0xff; pMyCameraInfo->ifIp[2] = (info1.SpecialInfo.stGigEInfo.nNetExport >> 1*8) & 0xff; pMyCameraInfo->ifIp[3] = (info1.SpecialInfo.stGigEInfo.nNetExport >> 0*8) & 0xff; return CameraOk; } int MVSCamera::OpenCamera(int Index) { ResultStr.Empty(); Idx=Index; MV_CC_DEVICE_INFO_LIST m_stDevList = {0}; int nRet = MV_CC_EnumDevices(MV_GIGE_DEVICE, &m_stDevList); if (MV_OK != nRet) { ResultStr.Format(_T("²éÕÒÏà»úʧ°Ü: EnumDevices [%x]"),nRet); return CameraError; } if (Idx>=m_stDevList.nDeviceNum) { ResultStr.Format(_T("´ò¿ªÏà»úʧ°Ü: Ïà»ú %d MVSÏà»ú×ÜÊý %d"), Idx, m_stDevList.nDeviceNum); return CameraError; } MV_CC_DEVICE_INFO m_stDevInfo = {0}; memcpy(&m_stDevInfo, m_stDevList.pDeviceInfo[Idx], sizeof(MV_CC_DEVICE_INFO)); nRet = MV_CC_CreateHandleWithoutLog(&m_hCam, &m_stDevInfo); if (MV_OK != nRet) { ResultStr.Format(_T("´ò¿ªÏà»úʧ°Ü: CreateHandle [%x]"), nRet); return CameraError; } //Á¬½ÓÉ豸 nRet = MV_CC_OpenDevice(m_hCam); if (MV_OK != nRet) { ResultStr.Format(_T("´ò¿ªÏà»úʧ°Ü: OpenDevice [%x]"), nRet); return CameraError; } m_IsCameraOpened=1; MVCC_INTVALUE iValue; MV_CC_GetWidth(m_hCam,&iValue); m_nWidth = iValue.nCurValue; MV_CC_GetHeight(m_hCam,&iValue); m_nHeight = iValue.nCurValue; nRet = MV_CC_SetPixelFormat(m_hCam,m_PixelFormat); if (MV_OK != nRet) { ResultStr.Format(_T("´ò¿ªÏà»úʧ°Ü: SetPixelFormat [%x]"), nRet); return CameraError; } MV_CC_SetTriggerMode(m_hCam,MV_TRIGGER_MODE_ON); MV_CC_SetTriggerSource(m_hCam,MV_TRIGGER_SOURCE_SOFTWARE); UpdateFps(); nRet = MV_CC_RegisterImageCallBackEx(m_hCam, MVSCamera::ImageCallBackEx, this); if (MV_OK != nRet) { ResultStr.Format(_T("´ò¿ªÏà»úʧ°Ü: RegisterImageCallBackEx [%x]"), nRet); return CameraError; } //¿ªÊ¼²É¼¯Í¼Ïñ nRet = MV_CC_StartGrabbing(m_hCam); if (MV_OK != nRet) { ResultStr.Format(_T("´ò¿ªÏà»úʧ°Ü: StartGrabbing [%x]"), nRet); return CameraError; } ResultStr.Format(_T("´ò¿ªMVSÏà»ú%d[%p]£¬³É¹¦"),Index,this); return CameraOk; } int MVSCamera::CloseCamera() { return MV_CC_CloseDevice(m_hCam); } int MVSCamera::GetExposureTime(double *ExposureTime) { MVCC_FLOATVALUE fValue; MV_CC_GetExposureTime(m_hCam, &fValue); *ExposureTime = fValue.fCurValue; return CameraOk; } int MVSCamera::SetExposureTime(double ExposeTime) { ResultStr.Empty(); m_fExpo=float(ExposeTime); if (!m_IsCameraOpened) { ResultStr.Format(_T("Ïà»úδ´ò¿ª")); return CameraError; } int nRet=MV_CC_SetExposureTime(m_hCam,m_fExpo*1000.0f); if (MV_OK == nRet) { ResultStr.Format(_T("ÉèÖÃÆعâʱ¼ä%.1f³É¹¦"),m_fExpo); return CameraOk; }else { ResultStr.Format(_T("ÉèÖÃÆعâʱ¼ä%.1fʧ°Ü SetExposureTime [%x]"),m_fExpo,nRet); return CameraError; } } void __stdcall MVSCamera::ImageCallBackEx(unsigned char* pData, MV_FRAME_OUT_INFO_EX* pFrameInfo, void* pUser) { MVSCamera *pCam = (MVSCamera *)(pUser); pCam->OnImageCallBackEx(pData, pFrameInfo); } int MVSCamera::OnImageCallBackEx(unsigned char* pData, MV_FRAME_OUT_INFO_EX* pFrameInfo) { BackCallCount++; double time1 = GetTickCountmS(); int stride = pFrameInfo->nFrameLen / pFrameInfo->nHeight; if (pFrameInfo->enPixelType==PixelType_Gvsp_YUV422_YUYV_Packed) { Image1.CopyFromBuf(pData,pFrameInfo->nWidth,pFrameInfo->nHeight,stride,MyImage::MyPixelType_YUV422_YUYV_Packed); ResultStr.AppendFormat(_T("(YUV422_YUYV_Packed)")); }else if (pFrameInfo->enPixelType==PixelType_Gvsp_Mono8) { Image1.CopyFromBuf(pData,pFrameInfo->nWidth,pFrameInfo->nHeight,stride,MyImage::MyPixelType_Mono8); ResultStr.AppendFormat(_T("(Mono8)")); } else if (pFrameInfo->enPixelType==PixelType_Gvsp_BayerGR8) { Image1.CopyFromBuf(pData,pFrameInfo->nWidth,pFrameInfo->nHeight,stride,MyImage::MyPixelType_BayerGR8); ResultStr.AppendFormat(_T("(BayerGR8)")); } else if (pFrameInfo->enPixelType==PixelType_Gvsp_BayerGB8) { Image1.CopyFromBuf(pData,pFrameInfo->nWidth,pFrameInfo->nHeight,stride,MyImage::MyPixelType_BayerGB8); ResultStr.AppendFormat(_T("(BayerGB8)")); } double time2 = GetTickCountmS(); ResultStr.AppendFormat(_T(" copy %.1fmS "),time2-time1); Grabok=1; return 0; } int MVSCamera::UpdateFps() { int nRet = -1; //ͼÏñ¿í¸ß¸Ä±äºó£¬×î´óÖ¡ÂÊ»áËæÖ®±ä»¯ float fFpsMin,fFpsMax; MVCC_FLOATVALUE fValue; nRet = MV_CC_GetFrameRate(m_hCam, &fValue); fFpsMin = fValue.fMin; fFpsMax = fValue.fMax; //ÉèÖõ½×î´óÖ¡ÂÊ nRet = MV_CC_SetFrameRate(m_hCam, fFpsMax); return nRet; } int MVSCamera::StartGrab() { Grabok=0; int nRet = -1; if (m_hCam!=NULL) { nRet = MV_CC_SetCommandValue(m_hCam, "TriggerSoftware"); if (MV_OK != nRet) { return CameraError; } TrigeredCount++; } return CameraOk; } int MVSCamera::WaitForGrabFinish(int TimeOutmS) { int i; for (i=0;i