基于迈德威视相机的封装
.h头文件
通过宏和条件编译来决定使用哪个相机SDK
//#define USING_MINDVISION_GRAB
#ifdef USING_MINDVISION_GRAB
#include "CameraApi.h"
#pragma comment(lib, "MVCAMSDK_X64.lib")
class CCameraMindVision
{
public:
CCameraMindVision();
~CCameraMindVision();
private:
CameraHandle m_hCamera; //相机的设备句柄|the handle of the camera we use
bool m_kong;
BYTE *m_pImageDataR;
BYTE *m_pImageDataG;
BYTE *m_pImageDataB;
public:
HObject grabImg;
HTuple hWith;
HTuple hHeight;
HTuple hWndID;
public:
static int InitCameraDevice();
static void CloseCameraDevice();
BOOL StartDevice(int cmIndex, char *chCamIndex);
void StopDevice();
/*void setShutter(int shutterTm);
void setGain(int gain);*/
BOOL GetImage(HObject *pDstImg=NULL);
};
#endif
这里是相机功能的实现
主要是1.初始化枚举相机相机2.关闭相机驱动的3.选择一个相机打开驱动4关闭驱动5抓图
如果需要扩展其他的相机,成员函数名不变,通过定义不同的宏名 来使用
#ifdef USING_MINDVISION_GRAB
CCameraMindVision::CCameraMindVision()
{
m_hCamera = 0;
m_kong = 1;
m_pImageDataR = NULL;
m_pImageDataG = NULL;
m_pImageDataB = NULL;
}
CCameraMindVision::~CCameraMindVision()
{
if(!m_kong)
{
//delete []m_pImageData;
delete []m_pImageDataR;
delete []m_pImageDataG;
delete []m_pImageDataB;
}
}
int CCameraMindVision::InitCameraDevice()
{
INT iCameraNums;
iCameraNums = CameraEnumerateDeviceEx();
return iCameraNums;
}
void CCameraMindVision::CloseCameraDevice()
{
}
BOOL CCameraMindVision::StartDevice(int cmIndex, char *chCamIndex)
{
CameraSdkStatus status;
if ((status = CameraInitEx(cmIndex,-1,-1,&m_hCamera)) != CAMERA_STATUS_SUCCESS)
{
return FALSE;
}
CameraPlay(m_hCamera);
return TRUE;
}
void CCameraMindVision::StopDevice()
{
if(m_hCamera != 0)
{
CameraUnInit(m_hCamera);
m_hCamera = 0;
}
}
BOOL CCameraMindVision::GetImage(HObject *pDstImg)
{
tSdkFrameHead sFrameInfo;
BYTE* pRgbBuffer;
pRgbBuffer = CameraGetImageBufferEx(m_hCamera,& sFrameInfo.iWidth,& sFrameInfo.iHeight,1000);
if(pRgbBuffer != NULL)
{
INT iW = sFrameInfo.iWidth;
INT iH = sFrameInfo.iHeight;
HObject Image;
if(m_kong)
{
m_pImageDataR = new BYTE[iW * iH];
m_pImageDataG = new BYTE[iW * iH];
m_pImageDataB = new BYTE[iW * iH];
m_kong = FALSE;
}
int bytewidth = (iW * 3 + 3 ) / 4 * 4 ;
int bytewidthg = (iW + 3 ) / 4 * 4 ;
for( int j = iH-1; j>=0; j--)
{//(注意tuple中图像数据的存放和VC中的差别)
for(int i = 0; i < iW; i++)
{
*(m_pImageDataB + j * bytewidthg + i ) =* (pRgbBuffer + j * bytewidth + i * 3 + 0 );
*(m_pImageDataG + j * bytewidthg + i) =* (pRgbBuffer + j * bytewidth + i * 3 + 1 );
*(m_pImageDataR + j * bytewidthg + i)=* (pRgbBuffer + j * bytewidth + i * 3 + 2 );
}
}
GenImage3Extern(pDstImg,"byte",iW,iH,
(long)m_pImageDataR,
(long)m_pImageDataG,
(long)m_pImageDataB,NULL);
return TRUE;
}
else
{
return FALSE;
}
}
#endif