最近在调试一批海康威视的工业相机,用到了C#和网口相机的使用,所以把实现代码分享出来

首先要在VS里面添加海康威视相机的动态库引用,路径为:

C:\Program Files (x86)\MVS\Development\DotNet\MvCameraControl.Net.dll

1

2

如果没有这个路径,请先安装海康相机的控制软件,也就是MVS

1.创建相机的类

在VS的解决方案资源管理器添加新类,我的类命令为hkws

在代码命名空间部分,引用海康威视的动态库

using MvCamCtrl.NET;

3

2.定义静态类和构造函数

        public static MyCamera MyCamera= new MyCamera();//相机对象
        private static MyCamera.MV_CC_DEVICE_INFO_LIST deviceList;//相机列表
        private static MyCamera.MV_CC_DEVICE_INFO deviceInfo;//相机信息

        private int temp = -1;//用于存储相机方法执行的返回值


        public hkws()//构造函数
        {
            System.GC.Collect();//回收未关闭的HTTP请求
            deviceList = new MyCamera.MV_CC_DEVICE_INFO_LIST();//获取设备列表
        }

3.获取并连接相机列表

        public int connectCamera()//获取并连接相机
        {
            string DeviceName;
            temp = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref deviceList);
            if (temp != MyCamera.MV_OK)
                return -1;


            if (deviceList.nDeviceNum <= 0)
            {
                MessageBox.Show("获取设备列表失败,请检查是否与电脑连接");
                return -1;
            }

            for (int i=0;i<deviceList.nDeviceNum;i++)
            {
                deviceInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(deviceList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO));

                if(deviceInfo.nTLayerType==MyCamera.MV_GIGE_DEVICE)//通过网口获取设备
                {
                    IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(deviceInfo.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 != "")
                    {
                        MessageBox.Show("GigE: " + gigeInfo.chUserDefinedName + " (" + gigeInfo.chSerialNumber + ")");
                    }
                    else
                    {
                        MessageBox.Show("GigE: " + gigeInfo.chManufacturerName + " " + gigeInfo.chModelName + " (" + gigeInfo.chSerialNumber + ")");
                    }
                }
                else if(deviceInfo.nTLayerType==MyCamera.MV_USB_DEVICE)//通过USB口获取设备
                {
                    IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(deviceInfo.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 != "")
                    {
                        MessageBox.Show("USB: " + usbInfo.chUserDefinedName + " (" + usbInfo.chSerialNumber + ")");
                    }
                    else
                    {
                        MessageBox.Show("USB: " + usbInfo.chManufacturerName + " " + usbInfo.chModelName + " (" + usbInfo.chSerialNumber + ")");
                    }
                }


                temp = MyCamera.MV_CC_CreateDevice_NET(ref deviceInfo);

                if (MyCamera.MV_OK != temp)
                {
                    MessageBox.Show("创建相机失败");
                    return -1;
                }
                temp = MyCamera.MV_CC_OpenDevice_NET();
                if (MyCamera.MV_OK != temp)
                {
                    MessageBox.Show("打开相机失败");
                    return -1;
                }

            }

            return 0;
        }

4.关闭相机

/// <summary>
        /// 关闭相机
        /// </summary>
        /// <returns></returns>
        public int closeCamera()
        {
            stopCamera();
            temp=MyCamera.MV_CC_CloseDevice_NET();
            if(MyCamera.MV_OK!=temp)
            {
                MessageBox.Show("关闭相机失败");
                return -1;
            }
            temp=MyCamera.MV_CC_DestroyDevice_NET();
            if (MyCamera.MV_OK != temp)
            {
                MessageBox.Show("销毁相机失败");
                return -1;
            }
            return 0;
        }

5.采集的开始与停止

        //开始采集
        public int startCamera()
        {
            int temp = MyCamera.MV_CC_StartGrabbing_NET();
            if (MyCamera.MV_OK != temp)
                return -1;
            return 0;
        }

        //停止采集
        public int stopCamera()
        {
            int temp = MyCamera.MV_CC_StopGrabbing_NET();
            if (MyCamera.MV_OK != temp)
                return -1;
            return 0;
        }

6.设置触发模式

       public void SoftTrigger(uint id)
        {
            MyCamera.MV_CC_SetEnumValue_NET("TriggerSource", id);//7为软触发

            // ch:触发源选择:0 - Line0
            //           1 - Line1;
            //           2 - Line2;
            //           3 - Line3;
            //           4 - Counter;
            //           7 - Software;
        }

7.触发软触发拍照及模式设置

        public int Trigger()//软触发拍照
        {
           temp = MyCamera.MV_CC_SetCommandValue_NET("TriggerSoftware");
            if (MyCamera.MV_OK != temp)
            {
                return -1;
            }
            return 0;
        }

        public int Mode(uint id)//模式设置 0为连续 1为触发
        {
            temp = MyCamera.MV_CC_SetEnumValue_NET("TriggerMode", id);
            if (MyCamera.MV_OK != temp)
            {
                return -1;
            }
            return 0;
        }

8.将图片显示到某个句柄上

        public int displayCamera(IntPtr hwd)//显示照片
        {

            int temp = MyCamera.MV_CC_Display_NET(hwd);
            if (MyCamera.MV_OK != temp)
                return -1;
            return 0;
        }

9.设置和获取增益、曝光和帧率

        public void getParam(TextBox a,TextBox b,TextBox c)
        {
            MyCamera.MVCC_FLOATVALUE stParam = new MyCamera.MVCC_FLOATVALUE();
            int temp =MyCamera.MV_CC_GetFloatValue_NET("ExposureTime", ref stParam);
            if (MyCamera.MV_OK == temp)
            {
                b.Text = stParam.fCurValue.ToString("F1");
            }
            temp = MyCamera.MV_CC_GetFloatValue_NET("Gain", ref stParam);
            if (MyCamera.MV_OK == temp)
            {
                a.Text = stParam.fCurValue.ToString("F1");
            }
            temp = MyCamera.MV_CC_GetFloatValue_NET("ResultingFrameRate", ref stParam);
            if (MyCamera.MV_OK == temp)
            {
                c.Text = stParam.fCurValue.ToString("F1");
            }
        }

        public void setParam(TextBox a, TextBox b, TextBox c)
        {
            // a 增益  b曝光  c帧率
            int temp;
            MyCamera.MV_CC_SetEnumValue_NET("ExposureAuto", 0);


            temp = MyCamera.MV_CC_SetFloatValue_NET("ExposureTime", float.Parse(b.Text));
            if (temp != MyCamera.MV_OK)
            {
                MessageBox.Show("曝光时间设置失败");
            }

            MyCamera.MV_CC_SetEnumValue_NET("GainAuto", 0);
            temp = MyCamera.MV_CC_SetFloatValue_NET("Gain", float.Parse(a.Text));
            if (temp != MyCamera.MV_OK)
            {
                MessageBox.Show("增益设置失败");
            }

            temp = MyCamera.MV_CC_SetFloatValue_NET("AcquisitionFrameRate", float.Parse(c.Text));
            if (temp != MyCamera.MV_OK)
            {
                MessageBox.Show("帧率设置失败");
            }
        }

10.画出程序界面

4

代码调用部分大家看代码吧

https://wws.lanzous.com/iz4dJk8fdmd
密码:xinf

最后修改:2021 年 01 月 10 日 11 : 25 AM
如果觉得我的文章对你有用,请随意赞赏