初始化相机类文件
# pragma once
# include "MvCameraControl.h"
class CMvCamera
{
public: CMvCamera ( ) ; ~ CMvCamera ( ) ; int InitCamera ( ) ; int SaveCurrentImage ( CString filePath) ; void CloseCamera ( ) ; int SetEnumValue ( IN const char * strKey, IN unsigned int nValue) ; int CommandExecute ( IN const char * strKey) ; public: int nRet; void * m_hDevHandle;
} ;
# include "pch.h"
# include "CMvCamera.h" CMvCamera:: CMvCamera ( )
{
} CMvCamera:: ~ CMvCamera ( )
{ CloseCamera ( ) ; if ( m_hDevHandle != NULL ) { MV_CC_DestroyHandle ( m_hDevHandle) ; m_hDevHandle = NULL ; }
} int CMvCamera:: InitCamera ( )
{ nRet = MV_OK; m_hDevHandle = NULL ; nRet = MV_CC_Initialize ( ) ; if ( MV_OK != nRet) { AfxMessageBox ( _T ( "初始化失败" ) ) ; return 0 ; } MV_CC_DEVICE_INFO_LIST stDeviceList; memset ( & stDeviceList, 0 , sizeof ( MV_CC_DEVICE_INFO_LIST) ) ; nRet = MV_CC_EnumDevices ( MV_GIGE_DEVICE | MV_USB_DEVICE, & stDeviceList) ; if ( MV_OK != nRet) { AfxMessageBox ( _T ( "枚举设备失败" ) ) ; return 0 ; } if ( stDeviceList. nDeviceNum > 0 ) { for ( unsigned int i = 0 ; i < stDeviceList. nDeviceNum; i++ ) { MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList. pDeviceInfo[ i] ; if ( NULL == pDeviceInfo) { break ; } } } else { AfxMessageBox ( _T ( "没有找到设备" ) ) ; return 0 ; } unsigned int nIndex = 0 ; nRet = MV_CC_CreateHandle ( & m_hDevHandle, stDeviceList. pDeviceInfo[ nIndex] ) ; if ( MV_OK != nRet) { AfxMessageBox ( _T ( "创建句柄失败" ) ) ; return 0 ; } nRet = MV_CC_OpenDevice ( m_hDevHandle) ; if ( MV_OK != nRet) { AfxMessageBox ( _T ( "打开设备失败" ) ) ; return 0 ; } if ( stDeviceList. pDeviceInfo[ nIndex] -> nTLayerType == MV_GIGE_DEVICE) { int nPacketSize = MV_CC_GetOptimalPacketSize ( m_hDevHandle) ; if ( nPacketSize > 0 ) { nRet = MV_CC_SetIntValue ( m_hDevHandle, "GevSCPSPacketSize" , nPacketSize) ; if ( nRet != MV_OK) { AfxMessageBox ( _T ( "Warning: Set Packet Size fail" ) ) ; return 0 ; } } else { AfxMessageBox ( _T ( "Warning: Get Packet Size fail" ) ) ; return 0 ; } } nRet = MV_CC_SetEnumValue ( m_hDevHandle, "TriggerMode" , 1 ) ; if ( MV_OK != nRet) { AfxMessageBox ( _T ( "Set Trigger Mode fail!" ) ) ; return 0 ; } nRet = MV_CC_SetEnumValue ( m_hDevHandle, "TriggerSource" , MV_TRIGGER_SOURCE_SOFTWARE) ; if ( MV_OK != nRet) { AfxMessageBox ( _T ( "Set Trigger Source fail!" ) ) ; return 0 ; } nRet = MV_CC_StartGrabbing ( m_hDevHandle) ; if ( MV_OK != nRet) { AfxMessageBox ( _T ( "Start Grabbing fail!" ) ) ; return 0 ; } return 1 ; } int CMvCamera:: SaveCurrentImage ( CString filePath)
{ MV_FRAME_OUT stOutFrame = { 0 } ; nRet = MV_CC_GetImageBuffer ( m_hDevHandle, & stOutFrame, 1000 ) ; if ( nRet != MV_OK) { AfxMessageBox ( _T ( "获取图像失败!" ) ) ; return - 1 ; } unsigned int nBufferSize = stOutFrame. stFrameInfo. nWidth * stOutFrame. stFrameInfo. nHeight * 4 + 2048 ; unsigned char * pImageBuf = new unsigned char [ nBufferSize] ; MV_SAVE_IMAGE_PARAM_EX stSaveParam = { 0 } ; stSaveParam. pData = stOutFrame. pBufAddr; stSaveParam. nDataLen = stOutFrame. stFrameInfo. nFrameLen; stSaveParam. enPixelType = stOutFrame. stFrameInfo. enPixelType; stSaveParam. nWidth = stOutFrame. stFrameInfo. nWidth; stSaveParam. nHeight = stOutFrame. stFrameInfo. nHeight; stSaveParam. pImageBuffer = pImageBuf; stSaveParam. nBufferSize = nBufferSize; stSaveParam. enImageType = MV_Image_Jpeg; stSaveParam. nJpgQuality = 95 ; stSaveParam. iMethodValue = 2 ; nRet = MV_CC_SaveImageEx2 ( m_hDevHandle, & stSaveParam) ; if ( MV_OK != nRet) { delete[ ] pImageBuf; MV_CC_FreeImageBuffer ( m_hDevHandle, & stOutFrame) ; CString strError; strError. Format ( _T ( "转换失败! 错误码: 0x%X" ) , nRet) ; AfxMessageBox ( strError) ; return - 2 ; } CFile file; if ( ! file. Open ( filePath, CFile:: modeCreate | CFile:: modeWrite) ) { delete[ ] pImageBuf; MV_CC_FreeImageBuffer ( m_hDevHandle, & stOutFrame) ; AfxMessageBox ( _T ( "创建文件失败!" ) ) ; return - 3 ; } file. Write ( pImageBuf, stSaveParam. nImageLen) ; file. Close ( ) ; delete[ ] pImageBuf; MV_CC_FreeImageBuffer ( m_hDevHandle, & stOutFrame) ; return 0 ;
} void CMvCamera:: CloseCamera ( )
{ nRet = MV_CC_StopGrabbing ( m_hDevHandle) ; if ( MV_OK != nRet) { AfxMessageBox ( _T ( "Stop Grabbing fail! \n" ) ) ; } nRet = MV_CC_CloseDevice ( m_hDevHandle) ; if ( MV_OK != nRet) { AfxMessageBox ( _T ( "ClosDevice fail\n" ) ) ; } nRet = MV_CC_DestroyHandle ( m_hDevHandle) ; if ( MV_OK != nRet) { AfxMessageBox ( _T ( "Destroy Handle fail!\n" ) ) ; } m_hDevHandle = NULL ; } int CMvCamera:: SetEnumValue ( IN const char * strKey, IN unsigned int nValue)
{ return MV_CC_SetEnumValue ( m_hDevHandle, strKey, nValue) ;
} int CMvCamera:: CommandExecute ( IN const char * strKey)
{ return MV_CC_SetCommandValue ( m_hDevHandle, strKey) ;
}
点击按钮进行软触发拍照显示
void CHalconImgTestDlg:: OnBnClickedGetimg ( )
{ int nRet = m_oMvCamera-> CommandExecute ( "TriggerSoftware" ) ; if ( MV_OK != nRet) { AfxMessageBox ( _T ( "执行软件触发失败" ) ) ; return ; } MV_FRAME_OUT stOutFrame = { 0 } ; nRet = MV_CC_GetImageBuffer ( m_oMvCamera-> m_hDevHandle, & stOutFrame, 1000 ) ; if ( nRet != MV_OK) { AfxMessageBox ( _T ( "获取图像失败!" ) ) ; return ; } HalconCpp:: HObject hImage; nRet = theApp. ConverMono8ToHalcon ( & hImage, stOutFrame. stFrameInfo. nHeight, stOutFrame. stFrameInfo. nWidth, stOutFrame. pBufAddr) ; MV_CC_FreeImageBuffer ( m_oMvCamera-> m_hDevHandle, & stOutFrame) ; if ( nRet != MV_OK) { AfxMessageBox ( _T ( "转换到Halcon图像失败!" ) ) ; return ; } HTuple hv_WindowHandle; HWND hWnd = GetDlgItem ( IDC_IMG) -> m_hWnd; if ( ! HDevWindowStack:: IsOpen ( ) ) { OpenWindow ( 0 , 0 , stOutFrame. stFrameInfo. nWidth / 2 , stOutFrame. stFrameInfo. nHeight / 2 , ( Hlong) hWnd, "visible" , "" , & hv_WindowHandle) ; HDevWindowStack:: Push ( hv_WindowHandle) ; } if ( HDevWindowStack:: IsOpen ( ) ) { DispObj ( hImage, HDevWindowStack:: GetActive ( ) ) ; }
}