海康相机回调函数

工作记录 专栏收录该内容
13 篇文章 0 订阅

记录C#海康相机回调用法,写的有点乱,回头再整理吧。
单个相机,取图输出CogImage8Grey 格式 和 CMvdImage格式。

用法:
先初始化相机 Camera
Camera.Run();
最后通过Camera.CIMG获取CogImage8Grey图像结果(康耐视图像结果)
最后通过Camera.MIMG获取CMvdImage图像结果(海康读码)

public class Camera
        {
            public static MyCamera.cbOutputExdelegate ImageCallback;
            MyCamera.MV_CC_DEVICE_INFO_LIST stDevList = new MyCamera.MV_CC_DEVICE_INFO_LIST();
            MyCamera.MV_CC_DEVICE_INFO stDevInfo;                            // 通用设备信息
            MyCamera device = new MyCamera();
            private double processtime;
            object imageLocker = new object();
            bool acqFinished = false;
            public double processTime
            {
                get { return processtime; }
                set { processtime = value; }
            }
           
            private bool succeed = true;
            Stopwatch Stopwatch = new Stopwatch();
            private CogImage8Grey cImg;

            public CogImage8Grey CIMG
            {
                get { return cImg; }
                set { cImg = value; }
            }

            private VisionDesigner.CMvdImage mImg;

            public VisionDesigner.CMvdImage MIMG
            {
                get { return mImg; }
                set { mImg = value; }
            }

            public bool SUCCEED
            {
                get { return succeed; }
                set
                {
                  succeed = value;
                }
            }

            public  bool Init()
            {
                int nRet;
                // ch:枚举设备 | en:Enum device
                stDevList = new MyCamera.MV_CC_DEVICE_INFO_LIST();
                nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref stDevList);
                if (MyCamera.MV_OK != nRet)
                {
                    return false;
                }
                if (0 == stDevList.nDeviceNum)
                {
                    return false;
                }

               // 通用设备信息

                // ch:打印设备信息 en:Print device info
                for (Int32 i = 0; i < stDevList.nDeviceNum; i++)
                {
                    stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO));

                    if (MyCamera.MV_GIGE_DEVICE == stDevInfo.nTLayerType)
                    {
                        MyCamera.MV_GIGE_DEVICE_INFO stGigEDeviceInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_GIGE_DEVICE_INFO));
                        uint nIp1 = ((stGigEDeviceInfo.nCurrentIp & 0xff000000) >> 24);
                        uint nIp2 = ((stGigEDeviceInfo.nCurrentIp & 0x00ff0000) >> 16);
                        uint nIp3 = ((stGigEDeviceInfo.nCurrentIp & 0x0000ff00) >> 8);
                        uint nIp4 = (stGigEDeviceInfo.nCurrentIp & 0x000000ff);
                    }
                    else if (MyCamera.MV_USB_DEVICE == stDevInfo.nTLayerType)
                    {
                        MyCamera.MV_USB3_DEVICE_INFO stUsb3DeviceInfo = (MyCamera.MV_USB3_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stUsb3VInfo, typeof(MyCamera.MV_USB3_DEVICE_INFO));
                    }
                }

               
                stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[0], typeof(MyCamera.MV_CC_DEVICE_INFO));

                // ch:创建设备 | en:Create device
                nRet = device.MV_CC_CreateDevice_NET(ref stDevInfo);
                if (MyCamera.MV_OK != nRet)
                {
                    return false;
                }

                // ch:打开设备 | en:Open device
                nRet = device.MV_CC_OpenDevice_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    return false;
                }

                // ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
                if (stDevInfo.nTLayerType == MyCamera.MV_GIGE_DEVICE)
                {
                    int nPacketSize = device.MV_CC_GetOptimalPacketSize_NET();
                    if (nPacketSize > 0)
                    {
                        nRet = device.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:设置触发模式为OFF || en:set trigger mode as off
                nRet = device.MV_CC_SetEnumValue_NET("TriggerMode", (uint)MyCamera.MV_CAM_TRIGGER_MODE.MV_TRIGGER_MODE_OFF);
                if (MyCamera.MV_OK != nRet)
                {
                    return false;
                }
              

                // ch:注册回调函数 | en:Register image callback
                ImageCallback = new MyCamera.cbOutputExdelegate(ImageCallbackFunc);
                nRet = device.MV_CC_RegisterImageCallBackEx_NET(ImageCallback, IntPtr.Zero);
                if (MyCamera.MV_OK != nRet)
                {
                    return false;
                }
                return true;
            }

            private void ImageCallbackFunc(IntPtr pData, ref MyCamera.MV_FRAME_OUT_INFO_EX pFrameInfo, IntPtr pUser)
            {
                int width = pFrameInfo.nWidth;
                int height = pFrameInfo.nHeight;
                byte[] SrcData = new byte[pFrameInfo.nHeight * pFrameInfo.nWidth];//图像数据
                Marshal.Copy(pData, SrcData, 0, (int)pFrameInfo.nFrameLen);
                CMvdImage tempImage = new CMvdImage();
                VisionDesigner.MVD_IMAGE_DATA_INFO stData = new MVD_IMAGE_DATA_INFO();
                stData.stDataChannel[0].arrDataBytes = SrcData;
                stData.stDataChannel[0].nLen = pFrameInfo.nFrameLen;
                stData.stDataChannel[0].nRowStep = pFrameInfo.nWidth;
                stData.stDataChannel[0].nSize = (uint)(pFrameInfo.nHeight * pFrameInfo.nWidth);
                tempImage.InitImage((uint)pFrameInfo.nWidth, (uint)pFrameInfo.nHeight, VisionDesigner.MVD_PIXEL_FORMAT.MVD_PIXEL_MONO_08, stData);
                mImg = tempImage;
                CogImage8Grey cImage = null;
                CogImage8Root cogImage8Root = new CogImage8Root();
                cogImage8Root.Initialize(width, height, pData, width, null);
                CogImage8Grey cogImage8Grey = new CogImage8Grey();
                cogImage8Grey.SetRoot(cogImage8Root);
                cImg = cogImage8Grey;
                lock (imageLocker)
                {
                    acqFinished = true;
                    Monitor.Pulse(imageLocker); //完成,通知等待队列,告知已完,执行下一个。
                }
                Stopwatch.Stop();
                processtime = Math.Round(Stopwatch.Elapsed.TotalMilliseconds,2);
                Stopwatch.Reset();
                device.MV_CC_StopGrabbing_NET();
            }

            public void Run()
            {
               
               int nRet = device.MV_CC_StartGrabbing_NET();
                Stopwatch.Start();
                if (MyCamera.MV_OK != nRet)
                {
                    succeed = false;
                }
                lock (imageLocker)
                {
                    while (!acqFinished)
                    {
                        Monitor.Wait(imageLocker);
                    }
                }
                acqFinished = false;
            }
            public void closeCamera()
            {
               int nRet = device.MV_CC_CloseDevice_NET();
            }
         
            public void setExposure(float exposure)
            {
                globalClass.device.MV_CC_SetEnumValue_NET("ExposureAuto", 0);
                int nRet = device.MV_CC_SetFloatValue_NET("ExposureTime", exposure);
                if (nRet != MyCamera.MV_OK)
                {
                   // ShowErrorTip("设置曝光失败!!");
                }
            }
            public float getExposure()
            {
                try
                {
                    MyCamera.MVCC_FLOATVALUE stParam = new MyCamera.MVCC_FLOATVALUE();
                    int nRet = device.MV_CC_GetFloatValue_NET("ExposureTime", ref stParam);
                    if (MyCamera.MV_OK == nRet)
                    {
                        return stParam.fCurValue;
                    }
                    return 0;

                }
                catch (Exception)
                {
                    return 0;
                }

            }
        }
  • 2
    点赞
  • 3
    评论
  • 3
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值