C#调用海康工业相机SDK采集图像并在Halcon窗口中显示方式

作者:Kevin_Sun777 时间:2022-06-26 00:48:02 

调用海康工业相机SDK采集图像并在Halcon窗口中显示

最近做项目需要对海康相机进行二次开发,现将所学进行整理。

开发环境    VS2012+C#(32位)  Halcon12

引用动态链接库

引用Halcon动态链接库(halcondotnet.dll)

引用海康相机动态链接库(MvCameraControl.Net.dll)这个文件在MVS安装目录下MVS\Development\DotNet中,如果你是32位的开发环境就选择win32文件夹下的,如果是64位就选择win64文件夹下的

C#调用海康工业相机SDK采集图像并在Halcon窗口中显示方式

创建相机类

先在创建的类中编写方法,之后实例化相机类,调用类中的方法。

鼠标右键单击工程项目–添加–类,选择“类”,输入类的名称,例如Hikvision,点击右下角的“添加”。

首先,要引入的命名空间:using HalconDotNet;     using MvCamCtrl.NET;

其次,需要用到的全部变量:

private MyCamera m_pMyCamera;
MyCamera.MV_CC_DEVICE_INFO_LIST m_pDeviceList;//设备列表
private MyCamera.MVCC_INTVALUE stParam;//用于接收特定的参数
//为读取、保存图像创建的数组
UInt32 m_nBufSizeForDriver = 3072 * 2048 * 3;
byte[] m_pBufForDriver = new byte[3072 * 2048 * 3];
UInt32 m_nBufSizeForSaveImage = 3072 * 2048 * 3 * 3 + 2048;
byte[] m_pBufForSaveImage = new byte[3072 * 2048 * 3 * 3 + 2048];
//要转成的Halcon图像
HImage image = new HImage();

(1) 查找设备列表

//查找设备
       public void DeviceListAcq()
       {
           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)
           {
               MessageBox.Show("查找设备失败!");
               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;
           }
       }

(2) 打开设备

//打开设备
       public void OpenDevice()
       {
           if (m_pDeviceList.nDeviceNum == 0 || cbDeviceList.SelectedIndex == -1)
           {
               MessageBox.Show("未发现设备,请选择");
               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();
               MessageBox.Show("设备打开失败");
               //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
       }

(3) 连续采集

//连续采集(也就是实时显示)
       public void ContinuesGrab(PictureBox picBox)
       {
           int nRet;

// ch:开始采集 | en:Start Grabbing
           nRet = m_pMyCamera.MV_CC_StartGrabbing_NET();
           if (MyCamera.MV_OK != nRet)
           {
               MessageBox.Show("采集失败!");
               //ShowErrorMsg("Trigger Fail!", nRet);
               return;
           }

//实时采集
           m_pMyCamera.MV_CC_SetEnumValue_NET("TriggerMode", 0);

// ch:显示 | en:Display   在PictureBox控件中显示
           nRet = m_pMyCamera.MV_CC_Display_NET(picBox.Handle);
           if (MyCamera.MV_OK != nRet)
           {
               MessageBox.Show("显示失败!");
           }
       }

(4) 停止采集

//停止采集
       public void StopGrab()
       {
           int nRet = -1;
           // ch:停止采集 | en:Stop Grabbing
           nRet = m_pMyCamera.MV_CC_StopGrabbing_NET();
           if (nRet != MyCamera.MV_OK)
           {
               MessageBox.Show("停止采集失败!");
           }

}

(5) 关闭设备

//关闭设备
       public void CloseDevice()
       {
           // ch:关闭设备 | en:Close Device
           int nRet;

nRet = m_pMyCamera.MV_CC_CloseDevice_NET();
           if (MyCamera.MV_OK != nRet)
           {
               return;
           }

nRet = m_pMyCamera.MV_CC_DestroyDevice_NET();
           if (MyCamera.MV_OK != nRet)
           {
               return;
           }
       }

(6) 转成Halcon图像

//读取图片转换成Halcon图像
       public HImage ReadImage()
       {
           int nRet;
           //MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
           UInt32 nPayloadSize = 0;
           nRet = m_pMyCamera.MV_CC_GetIntValue_NET("PayloadSize", ref stParam);
           if (MyCamera.MV_OK != nRet)
           {
               return null;
           }
           nPayloadSize = stParam.nCurValue;
           if (nPayloadSize > m_nBufSizeForDriver)
           {
               m_nBufSizeForDriver = nPayloadSize;
               m_pBufForDriver = new byte[m_nBufSizeForDriver];
               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();
           nRet = m_pMyCamera.MV_CC_GetOneFrameTimeout_NET(pData, m_nBufSizeForDriver, ref stFrameInfo, 1000);//获取一帧图像,超时时间设置为1000
           if (MyCamera.MV_OK != nRet)
           {
               return null;
           }

HImage image = new HImage();

//采集的是黑白图像,利用Halcon图像库中的GenImage1算子来构建图像
           image.GenImage1("byte", (int)stFrameInfo.nWidth, (int)stFrameInfo.nHeight, pData);
           return image;
       }

下面是我所做的一个窗体界面,显示设备序列号的下拉列表做成了一个用户控件,将上述的代码全部放在用户控件中,在主窗体里调用用户控件里的方法来实现相机的连接和采集

具体见示例代码。

C#调用海康工业相机SDK采集图像并在Halcon窗口中显示方式

示例代码:

//查找设备
       private void btnEnum_Click(object sender, EventArgs e)
       {
           ucDeviceList1.DeviceListAcq();
       }

//打开设备
       private void btnOpen_Click(object sender, EventArgs e)
       {
           ucDeviceList1.OpenDevice();
           btnOpen.Enabled = false;
           btnClose.Enabled = true;

btnContinuesGrab.Enabled = true;
           //btnSingleStep.Enabled = true;
           btnStopGrab.Enabled = true;
       }

//关闭设备
       private void btnClose_Click(object sender, EventArgs e)
       {
           ucDeviceList1.CloseDevice();

btnOpen.Enabled = true;
           btnClose.Enabled = false;

btnContinuesGrab.Enabled = false;
           btnSingleStep.Enabled = false;
           btnStopGrab.Enabled = false;
       }

//实时显示
       private void btnContinuesGrab_Click(object sender, EventArgs e)
       {
           ucDeviceList1.ContinuesGrab(picboxShowImg);
           btnContinuesGrab.Enabled = false;
           btnSingleStep.Enabled = true;
           btnStopGrab.Enabled = true;
       }

//单步采集
       private void btnSingleStep_Click(object sender, EventArgs e)
       {
           // 将采集到的图像在Halcon窗口中显示
           HTuple hWind = hWindowControl1.HalconWindow;

HTuple width, height;

HObject hv_image;
           HOperatorSet.GenEmptyObj(out hv_image);
           hv_image.Dispose();
           hv_image = ucDeviceList1.ReadImage();

HOperatorSet.GetImageSize(hv_image, out width, out height);
           HOperatorSet.SetPart(hWind, 0, 0, height - 1, width - 1);
           HOperatorSet.DispObj(img, hWind);

btnStopGrab.Enabled = true;
        }

//停止采集
       private void btnStopGrab_Click(object sender, EventArgs e)
       {
           ucDeviceList1.StopGrab();
           btnContinuesGrab.Enabled = true;
           btnStopGrab.Enabled = false;
       }

//关闭设备
       private void btnClose_Click(object sender, EventArgs e)
       {
           ucDeviceList1.CloseDevice();

btnOpen.Enabled = true;
           btnClose.Enabled = false;

btnContinuesGrab.Enabled = false;
           btnSingleStep.Enabled = false;
           btnStopGrab.Enabled = false;
       }

注:上述方法是不需要修改相机IP的,但是要提前用MVS软件修改相机的IP

来源:https://blog.csdn.net/Kevin_Sun777/article/details/109210383

标签:C#,海康,SDK,采集图像,Halcon
0
投稿

猜你喜欢

  • java9迁移注意问题总结

    2022-07-19 11:26:30
  • Android重要控件SnackBar使用方法详解

    2022-11-10 04:49:41
  • SpringBoot线程池和Java线程池的使用和实现原理解析

    2022-06-27 07:22:30
  • 基于JDK动态代理原理解析

    2022-07-24 19:43:33
  • 利用Java计算某个日期是星期几

    2023-11-17 05:49:42
  • 利用C#实现分割GIF图片

    2021-06-15 16:16:41
  • C#实现发送手机验证码功能

    2022-01-11 18:49:57
  • Java垃圾回收器的方法和原理总结

    2022-06-27 07:11:46
  • SpringBoot利用切面注解及反射实现事件监听功能

    2022-09-25 16:55:00
  • 基于@Bean修饰的方法参数的注入方式

    2022-09-16 06:45:14
  • Java Set集合去重的原理及实现

    2023-08-11 10:56:11
  • Java map为什么不能遍历的同时进行增删操作

    2023-12-21 17:28:29
  • Java爬虫实现Jsoup利用dom方法遍历Document对象

    2023-06-15 07:52:36
  • C#文件下载实例代码(适用于各个浏览器)

    2022-12-28 06:25:48
  • Java异常处理try catch的基本用法

    2022-11-27 11:36:15
  • Android组件Glide实现图片平滑滚动效果

    2022-02-24 22:53:14
  • Java BigDecimal除法精度和格式化输出方式

    2023-01-06 01:03:42
  • Android Studio升级到3.0后遇到的坑

    2022-01-23 00:59:07
  • Java调用第三方http接口的常用方式总结

    2023-11-06 22:47:24
  • 浅谈C#多线程简单例子讲解

    2023-06-24 03:03:50
  • asp之家 软件编程 m.aspxhome.com