[C++] 纯文本查看 复制代码 class MVS {
public:
//opencv 图像
cv::Mat Image;
private:
//设备信息列表结构体 支持最多256个设备
MV_CC_DEVICE_INFO_LIST stDeviceList;
//设备句柄
void* Handle = NULL;
//图像数据参数
MVCC_INTVALUE stParam;
//图片缓冲区
unsigned char* pData = NULL;
//输出帧的信息
MV_FRAME_OUT_INFO_EX stImageInfo = { 0 };
int RGB2BGR(unsigned char* pRgbData, unsigned int nWidth, unsigned int nHeight) {
if (NULL == pRgbData) {
return MV_E_PARAMETER;
}
for (unsigned int j = 0; j < nHeight; j++) {
for (unsigned int i = 0; i < nWidth; i++) {
unsigned char red = pRgbData[j * (nWidth * 3) + i * 3];
pRgbData[j * (nWidth * 3) + i * 3] = pRgbData[j * (nWidth * 3) + i * 3 + 2];
pRgbData[j * (nWidth * 3) + i * 3 + 2] = red;
}
}
return MV_OK;
}
bool Convert2Mat(MV_FRAME_OUT_INFO_EX* pstImageInfo, unsigned char* pData) {
cv::Mat srcImage;
if (pstImageInfo->enPixelType == PixelType_Gvsp_Mono8) {
srcImage = cv::Mat(pstImageInfo->nHeight, pstImageInfo->nWidth, CV_8UC1, pData);
} else if (pstImageInfo->enPixelType == PixelType_Gvsp_RGB8_Packed) {
if (RGB2BGR(pData, pstImageInfo->nWidth, pstImageInfo->nHeight) == MV_OK) { srcImage = cv::Mat(pstImageInfo->nHeight, pstImageInfo->nWidth, CV_8UC3, pData); } else {
return false;
}
} else {
return false;
}
if (NULL == srcImage.data) {
return false;
}
Image = srcImage.clone();
srcImage.release();
return true;
}
public:
//枚举设备 成功返回设备数量 常用 USB 与 GIGE
unsigned int Enumerate(unsigned int nTLayerType= MV_GIGE_DEVICE | MV_USB_DEVICE) {
memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
if (MV_CC_EnumDevices(nTLayerType, &stDeviceList) != MV_OK) { return 0; } else {return stDeviceList.nDeviceNum;}
}
//取主要版本
unsigned short Get_device_MajorVer(unsigned int nID) {return stDeviceList.pDeviceInfo[nID]->nMajorVer;}
//取次要版本
unsigned short Get_device_MinorVer(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->nMinorVer; }
//取高MAC地址
unsigned int Get_device_MacAddrHigh(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->nMacAddrHigh; }
//取低MAC地址
unsigned int Get_device_MacAddrLow(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->nMacAddrLow; }
//取设备传输层协议类型
unsigned int Get_device_TLayerType(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->nTLayerType; }
//取GigE设备信息 IP配置选项
unsigned int Get_device_GigE_IpCfgOption(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stGigEInfo.nIpCfgOption; }
//取GigE设备信息 当前IP配置
unsigned int Get_device_GigE_IpCfgCurrent(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stGigEInfo.nIpCfgCurrent; }
//取GigE设备信息 当前IP地址
unsigned int Get_device_GigE_CurrentIp(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stGigEInfo.nCurrentIp;}
//取GigE设备信息 当前子网掩码
unsigned int Get_device_GigE_CurrentSubNetMask(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stGigEInfo.nCurrentSubNetMask; }
//取GigE设备信息 当前网关
unsigned int Get_device_GigE_DefultGateWay(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stGigEInfo.nDefultGateWay; }
//取GigE设备信息 制造商名称
unsigned char* Get_device_GigE_ManufacturerName(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stGigEInfo.chManufacturerName; }
//取GigE设备信息 型号名称
unsigned char* Get_device_GigE_ModelName(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stGigEInfo.chModelName; }
//取GigE设备信息 设备版本
unsigned char* Get_device_GigE_DeviceVersion(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stGigEInfo.chDeviceVersion; }
//取GigE设备信息 制造商的具体信息
unsigned char* Get_device_GigE_ManufacturerSpecificInfo(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stGigEInfo.chManufacturerSpecificInfo; }
//取GigE设备信息 序列号
unsigned char* Get_device_GigE_SerialNumber(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stGigEInfo.chSerialNumber; }
//取GigE设备信息 用户自定义名称
unsigned char* Get_device_GigE_UserDefinedName(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stGigEInfo.chUserDefinedName; }
//取GigE设备信息 网口IP地址
unsigned int Get_device_GigE_NetExport(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stGigEInfo.nNetExport; }
//取USB设备信息 控制输入端点
unsigned char Get_device_USB_InEndPoint(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stUsb3VInfo.CrtlInEndPoint; }
//取USB设备信息 控制输出端点
unsigned char Get_device_USB_OutEndPoint(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stUsb3VInfo.CrtlOutEndPoint; }
//取USB设备信息 流端点
unsigned char Get_device_USB_StreamEndPoint(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stUsb3VInfo.StreamEndPoint; }
//取USB设备信息 事件端点
unsigned char Get_device_USB_EventEndPoint(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stUsb3VInfo.EventEndPoint; }
//取USB设备信息 供应商ID号
unsigned short Get_device_USB_idVendor(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stUsb3VInfo.idVendor; }
//取USB设备信息 产品ID号
unsigned short Get_device_USB_idProduct(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stUsb3VInfo.idProduct; }
//取USB设备信息 设备索引号
unsigned int Get_device_USB_DeviceNumber(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stUsb3VInfo.nDeviceNumber; }
//取USB设备信息 设备GUID号
unsigned char* Get_device_USB_DeviceGUID(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stUsb3VInfo.chDeviceGUID; }
//取USB设备信息 供应商名字
unsigned char* Get_device_USB_VendorName(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stUsb3VInfo.chVendorName; }
//取USB设备信息 型号名字
unsigned char* Get_device_USB_ModelName(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stUsb3VInfo.chModelName; }
//取USB设备信息 家族名字
unsigned char* Get_device_USB_FamilyName(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stUsb3VInfo.chFamilyName; }
//取USB设备信息 设备版本
unsigned char* Get_device_USB_DeviceVersion(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stUsb3VInfo.chDeviceVersion; }
//取USB设备信息 制造商名字
unsigned char* Get_device_USB_ManufacturerName(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stUsb3VInfo.chManufacturerName; }
//取USB设备信息 序列号
unsigned char* Get_device_USB_SerialNumber(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stUsb3VInfo.chSerialNumber;}
//取USB设备信息 支持的USB协议
unsigned int Get_device_USB_bcdUSB(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stUsb3VInfo.nbcdUSB; }
//取USB设备信息 设备地址
unsigned int Get_device_USB_DeviceAddress(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stUsb3VInfo.nDeviceAddress; }
//取CameraLink设备信息 端口号
unsigned char* Get_device_deviceCameraLink_PortID(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stCamLInfo.chPortID; }
//取CameraLink设备信息 型号名字
unsigned char* Get_device_deviceCameraLink_ModelName(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stCamLInfo.chModelName; }
//取CameraLink设备信息 名称
unsigned char* Get_device_deviceCameraLink_FamilyName(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stCamLInfo.chFamilyName; }
//取CameraLink设备信息 设备版本
unsigned char* Get_device_deviceCameraLink_DeviceVersion(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stCamLInfo.chDeviceVersion; }
//取CameraLink设备信息 制造商名字
unsigned char* Get_device_deviceCameraLink_ManufacturerName(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stCamLInfo.chManufacturerName; }
//取CameraLink设备信息 制造商名字
unsigned char* Get_device_deviceCameraLink_SerialNumber(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stCamLInfo.chSerialNumber; }
//创建设备句柄
bool Create(unsigned int nID=0) { if (MV_CC_CreateHandle(&Handle, stDeviceList.pDeviceInfo[nID]) != MV_OK) { return false; } else { return true; } }
//打开设备
bool Open() {if( MV_CC_OpenDevice(Handle) != MV_OK) { return false; } else { return true; }}
//关闭设备
bool Close() { if (MV_CC_CloseDevice(Handle) != MV_OK) { return false; } else { return true; }}
//设置曝光 15 - 9999500
bool SetExposureTime(float value) { if (MV_CC_SetFloatValue(Handle, "ExposureTime", value) != MV_OK) { return false; } else { return true; }}
//像素格式 Opencv支持 Mono8(黑白) 或 RGB8Packed(彩色)
bool SetPixelFormat( bool value = false) {
if (value) {
if (MV_CC_SetEnumValueByString(Handle, "PixelFormat", "Mono8") != MV_OK) { return false; } else { return true; }
} else {
if (MV_CC_SetEnumValueByString(Handle, "PixelFormat", "RGB8Packed") != MV_OK) { return false; } else { return true; }
}
}
//检测网络最优封装尺寸(仅适用于GigE摄像头)
bool OptimizedSize(unsigned int nID) {
if (stDeviceList.pDeviceInfo[nID]->nTLayerType == MV_GIGE_DEVICE) {
int nPacketSize = MV_CC_GetOptimalPacketSize(Handle);
if (nPacketSize > 0) {
if (MV_CC_SetIntValue(Handle, "GevSCPSPacketSize", nPacketSize) == MV_OK) {
return true;
}
}
}
return false;
}
// 设置触发模式为关闭 0 是关闭
bool SetTriggerMode( unsigned int nValue=0) { if(MV_CC_SetEnumValue(Handle, "TriggerMode", nValue) != MV_OK) { return false; } else { return true; }}
//创建图像数据缓冲区
bool StartGrabbing() {
memset(&stParam, 0, sizeof(MVCC_INTVALUE));
if (MV_CC_GetIntValue(Handle, "PayloadSize", &stParam) == MV_OK) {
if (MV_CC_StartGrabbing(Handle) != MV_OK) { return false; } else {
memset(&stImageInfo, 0, sizeof(MV_FRAME_OUT_INFO_EX));
pData = (unsigned char*)malloc(sizeof(unsigned char) * (stParam.nCurValue));
if (pData == NULL) { return false; }
return true;
}
} else {
{ return false; }
}
}
//获取一帧图片
bool GetimagData(unsigned int nMsec=1000) {
if (MV_CC_GetOneFrameTimeout(Handle, pData, stParam.nCurValue, &stImageInfo, nMsec) != MV_OK)
{
return false;
} else {
return Convert2Mat(&stImageInfo, pData);
}
}
//析函数 释放资源
~MVS() {
//停止数据流
MV_CC_StopGrabbing(Handle);
//关闭设备
MV_CC_CloseDevice(Handle);
//销毁设备句柄
if (Handle != NULL) {
MV_CC_DestroyHandle(Handle);
}
if (pData != NULL) {
free(pData);
}
Image.release();
}
};
int main()
{
//创建海康相机对象
MVS 海康相机;
//枚举相机 如果没有返回-1
if (!海康相机.Enumerate()) {
return -1;
}
//创建 相机0 的对象 失败返回 -1
if (!海康相机.Create(0)) {
return -1;
}
//打开 相机0 失败返回 -1
if (!海康相机.Open()) {
return -1;
}
//设置相机曝光时间 1000
海康相机.SetExposureTime(85930);
//设置像素 黑白 默认黑白
海康相机.SetPixelFormat();
//优化网口相机 封装尺寸 USB 忽略
//海康相机.OptimizedSize(0);
//关闭触发模式 失败返回 -1
if (!海康相机.SetTriggerMode()) {
return -1;
}
//创建图像数据缓冲区 用于接收来自相机图像数据 识别返回-1;
if (!海康相机.StartGrabbing()) {
return -1;
}
cv::namedWindow("小白鼠", cv::WINDOW_NORMAL);
while (海康相机.GetimagData()) {
//显示相机图像
cv::imshow("小白鼠", 海康相机.Image);
//等待按键输入
if (cv::waitKey(1)!=-1) break;
}
//相机一定要要正常退出 要不会显示被占用
//自动析结构 释放资源
}
|