首先引用海康威视的MVS中的MvCameraControl.Net
然后 using MvCamCtrl.NET;
查找设备程序:
其中的ComboBox cbDeviceList是显示设备序列号的下拉列表
public void DeviceListAcq(ComboBox cbDeviceList)
{
int nRet;
// ch:创建设备列表 en:Create Device List
System.GC.Collect();
cbDeviceList.Items.Clear();
nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref m_pDeviceList);
if (0 != nRet)
{
ShowErrorMsg("Enumerate devices fail!", 0);
return;
}
// ch:在窗体列表中显示设备名 | en:Display device name in the form list
for (int i = 0; i < m_pDeviceList.nDeviceNum; i++)
{
MyCamera.MV_CC_DEVICE_INFO device = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_pDeviceList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO));
if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)
{
IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(device.SpecialInfo.stGigEInfo, 0);
MyCamera.MV_GIGE_DEVICE_INFO gigeInfo = (MyCamera.MV_GIGE_DEVICE_INFO)Marshal.PtrToStructure(buffer, typeof(MyCamera.MV_GIGE_DEVICE_INFO));
if (gigeInfo.chUserDefinedName != "")
{
cbDeviceList.Items.Add("GigE: " + gigeInfo.chUserDefinedName + " (" + gigeInfo.chSerialNumber + ")");
}
else
{
cbDeviceList.Items.Add("GigE: " + gigeInfo.chManufacturerName + " " + gigeInfo.chModelName + " (" + gigeInfo.chSerialNumber + ")");
}
}
else if (device.nTLayerType == MyCamera.MV_USB_DEVICE)
{
IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(device.SpecialInfo.stUsb3VInfo, 0);
MyCamera.MV_USB3_DEVICE_INFO usbInfo = (MyCamera.MV_USB3_DEVICE_INFO)Marshal.PtrToStructure(buffer, typeof(MyCamera.MV_USB3_DEVICE_INFO));
if (usbInfo.chUserDefinedName != "")
{
cbDeviceList.Items.Add("USB: " + usbInfo.chUserDefinedName + " (" + usbInfo.chSerialNumber + ")");
}
else
{
cbDeviceList.Items.Add("USB: " + usbInfo.chManufacturerName + " " + usbInfo.chModelName + " (" + usbInfo.chSerialNumber + ")");
}
}
}
// ch:选择第一项 | en:Select the first item
if (m_pDeviceList.nDeviceNum != 0)
{
cbDeviceList.SelectedIndex = 0;
}
}
打开设备程序:
//打开设备
public bool b_NoDevice;
public void OpenDevice(ComboBox cbDeviceList)
{
if (m_pDeviceList.nDeviceNum == 0 || cbDeviceList.SelectedIndex == -1)
{
ShowErrorMsg("No device, please select", 0);
b_NoDevice = true;
return;
}
int nRet = -1;
// ch:获取选择的设备信息 | en:Get selected device information
MyCamera.MV_CC_DEVICE_INFO device =
(MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_pDeviceList.pDeviceInfo[cbDeviceList.SelectedIndex],
typeof(MyCamera.MV_CC_DEVICE_INFO));
// ch:打开设备 | en:Open device
if (null == m_pMyCamera)
{
m_pMyCamera = new MyCamera();
if (null == m_pMyCamera)
{
return;
}
}
nRet = m_pMyCamera.MV_CC_CreateDevice_NET(ref device);
if (MyCamera.MV_OK != nRet)
{
return;
}
nRet = m_pMyCamera.MV_CC_OpenDevice_NET();
if (MyCamera.MV_OK != nRet)
{
m_pMyCamera.MV_CC_DestroyDevice_NET();
ShowErrorMsg("Device open fail!", nRet);
return;
}
// ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)
{
int nPacketSize = m_pMyCamera.MV_CC_GetOptimalPacketSize_NET();
if (nPacketSize > 0)
{
nRet = m_pMyCamera.MV_CC_SetIntValue_NET("GevSCPSPacketSize", (uint)nPacketSize);
if (nRet != MyCamera.MV_OK)
{
Console.WriteLine("Warning: Set Packet Size failed {0:x8}", nRet);
}
}
else
{
Console.WriteLine("Warning: Get Packet Size failed {0:x8}", nPacketSize);
}
}
// ch:设置采集连续模式 | en:Set Continues Aquisition Mode
m_pMyCamera.MV_CC_SetEnumValue_NET("AcquisitionMode", 2);// ch:工作在连续模式 | en:Acquisition On Continuous Mode
m_pMyCamera.MV_CC_SetEnumValue_NET("TriggerMode", 0); // ch:连续模式 | en:Continuous
b_NoDevice = false;
}
连续采图程序:
//连续采集
public void ContinuesGrab(ComboBox cbDeviceList)
{
int nRet;
// ch:开始采集 | en:Start Grabbing
nRet = m_pMyCamera.MV_CC_StartGrabbing_NET();
if (MyCamera.MV_OK != nRet)
{
ShowErrorMsg("Trigger Fail!", nRet);
return;
}
//实时采集
m_pMyCamera.MV_CC_SetEnumValue_NET("TriggerMode", 0);
m_bGrabbing = true;
// ch:显示 | en:Display
nRet = m_pMyCamera.MV_CC_Display_NET(pictureBox1.Handle);
if (MyCamera.MV_OK != nRet)
{
ShowErrorMsg("Display Fail!", nRet);
}
}
//停止采集
public void StopGrab()
{
int nRet = -1;
// ch:停止采集 | en:Stop Grabbing
nRet = m_pMyCamera.MV_CC_StopGrabbing_NET();
if (nRet != MyCamera.MV_OK)
{
ShowErrorMsg("Stop Grabbing Fail!", nRet);
}
m_bGrabbing = false;
}
单步采图程序:
public Bitmap ReadImage()
{
int nRet;
UInt32 nPayloadSize = 0;
MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
nRet = m_pMyCamera.MV_CC_GetIntValue_NET("PayloadSize", ref stParam);
if (MyCamera.MV_OK != nRet)
{
ShowErrorMsg("Get PayloadSize failed", nRet);
return null;
}
nPayloadSize = stParam.nCurValue;
if (nPayloadSize > m_nBufSizeForDriver)
{
m_nBufSizeForDriver = nPayloadSize;
m_pBufForDriver = new byte[m_nBufSizeForDriver];
// ch:同时对保存图像的缓存做大小判断处理 | en:Determine the buffer size to save image
// ch:BMP图片大小:width * height * 3 + 2048(预留BMP头大小) | en:BMP image size: width * height * 3 + 2048 (Reserved for BMP header)
m_nBufSizeForSaveImage = m_nBufSizeForDriver * 3 + 2048;
m_pBufForSaveImage = new byte[m_nBufSizeForSaveImage];
}
IntPtr pData = Marshal.UnsafeAddrOfPinnedArrayElement(m_pBufForDriver, 0);
MyCamera.MV_FRAME_OUT_INFO_EX stFrameInfo = new MyCamera.MV_FRAME_OUT_INFO_EX();
// ch:超时获取一帧,超时时间为1秒 | en:Get one frame timeout, timeout is 1 sec
nRet = m_pMyCamera.MV_CC_GetOneFrameTimeout_NET(pData, m_nBufSizeForDriver, ref stFrameInfo, 1000);
if (MyCamera.MV_OK != nRet)
{
ShowErrorMsg("No Data!", nRet);
return null;
}
MyCamera.MvGvspPixelType enDstPixelType;
if (IsMonoData(stFrameInfo.enPixelType))
{
enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8;
}
else if (IsColorData(stFrameInfo.enPixelType))
{
enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed;
}
else
{
ShowErrorMsg("No such pixel type!", 0);
return null;
}
IntPtr pImage = Marshal.UnsafeAddrOfPinnedArrayElement(m_pBufForSaveImage, 0);
MyCamera.MV_SAVE_IMAGE_PARAM_EX stSaveParam = new MyCamera.MV_SAVE_IMAGE_PARAM_EX();
MyCamera.MV_PIXEL_CONVERT_PARAM stConverPixelParam = new MyCamera.MV_PIXEL_CONVERT_PARAM();
stConverPixelParam.nWidth = stFrameInfo.nWidth;
stConverPixelParam.nHeight = stFrameInfo.nHeight;
stConverPixelParam.pSrcData = pData;
stConverPixelParam.nSrcDataLen = stFrameInfo.nFrameLen;
stConverPixelParam.enSrcPixelType = stFrameInfo.enPixelType;
stConverPixelParam.enDstPixelType = enDstPixelType;
stConverPixelParam.pDstBuffer = pImage;
stConverPixelParam.nDstBufferSize = m_nBufSizeForSaveImage;
nRet = m_pMyCamera.MV_CC_ConvertPixelType_NET(ref stConverPixelParam);
if (MyCamera.MV_OK != nRet)
{
return null;
}
if (enDstPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8)
{
//************************Mono8 转 Bitmap*******************************
Bitmap bmp = new Bitmap(stFrameInfo.nWidth, stFrameInfo.nHeight, stFrameInfo.nWidth * 1, PixelFormat.Format8bppIndexed, pImage);
ColorPalette cp = bmp.Palette;
// init palette
for (int i = 0; i < 256; i++)
{
cp.Entries[i] = Color.FromArgb(i, i, i);
}
// set palette back
bmp.Palette = cp;
return bmp;
}
else
{
//*********************RGB8 转 Bitmap**************************
for (int i = 0; i < stFrameInfo.nHeight; i++)
{
for (int j = 0; j < stFrameInfo.nWidth; j++)
{
byte chRed = m_pBufForSaveImage[i * stFrameInfo.nWidth * 3 + j * 3];
m_pBufForSaveImage[i * stFrameInfo.nWidth * 3 + j * 3] = m_pBufForSaveImage[i * stFrameInfo.nWidth * 3 + j * 3 + 2];
m_pBufForSaveImage[i * stFrameInfo.nWidth * 3 + j * 3 + 2] = chRed;
}
}
try
{
Bitmap bmp = new Bitmap(stFrameInfo.nWidth, stFrameInfo.nHeight, stFrameInfo.nWidth * 3, PixelFormat.Format24bppRgb, pImage);
return bmp;
}
catch
{
}
}
return null;
}
private Boolean IsMonoData(MyCamera.MvGvspPixelType enGvspPixelType)
{
switch (enGvspPixelType)
{
case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono10:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono10_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono12:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono12_Packed:
return true;
default:
return false;
}
}
/************************************************************************
* @fn IsColorData()
* @brief 判断是否是彩色数据
* @param enGvspPixelType [IN] 像素格式
* @return 成功,返回0;错误,返回-1
************************************************************************/
private Boolean IsColorData(MyCamera.MvGvspPixelType enGvspPixelType)
{
switch (enGvspPixelType)
{
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGR8:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerRG8:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGB8:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerBG8:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGR10:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerRG10:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGB10:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerBG10:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGR12:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerRG12:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGB12:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerBG12:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGR10_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerRG10_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGB10_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerBG10_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGR12_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerRG12_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGB12_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerBG12_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_YUV422_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_YUV422_YUYV_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_YCBCR411_8_CBYYCRYY:
return true;
default:
return false;
}
}