Example #1
0
        public short GetExposureRange(out int minValue, out int maxValue)
        {
            minValue = 0;
            maxValue = 10000;
            try
            {
                MyCamera.MVCC_INTVALUE nValue = new MyCamera.MVCC_INTVALUE();
                int nRet = this.camera.MV_CC_GetIntValue_NET("AutoExposureTimeLowerLimit", ref nValue);
                if (nRet != MyCamera.MV_OK)
                {
                    return(-1);
                }
                minValue = (int)nValue.nMin;
                maxValue = (int)nValue.nMax;
                if (maxValue > 10000)
                {
                    maxValue = 10000;
                }

                return(0);
            }
            catch
            {
                return(-1);
            }
        }
Example #2
0
        public short Init()
        {
            try
            {
                MyCamera.MVCC_FLOATVALUE stParam = new MyCamera.MVCC_FLOATVALUE();
                camera.MV_CC_GetFloatValue_NET("ResultingFrameRate", ref stParam);
                this.FrameRate = stParam.fCurValue;

                MyCamera.MVCC_INTVALUE nValue = new MyCamera.MVCC_INTVALUE();
                camera.MV_CC_GetIntValue_NET("Width", ref nValue);
                this.ImageWidth = (int)nValue.nCurValue;

                camera.MV_CC_GetIntValue_NET("Height", ref nValue);
                this.ImageHeight = (int)nValue.nCurValue;

                if (this.ImageWidth <= 0)
                {
                    this.ImageWidth = 1280;
                }
                if (this.ImageHeight <= 0)
                {
                    this.ImageHeight = 960;
                }

                return(0);
            }
            catch
            {
                return(-1);
            }
        }
Example #3
0
        private IDictionary <ECameraParameter, CameraParameter> GetParameterDictionary()
        {
            var dictionary   = new Dictionary <ECameraParameter, CameraParameter>();
            var width        = new MyCamera.MVCC_INTVALUE();
            var height       = new MyCamera.MVCC_INTVALUE();
            var offsetX      = new MyCamera.MVCC_INTVALUE();
            var offsetY      = new MyCamera.MVCC_INTVALUE();
            var exposure     = new MyCamera.MVCC_FLOATVALUE();
            var gain         = new MyCamera.MVCC_FLOATVALUE();
            var frameRate    = new MyCamera.MVCC_FLOATVALUE();
            var triggerDelay = new MyCamera.MVCC_FLOATVALUE();

            _device.MV_CC_GetWidth_NET(ref width);
            _device.MV_CC_GetHeight_NET(ref height);
            _device.MV_CC_GetAOIoffsetX_NET(ref offsetX);
            _device.MV_CC_GetAOIoffsetY_NET(ref offsetY);
            _device.MV_CC_GetExposureTime_NET(ref exposure);
            _device.MV_CC_GetGain_NET(ref gain);
            _device.MV_CC_GetFrameRate_NET(ref frameRate);
            _device.MV_CC_GetTriggerDelay_NET(ref triggerDelay);

            dictionary[ECameraParameter.Width]        = new CameraParameter(width.nCurValue, width.nMin, width.nMax);
            dictionary[ECameraParameter.Height]       = new CameraParameter(height.nCurValue, height.nMin, height.nMax);
            dictionary[ECameraParameter.OffsetX]      = new CameraParameter(offsetX.nCurValue, offsetX.nMin, offsetX.nMax);
            dictionary[ECameraParameter.OffsetY]      = new CameraParameter(offsetY.nCurValue, offsetY.nMin, offsetY.nMax);
            dictionary[ECameraParameter.Exposure]     = new CameraParameter(exposure.fCurValue, exposure.fMin, exposure.fMax);
            dictionary[ECameraParameter.Gain]         = new CameraParameter(gain.fCurValue, gain.fMin, gain.fMax);
            dictionary[ECameraParameter.FrameRate]    = new CameraParameter(frameRate.fCurValue, frameRate.fMin, frameRate.fMax);
            dictionary[ECameraParameter.TriggerDelay] = new CameraParameter(triggerDelay.fCurValue, triggerDelay.fMin, triggerDelay.fMax);

            return(dictionary);
        }
Example #4
0
        public void SetImageROI()
        {
            try
            {
                MyCamera.MVCC_INTVALUE pstValue = new MyCamera.MVCC_INTVALUE();
                nRet = getonecamera.MV_CC_GetWidth_NET(ref pstValue);

                if ((uint)ImageWidth > pstValue.nMin && (uint)ImageWidth < pstValue.nMax)
                {
                    nRet = getonecamera.MV_CC_SetWidth_NET((uint)ImageWidth);
                    if (nRet != MyCamera.MV_OK)
                    {
                        Console.WriteLine("Set ImageWidth failed!");
                    }
                }
                else
                {
                    nRet = getonecamera.MV_CC_SetWidth_NET(pstValue.nMax);
                    if (nRet != MyCamera.MV_OK)
                    {
                        Console.WriteLine("Set ImageWidth failed!");
                    }
                }

                nRet = getonecamera.MV_CC_GetHeight_NET(ref pstValue);
                if ((uint)ImageHeight > pstValue.nMin && (uint)ImageHeight < pstValue.nMax)
                {
                    nRet = getonecamera.MV_CC_SetHeight_NET((uint)ImageHeight);
                    if (nRet != MyCamera.MV_OK)
                    {
                        Console.WriteLine("Set ImageWidth failed!");
                    }
                }
                else
                {
                    nRet = getonecamera.MV_CC_SetHeight_NET(pstValue.nMax);
                    if (nRet != MyCamera.MV_OK)
                    {
                        Console.WriteLine("Set ImageWidth failed!");
                    }
                }

                nRet = getonecamera.MV_CC_SetAOIoffsetX_NET((uint)ImageoffsetX);
                if (nRet != MyCamera.MV_OK)
                {
                    Console.WriteLine("Set ImageoffsetX failed!");
                }
                nRet = getonecamera.MV_CC_SetAOIoffsetY_NET((uint)ImageoffsetY);
                if (nRet != MyCamera.MV_OK)
                {
                    Console.WriteLine("Set ImageoffsetY failed!");
                }
            }
            catch (Exception)
            {
                Console.WriteLine("Set ImageoffsetY  or ImageoffsetX or ImageHeight or ImageWidth failed!");
            }
        }
Example #5
0
 public override long GetMaxImageOffsetX()
 {
     MyCamera.MVCC_INTVALUE mVCC_INTVALUE = new MyCamera.MVCC_INTVALUE();
     if (hikvisionCamera.MV_CC_GetAOIoffsetX_NET(ref mVCC_INTVALUE) == MyCamera.MV_OK)
     {
         return(mVCC_INTVALUE.nMax);
     }
     return(0);
 }
        private void ImageCallBack_2(IntPtr m_BufForDriver, ref MyCamera.MV_FRAME_OUT_INFO_EX pFrameInfo, IntPtr pUser)
        {
            MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
            int nRet = cameraArr1[0].MV_CC_GetIntValue_NET("PayloadSize", ref stParam);

            if (MyCamera.MV_OK != nRet)
            {
                MessageBox.Show("Get PayloadSize failed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            UInt32 nPayloadSize = stParam.nCurValue;

            if (nPayloadSize > m_nBufSizeForDriver)
            {
                if (m_BufForDriver != IntPtr.Zero)
                {
                    Marshal.Release(m_BufForDriver);
                }
                m_nBufSizeForDriver = nPayloadSize;
                m_BufForDriver      = Marshal.AllocHGlobal((Int32)m_nBufSizeForDriver);
            }
            if (m_BufForDriver == IntPtr.Zero)
            {
                MessageBox.Show("采集失败,请重新连接设备", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            // 将海康数据类型转为Mat
            nRet = cameraArr1[0].MV_CC_GetOneFrameTimeout_NET(m_BufForDriver, nPayloadSize, ref pFrameInfo, 1000); // m_BufForDriver为图像数据接收指针
            //pTemp = m_BufForDriver;
            byte[] byteImage = new byte[pFrameInfo.nHeight * pFrameInfo.nWidth];
            Marshal.Copy(m_BufForDriver, byteImage, 0, pFrameInfo.nHeight * pFrameInfo.nWidth);
            Mat matImage = new Mat(pFrameInfo.nHeight, pFrameInfo.nWidth, MatType.CV_8UC1, byteImage);
            // 单通道图像转为三通道
            Mat matImageNew = new Mat();

            Cv2.CvtColor(matImage, matImageNew, ColorConversionCodes.GRAY2RGB);
            Bitmap bitmap = matImageNew.ToBitmap();  // Mat转为Bitmap
                                                     // 是否进行推理
            DeepLearning deepLearning = new DeepLearning();

            if (isInference2)
            {
                bitmap = deepLearning.Inference(model2, modelType, bitmap);
            }
            if (pictureBox2.InvokeRequired)  // 当一个控件的InvokeRequired属性值为真时,说明有一个创建它以外的线程想访问它
            {
                UpdateUI update = delegate { pictureBox2.Image = bitmap; };
                pictureBox2.BeginInvoke(update);
            }
            else
            {
                pictureBox2.Image = bitmap;
            }
        }
Example #7
0
        /****************************************************************************
        * @fn           GetIntValue
        * @brief        获取Int型参数值
        * @param        strKey                IN        参数键值,具体键值名称参考HikCameraNode.xls文档
        * @param        pnValue               OUT       返回值
        * @return       成功:0;错误:-1
        ****************************************************************************/
        public int GetIntValue(string strKey, ref UInt32 pnValue)
        {
            MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
            int nRet = m_pCSI.MV_CC_GetIntValue_NET(strKey, ref stParam);

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

            pnValue = stParam.nCurValue;

            return(CO_OK);
        }
Example #8
0
        public void ReceiveThreadProcess()
        {
            MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
            int nRet = m_MyCamera.MV_CC_GetIntValue_NET("PayloadSize", ref stParam);

            if (MyCamera.MV_OK != nRet)
            {
                ShowErrorMsg("Get PayloadSize failed", nRet);
                return;
            }

            UInt32 nPayloadSize = stParam.nCurValue;

            if (nPayloadSize > m_nGrabBufSize)
            {
                m_nGrabBufSize = nPayloadSize;
                m_pGrabBuf     = Marshal.AllocHGlobal((Int32)m_nGrabBufSize);
            }

            if (m_pGrabBuf == IntPtr.Zero)
            {
                return;
            }

            MyCamera.MV_FRAME_OUT_INFO_EX  stFrameInfo   = new MyCamera.MV_FRAME_OUT_INFO_EX();
            MyCamera.MV_DISPLAY_FRAME_INFO stDisplayInfo = new MyCamera.MV_DISPLAY_FRAME_INFO();

            while (m_bGrabbing)
            {
                nRet = m_MyCamera.MV_CC_GetOneFrameTimeout_NET(m_pGrabBuf, nPayloadSize, ref stFrameInfo, 1000);
                if (nRet == MyCamera.MV_OK)
                {
                    stDisplayInfo.hWnd        = pictureBox1.Handle;
                    stDisplayInfo.pData       = m_pGrabBuf;
                    stDisplayInfo.nDataLen    = stFrameInfo.nFrameLen;
                    stDisplayInfo.nWidth      = stFrameInfo.nWidth;
                    stDisplayInfo.nHeight     = stFrameInfo.nHeight;
                    stDisplayInfo.enPixelType = stFrameInfo.enPixelType;
                    m_MyCamera.MV_CC_DisplayOneFrame_NET(ref stDisplayInfo);
                }
                else
                {
                    if (bnTriggerMode.Checked)
                    {
                        Thread.Sleep(5);
                    }
                }
            }
        }
Example #9
0
        /// <summary>实时抓拍一张图片</summary>
        public int GrabOne(out IJFImage img, int timeoutMilSeconds = -1)
        {
            img = null;
            if (!IsDeviceOpen)
            {
                return((int)ErrorDef.NotOpen);
            }
            if (!IsGrabbing)
            {
                return((int)ErrorDef.NotGrabbing);
            }
            JFCmrTrigMode currTM = JFCmrTrigMode.disable;

            GetTrigMode(out currTM);
            if (currTM != JFCmrTrigMode.disable)
            {
                return((int)ErrorDef.TrigModeUnMatch);
            }

            int err = 0;

            if (_payloadSize <= 0)
            {
                MyCamera.MVCC_INTVALUE stVal = new MyCamera.MVCC_INTVALUE();
                err = _hikCmr.MV_CC_GetIntValue_NET("PayloadSize", ref stVal);
                if (MyCamera.MV_OK != err)
                {
                    return((int)ErrorDef.InvokeFailed);
                }
                else
                {
                    _payloadSize = (int)stVal.nCurValue;
                }
            }

            byte[] dataBytes = new byte[_payloadSize];
            IntPtr ptr       = Marshal.UnsafeAddrOfPinnedArrayElement(dataBytes, 0);

            MyCamera.MV_FRAME_OUT_INFO_EX frameInfo = new MyCamera.MV_FRAME_OUT_INFO_EX();
            err = _hikCmr.MV_CC_GetOneFrameTimeout_NET(ptr, (uint)_payloadSize, ref frameInfo, timeoutMilSeconds);
            if (err != MyCamera.MV_OK)
            {
                return((int)ErrorDef.InvokeFailed);
            }
            img = new JFImage_Hik(dataBytes, frameInfo, _hikCmr);
            return((int)ErrorDef.Success);
        }
Example #10
0
        public override HObject SnapShot()
        {
            lock (m_lock)
            {
                HObject _image = new HObject();
                MyCamera.MV_FRAME_OUT_INFO_EX FrameInfo = new MyCamera.MV_FRAME_OUT_INFO_EX();
                IntPtr pBufForDriver    = IntPtr.Zero;
                IntPtr pBufForSaveImage = IntPtr.Zero;
                try
                {
                    // ch:获取包大小 || en: Get Payload Size
                    MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
                    int nRet = m_pMyCamera.MV_CC_GetIntValue_NET("PayloadSize", ref stParam);
                    if (MyCamera.MV_OK != nRet)
                    {
                        Console.WriteLine("Get PayloadSize failed:{0:x8}", nRet);
                    }
                    UInt32 nPayloadSize = stParam.nCurValue;

                    int nCount = 0;
                    pBufForDriver = Marshal.AllocHGlobal((int)nPayloadSize);



                    nRet = m_pMyCamera.MV_CC_GetOneFrameTimeout_NET(pBufForDriver, nPayloadSize, ref FrameInfo, 1000);
                    // ch:获取一帧图像 | en:Get one image
                    if (MyCamera.MV_OK == nRet)
                    {
                        _image = GetImage(pBufForDriver, ref FrameInfo);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception($"{m_Cameraname} 获取图像失败," + ex.Message);
                }
                finally
                {
                    Marshal.FreeHGlobal(pBufForDriver);
                    Marshal.FreeHGlobal(pBufForSaveImage);
                }
                return(_image);
            }
        }
Example #11
0
        static void WorkThread()
        {
            // ch:获取包大小 || en: Get Payload Size
            MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
            int nRet = device.MV_CC_GetIntValue_NET("PayloadSize", ref stParam);

            if (MyCamera.MV_OK != nRet)
            {
                Console.WriteLine("Get PayloadSize failed:{0:x8}", nRet);
                return;
            }
            UInt32 nPayloadSize = stParam.nCurValue;

            IntPtr pBufForDriver = Marshal.AllocHGlobal((int)nPayloadSize);

            MyCamera.MV_FRAME_OUT_INFO_EX FrameInfo = new MyCamera.MV_FRAME_OUT_INFO_EX();
            while (true)
            {
                nRet = device.MV_CC_GetOneFrameTimeout_NET(pBufForDriver, nPayloadSize, ref FrameInfo, 1000);
                // ch:获取一帧图像 | en:Get image
                if (MyCamera.MV_OK == nRet)
                {
                    Console.WriteLine("Get One Frame:" + "Width[" + Convert.ToString(FrameInfo.nWidth) + "] , Height[" + Convert.ToString(FrameInfo.nHeight)
                                      + "] , FrameNum[" + Convert.ToString(FrameInfo.nFrameNum) + "]");
                }
                else
                {
                    Console.WriteLine("No data:{0:x8}", nRet);
                }
                if (g_bExit)
                {
                    break;
                }
            }
            Marshal.FreeHGlobal(pBufForDriver);
            return;
        }
Example #12
0
        static void Main(string[] args)
        {
            int      nRet   = MyCamera.MV_OK;
            MyCamera device = new MyCamera();

            do
            {
                // ch:枚举设备 | en:Enum device
                MyCamera.MV_CC_DEVICE_INFO_LIST 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)
                {
                    Console.WriteLine("Enum device failed:{0:x8}", nRet);
                    break;
                }
                Console.WriteLine("Enum device count : " + Convert.ToString(stDevList.nDeviceNum));
                if (0 == stDevList.nDeviceNum)
                {
                    break;
                }

                MyCamera.MV_CC_DEVICE_INFO stDevInfo;                            // 通用设备信息

                // 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);
                        Console.WriteLine("\n" + i.ToString() + ": [GigE] User Define Name : " + stGigEDeviceInfo.chUserDefinedName);
                        Console.WriteLine("device IP :" + nIp1 + "." + nIp2 + "." + nIp3 + "." + nIp4);
                    }
                    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));
                        Console.WriteLine("\n" + i.ToString() + ": [U3V] User Define Name : " + stUsb3DeviceInfo.chUserDefinedName);
                        Console.WriteLine("\n Serial Number : " + stUsb3DeviceInfo.chSerialNumber);
                        Console.WriteLine("\n Device Number : " + stUsb3DeviceInfo.nDeviceNumber);
                    }
                }

                Int32 nDevIndex = 0;
                Console.Write("\nPlease input index (0 -- {0:d}) : ", stDevList.nDeviceNum - 1);
                try
                {
                    nDevIndex = Convert.ToInt32(Console.ReadLine());
                }
                catch
                {
                    Console.Write("Invalid Input!\n");
                    break;
                }

                if (nDevIndex > stDevList.nDeviceNum - 1 || nDevIndex < 0)
                {
                    Console.Write("Input Error!\n");
                    break;
                }
                stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[nDevIndex], typeof(MyCamera.MV_CC_DEVICE_INFO));

                // ch:创建设备 | en:Create device
                nRet = device.MV_CC_CreateDevice_NET(ref stDevInfo);
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Create device failed:{0:x8}", nRet);
                    break;
                }

                // ch:打开设备 | en:Open device
                nRet = device.MV_CC_OpenDevice_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Open device failed:{0:x8}", nRet);
                    break;
                }

                // ch:判断设备是否是设置的3D格式 | en:Judge Whether the device is set to 3D format
                MyCamera.MVCC_ENUMVALUE EnumValue = new MyCamera.MVCC_ENUMVALUE();
                nRet = device.MV_CC_GetEnumValue_NET("PixelFormat", ref EnumValue);
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Get the Camera format fail:{0:x8}", nRet);
                    break;
                }

                MyCamera.MvGvspPixelType ePixelFormat = (MyCamera.MvGvspPixelType)EnumValue.nCurValue;
                switch (ePixelFormat)
                {
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_Coord3D_ABC32:
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_Coord3D_ABC32f:
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_Coord3D_AB32:
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_Coord3D_AB32f:
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_Coord3D_AC32:
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_Coord3D_AC32f:
                {
                    nRet = MyCamera.MV_OK;
                    break;
                }

                default:
                {
                    nRet = MyCamera.MV_E_SUPPORT;
                    break;
                }
                }
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("This is not a supported 3D format!");
                    break;
                }

                // 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", Convert.ToUInt32(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:Get Access mode of trigger mode
                MyCamera.MV_XML_AccessMode pAccessMode = MyCamera.MV_XML_AccessMode.AM_NI;
                if (MyCamera.MV_OK != device.MV_XML_GetNodeAccessMode_NET("TriggerMode", ref pAccessMode))
                {
                    Console.WriteLine("Get Access mode of trigger mode fail! nRet [0x%x]\n", nRet);
                }
                else
                {
                    // ch:设置触发模式为off || en:set trigger mode as off
                    if (MyCamera.MV_OK != device.MV_CC_SetEnumValue_NET("TriggerMode", 0))
                    {
                        Console.WriteLine("Set TriggerMode failed!");
                        break;
                    }
                }

                // ch:获取包大小 || en: Get Payload Size
                MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
                nRet = device.MV_CC_GetIntValue_NET("PayloadSize", ref stParam);
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Get PayloadSize failed:{0:x8}", nRet);
                    break;
                }
                UInt32 nPayloadSize = stParam.nCurValue;

                // ch:开启抓图 | en:start grab
                nRet = device.MV_CC_StartGrabbing_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Start grabbing failed:{0:x8}", nRet);
                    break;
                }

                uint   nImageNum     = 100;
                byte[] bSaveImageBuf = null;

                try
                {
                    // ch:申请足够大的缓存,用于保存获取到的图像
                    bSaveImageBuf = new byte[nPayloadSize * nImageNum];
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Malloc  Save buffer fail!\n");
                    break;
                }

                uint nSaveImageSize = nPayloadSize * nImageNum;

                // ch:已获取的总图片大小
                uint nSaveDataLen = 0;

                MyCamera.MV_FRAME_OUT stOutFrame = new MyCamera.MV_FRAME_OUT();
                for (uint i = 0; i < nImageNum; i++)
                {
                    nRet = device.MV_CC_GetImageBuffer_NET(ref stOutFrame, 1000);
                    if (nRet == MyCamera.MV_OK)
                    {
                        Console.WriteLine("Get One Frame:" + "Width[" + Convert.ToString(stOutFrame.stFrameInfo.nWidth) + "] , Height[" + Convert.ToString(stOutFrame.stFrameInfo.nHeight)
                                          + "] , FrameNum[" + Convert.ToString(stOutFrame.stFrameInfo.nFrameNum) + "]");

                        if (nSaveImageSize > (nSaveDataLen + stOutFrame.stFrameInfo.nFrameLen))
                        {
                            // ch:将图像拷贝到pSaveImageBuf | Copy one frame of image to the buffer named pSaveImageBuf
                            Marshal.Copy(stOutFrame.pBufAddr, bSaveImageBuf, Convert.ToInt32(nSaveDataLen), Convert.ToInt32(stOutFrame.stFrameInfo.nFrameLen));
                            nSaveDataLen += stOutFrame.stFrameInfo.nFrameLen;
                        }

                        nRet = device.MV_CC_FreeImageBuffer_NET(ref stOutFrame);
                        if (nRet != MyCamera.MV_OK)
                        {
                            Console.WriteLine("Free Image Buffer fail:{0:x8}", nRet);
                        }
                    }
                    else
                    {
                        Console.WriteLine("No data:{0:x8}", nRet);
                    }
                }

                MyCamera.MV_SAVE_POINT_CLOUD_PARAM stSavePoCloudPar = new MyCamera.MV_SAVE_POINT_CLOUD_PARAM();

                stSavePoCloudPar.nLinePntNum = stOutFrame.stFrameInfo.nWidth;
                stSavePoCloudPar.nLineNum    = stOutFrame.stFrameInfo.nHeight * nImageNum;

                byte[] bDstImageBuf  = new byte[stSavePoCloudPar.nLineNum * stSavePoCloudPar.nLinePntNum * (16 * 3 + 4) + 2048];
                uint   nDstImageSize = stSavePoCloudPar.nLineNum * stSavePoCloudPar.nLinePntNum * (16 * 3 + 4) + 2048;

                stSavePoCloudPar.enPointCloudFileType = MyCamera.MV_SAVE_POINT_CLOUD_FILE_TYPE.MV_PointCloudFile_PLY;
                stSavePoCloudPar.enSrcPixelType       = stOutFrame.stFrameInfo.enPixelType;
                stSavePoCloudPar.nSrcDataLen          = nSaveDataLen;

                GCHandle hSrcData = GCHandle.Alloc(bSaveImageBuf, GCHandleType.Pinned);
                stSavePoCloudPar.pSrcData = hSrcData.AddrOfPinnedObject();

                GCHandle hDstData = GCHandle.Alloc(bDstImageBuf, GCHandleType.Pinned);
                stSavePoCloudPar.pDstBuf = hDstData.AddrOfPinnedObject();

                stSavePoCloudPar.nDstBufSize = nDstImageSize;

                //Save point cloud data
                nRet = device.MV_CC_SavePointCloudData_NET(ref stSavePoCloudPar);
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Save point cloud data fail:{0:x8}", nRet);
                    break;
                }

                FileStream file = new FileStream("PointCloudData.ply", FileMode.Create, FileAccess.Write);
                file.Write(bDstImageBuf, 0, Convert.ToInt32(stSavePoCloudPar.nDstBufLen));
                file.Close();
                Console.WriteLine("Save point cloud data succeed");

                hSrcData.Free();
                hDstData.Free();

                // ch:停止抓图 | en:Stop grab image
                nRet = device.MV_CC_StopGrabbing_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Stop grabbing failed{0:x8}", nRet);
                    break;
                }

                // ch:关闭设备 | en:Close device
                nRet = device.MV_CC_CloseDevice_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Close device failed{0:x8}", nRet);
                    break;
                }

                // ch:销毁设备 | en:Destroy device
                nRet = device.MV_CC_DestroyDevice_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Destroy device failed:{0:x8}", nRet);
                    break;
                }
            } while (false);

            if (MyCamera.MV_OK != nRet)
            {
                // ch:销毁设备 | en:Destroy device
                nRet = device.MV_CC_DestroyDevice_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Destroy device failed:{0:x8}", nRet);
                }
            }

            Console.WriteLine("Press enter to exit");
            Console.ReadKey();
        }
Example #13
0
        //初始化设备
        static public int InitCamera(out string CameraNames)
        {
            int CmaeraNum = 0;

            CameraNames = "";
            int nRet = MyCamera.MV_OK;

            //   MyCamera device = new MyCamera();
            // ch:枚举设备 | en:Enum device
            MyCamera.MV_CC_DEVICE_INFO_LIST 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)
            {
                Console.WriteLine("Enum device failed:{0:x8}", nRet);
            }
            Console.WriteLine("Enum device count : " + Convert.ToString(stDevList.nDeviceNum));
            if (0 == stDevList.nDeviceNum)
            {
                return(0);
            }

            MyCamera.MV_CC_DEVICE_INFO stDevInfo;                            // 通用设备信息

            int DevCounter = 0;
            List <MyCamera.MV_CC_DEVICE_INFO> DevList = new List <MyCamera.MV_CC_DEVICE_INFO>();

            // 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);
                    Console.WriteLine("\n" + i.ToString() + ": [GigE] User Define Name : " + stGigEDeviceInfo.chUserDefinedName);
                    Console.WriteLine("device IP :" + nIp1 + "." + nIp2 + "." + nIp3 + "." + nIp4);
                    DevList.Add(stDevInfo);
                    DevCounter++;
                }
                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));
                    Console.WriteLine("\n" + i.ToString() + ": [U3V] User Define Name : " + stUsb3DeviceInfo.chUserDefinedName);
                    Console.WriteLine("\n Serial Number : " + stUsb3DeviceInfo.chSerialNumber);
                    Console.WriteLine("\n Device Number : " + stUsb3DeviceInfo.nDeviceNumber);
                }
            }

            Console.Write("\nPlease input index (0 -- {0:d}) : ", stDevList.nDeviceNum - 1);
            Int32 nDevIndex = Convert.ToInt32(Console.ReadLine());

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

            DevList.Sort((a, b) => ((MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(a.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_GIGE_DEVICE_INFO))).chUserDefinedName.CompareTo(
                             ((MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(b.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_GIGE_DEVICE_INFO))).chUserDefinedName));

            foreach (var b in DevList)
            {
                CameraNames += ((MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(b.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_GIGE_DEVICE_INFO))).chUserDefinedName + ",";
            }

            int minDevCounter = DevCounter < 4 ? DevCounter : 4;

            for (int i = 0; i < minDevCounter; i++)
            {
                stDevInfo = DevList[i];
                nRet      = devices[i].MV_CC_CreateDevice_NET(ref stDevInfo);
                if (MyCamera.MV_OK != nRet)
                {
                    continue;
                }

                nRet = devices[i].MV_CC_OpenDevice_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    continue;
                }

                // ch:设置触发模式为off || en:set trigger mode as off
                if (MyCamera.MV_OK != devices[i].MV_CC_SetEnumValue_NET("TriggerMode", 0))
                {
                    continue;
                }

                //设置曝光时间
                float fValue = 5000;
                if (MyCamera.MV_OK != devices[i].MV_CC_SetExposureTime_NET(fValue))
                {
                    continue;
                }


                // ch:开启抓图 | en:start grab
                nRet = devices[i].MV_CC_StartGrabbing_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Start grabbing failed:{0:x8}", nRet);
                    continue;
                }

                // ch:获取包大小 || en: Get Payload Size
                MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
                nRet = devices[i].MV_CC_GetIntValue_NET("PayloadSize", ref stParam);
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Get PayloadSize failed:{0:x8}", nRet);
                    continue;
                }
                nPayloadSize[i] = stParam.nCurValue;
                //int nCount = 0;
                pBufForDriver[i] = Marshal.AllocHGlobal((int)nPayloadSize[i]);
                CameraInitOK[i]  = true;
                CmaeraNum++;
            }

            return(CmaeraNum);
        }
Example #14
0
        static void Main(string[] args)
        {
            int      nRet   = MyCamera.MV_OK;
            MyCamera device = new MyCamera();

            do
            {
                // ch:枚举设备 | en:Enum device
                MyCamera.MV_CC_DEVICE_INFO_LIST 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)
                {
                    Console.WriteLine("Enum device failed:{0:x8}", nRet);
                    break;
                }
                Console.WriteLine("Enum device count : " + Convert.ToString(stDevList.nDeviceNum));
                if (0 == stDevList.nDeviceNum)
                {
                    break;
                }

                MyCamera.MV_CC_DEVICE_INFO stDevInfo;                            // 通用设备信息

                // 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);
                        Console.WriteLine("\n" + i.ToString() + ": [GigE] User Define Name : " + stGigEDeviceInfo.chUserDefinedName);
                        Console.WriteLine("device IP :" + nIp1 + "." + nIp2 + "." + nIp3 + "." + nIp4);
                    }
                    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));
                        Console.WriteLine("\n" + i.ToString() + ": [U3V] User Define Name : " + stUsb3DeviceInfo.chUserDefinedName);
                        Console.WriteLine("\n Serial Number : " + stUsb3DeviceInfo.chSerialNumber);
                        Console.WriteLine("\n Device Number : " + stUsb3DeviceInfo.nDeviceNumber);
                    }
                }

                Int32 nDevIndex = 0;
                Console.Write("\nPlease input index (0 -- {0:d}) : ", stDevList.nDeviceNum - 1);
                try
                {
                    nDevIndex = Convert.ToInt32(Console.ReadLine());
                }
                catch
                {
                    Console.Write("Invalid Input!\n");
                    break;
                }

                if (nDevIndex > stDevList.nDeviceNum - 1 || nDevIndex < 0)
                {
                    Console.Write("Input Error!\n");
                    break;
                }
                stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[nDevIndex], typeof(MyCamera.MV_CC_DEVICE_INFO));

                // ch:创建设备 | en:Create device
                nRet = device.MV_CC_CreateDevice_NET(ref stDevInfo);
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Create device failed:{0:x8}", nRet);
                    break;
                }

                // ch:打开设备 | en:Open device
                nRet = device.MV_CC_OpenDevice_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Open device failed:{0:x8}", nRet);
                    break;
                }

                // 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
                if (MyCamera.MV_OK != device.MV_CC_SetEnumValue_NET("TriggerMode", 0))
                {
                    Console.WriteLine("Set TriggerMode failed!");
                    break;
                }

                // ch:获取包大小 || en: Get Payload Size
                MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
                nRet = device.MV_CC_GetIntValue_NET("PayloadSize", ref stParam);
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Get PayloadSize failed:{0:x8}", nRet);
                    break;
                }
                g_nPayloadSize = stParam.nCurValue;

                MyCamera.MV_CC_RECORD_PARAM stRecordPar = new MyCamera.MV_CC_RECORD_PARAM();
                nRet = device.MV_CC_GetIntValue_NET("Width", ref stParam);
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Get Width failed: nRet {0:x8}", nRet);
                    break;
                }
                stRecordPar.nWidth = (ushort)stParam.nCurValue;

                nRet = device.MV_CC_GetIntValue_NET("Height", ref stParam);
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Get Height failed: nRet {0:x8}", nRet);
                    break;
                }
                stRecordPar.nHeight = (ushort)stParam.nCurValue;

                MyCamera.MVCC_ENUMVALUE stEnumValue = new MyCamera.MVCC_ENUMVALUE();
                nRet = device.MV_CC_GetEnumValue_NET("PixelFormat", ref stEnumValue);
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Get Width failed: nRet {0:x8}", nRet);
                    break;
                }
                stRecordPar.enPixelType = (MyCamera.MvGvspPixelType)stEnumValue.nCurValue;

                MyCamera.MVCC_FLOATVALUE stFloatValue = new MyCamera.MVCC_FLOATVALUE();
                nRet = device.MV_CC_GetFloatValue_NET("ResultingFrameRate", ref stFloatValue);
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Get Float value failed: nRet {0:x8}", nRet);
                    break;
                }
                // ch:帧率(大于1/16)fps | en:Frame Rate (>1/16)fps
                stRecordPar.fFrameRate = stFloatValue.fCurValue;
                // ch:码率kbps(128kbps-16Mbps) | en:Bitrate kbps(128kbps-16Mbps)
                stRecordPar.nBitRate = 1000;
                // ch:录像格式(仅支持AVI) | en:Record Format(AVI is only supported)
                stRecordPar.enRecordFmtType = MyCamera.MV_RECORD_FORMAT_TYPE.MV_FormatType_AVI;
                stRecordPar.strFilePath     = "./Recording.avi";
                nRet = device.MV_CC_StartRecord_NET(ref stRecordPar);
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Start Record failed: nRet {0:x8}", nRet);
                    break;
                }

                // ch:开启抓图 | en:start grab
                nRet = device.MV_CC_StartGrabbing_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Start grabbing failed:{0:x8}", nRet);
                    break;
                }

                Thread hReceiveImageThreadHandle = new Thread(ReceiveImageWorkThread);
                hReceiveImageThreadHandle.Start(device);

                Console.WriteLine("Press enter to exit");
                Console.ReadKey();

                g_bExit = true;
                hReceiveImageThreadHandle.Join();

                // ch:停止抓图 | en:Stop grab image
                nRet = device.MV_CC_StopGrabbing_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Stop grabbing failed{0:x8}", nRet);
                    break;
                }

                // ch:停止录像 | en:Stop record
                nRet = device.MV_CC_StopRecord_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Stop Record failed{0:x8}", nRet);
                    break;
                }

                // ch:关闭设备 | en:Close device
                nRet = device.MV_CC_CloseDevice_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Close device failed{0:x8}", nRet);
                    break;
                }

                // ch:销毁设备 | en:Destroy device
                nRet = device.MV_CC_DestroyDevice_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Destroy device failed:{0:x8}", nRet);
                    break;
                }
            } while (false);

            if (MyCamera.MV_OK != nRet)
            {
                // ch:销毁设备 | en:Destroy device
                nRet = device.MV_CC_DestroyDevice_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Destroy device failed:{0:x8}", nRet);
                }
            }

            Console.WriteLine("Press enter to exit");
            Console.ReadKey();
        }
Example #15
0
        // 采集进程
        public void GrabThreadProcess()
        {
            if ((chooseHIK) && (!chooseBasler))
            {
                MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
                int nRet = camera2.MV_CC_GetIntValue_NET("PayloadSize", ref stParam);
                if (MyCamera.MV_OK != nRet)
                {
                    MessageBox.Show("Get PayloadSize failed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                UInt32 nPayloadSize = stParam.nCurValue;
                if (nPayloadSize > m_nBufSizeForDriver)
                {
                    if (m_BufForDriver != IntPtr.Zero)
                    {
                        Marshal.Release(m_BufForDriver);
                    }
                    m_nBufSizeForDriver = nPayloadSize;
                    m_BufForDriver      = Marshal.AllocHGlobal((Int32)m_nBufSizeForDriver);
                }
                if (m_BufForDriver == IntPtr.Zero)
                {
                    return;
                }

                MyCamera.MV_FRAME_OUT_INFO_EX stFrameInfo = new MyCamera.MV_FRAME_OUT_INFO_EX();  // 定义输出帧信息结构体
                //IntPtr pTemp = IntPtr.Zero;

                while (hikCanGrab)
                {
                    // 将海康数据类型转为Mat
                    nRet = camera2.MV_CC_GetOneFrameTimeout_NET(m_BufForDriver, nPayloadSize, ref stFrameInfo, 1000); // m_BufForDriver为图像数据接收指针
                    //pTemp = m_BufForDriver;
                    byte[] byteImage = new byte[stFrameInfo.nHeight * stFrameInfo.nWidth];
                    Marshal.Copy(m_BufForDriver, byteImage, 0, stFrameInfo.nHeight * stFrameInfo.nWidth);
                    Mat matImage = new Mat(stFrameInfo.nHeight, stFrameInfo.nWidth, MatType.CV_8UC1, byteImage);
                    // 单通道图像转为三通道
                    Mat matImageNew = new Mat();
                    Cv2.CvtColor(matImage, matImageNew, ColorConversionCodes.GRAY2RGB);
                    Bitmap bitmap = matImageNew.ToBitmap();  // Mat转为Bitmap
                    // 是否进行推理
                    if (isInference)
                    {
                        bitmap = Inference(bitmap);
                    }
                    if (pictureBox1.InvokeRequired)  // 当一个控件的InvokeRequired属性值为真时,说明有一个创建它以外的线程想访问它
                    {
                        UpdateUI update = delegate { pictureBox1.Image = bitmap; };
                        pictureBox1.BeginInvoke(update);
                    }
                    else
                    {
                        pictureBox1.Image = bitmap;
                    }
                }
            }
            else if ((chooseBasler) && (!chooseHIK))
            {
                while (baslerCanGrab)
                {
                    IGrabResult grabResult;
                    using (grabResult = camera1.StreamGrabber.RetrieveResult(5000, TimeoutHandling.ThrowException))
                    {
                        if (grabResult.GrabSucceeded)
                        {
                            // 四通道RGBA
                            Bitmap bitmap = new Bitmap(grabResult.Width, grabResult.Height, PixelFormat.Format32bppRgb);
                            // 锁定位图的位
                            BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
                            // 将指针放置到位图的缓冲区
                            converter.OutputPixelFormat = PixelType.BGRA8packed;
                            IntPtr ptrBmp = bmpData.Scan0;
                            converter.Convert(ptrBmp, bmpData.Stride * bitmap.Height, grabResult);
                            bitmap.UnlockBits(bmpData);
                            // 是否进行推理
                            if (isInference)
                            {
                                bitmap = Inference(bitmap);
                            }
                            // 禁止跨线程直接访问控件,故invoke到主线程中
                            // 参考:https://bbs.csdn.net/topics/350050105
                            //       https://www.cnblogs.com/lky-learning/p/14025280.html
                            if (pictureBox1.InvokeRequired)  // 当一个控件的InvokeRequired属性值为真时,说明有一个创建它以外的线程想访问它
                            {
                                UpdateUI update = delegate { pictureBox1.Image = bitmap; };
                                pictureBox1.BeginInvoke(update);
                            }
                            else
                            {
                                pictureBox1.Image = bitmap;
                            }
                        }
                    }
                }
            }
        }
Example #16
0
        // Get the value of various feature nodes
        static int GetParameters(ref MyCamera device)
        {
            if (null == device)
            {
                return(MyCamera.MV_E_PARAMETER);
            }

            int nRet = MyCamera.MV_OK;

            // Get value of Integer nodes. Such as, 'width' etc.
            MyCamera.MVCC_INTVALUE stIntVal = new MyCamera.MVCC_INTVALUE();
            nRet = device.MV_CC_GetIntValue_NET("Width", ref stIntVal);
            if (MyCamera.MV_OK != nRet)
            {
                Console.WriteLine("Get width failed:{0:x8}", nRet);
                return(nRet);
            }
            Console.WriteLine("Current Width:{0:d}", stIntVal.nCurValue);

            // Get value of Enum nodes. Such as, 'TriggerMode' etc.
            MyCamera.MVCC_ENUMVALUE stEnumVal = new MyCamera.MVCC_ENUMVALUE();
            nRet = device.MV_CC_GetEnumValue_NET("TriggerMode", ref stEnumVal);
            if (MyCamera.MV_OK != nRet)
            {
                Console.WriteLine("Get Trigger Mode failed:{0:x8}", nRet);
                return(nRet);
            }
            Console.WriteLine("Current TriggerMode:{0:d}", stEnumVal.nCurValue);

            // Get value of float nodes. Such as, 'AcquisitionFrameRate' etc.
            MyCamera.MVCC_FLOATVALUE stFloatVal = new MyCamera.MVCC_FLOATVALUE();
            nRet = device.MV_CC_GetFloatValue_NET("AcquisitionFrameRate", ref stFloatVal);
            if (MyCamera.MV_OK != nRet)
            {
                Console.WriteLine("Get AcquisitionFrameRate failed:{0:x8}", nRet);
                return(nRet);
            }
            Console.WriteLine("Current AcquisitionFrameRate:{0:f}Fps", stFloatVal.fCurValue);

            // Get value of bool nodes. Such as, 'AcquisitionFrameRateEnable' etc.
            bool bBoolVal = false;

            nRet = device.MV_CC_GetBoolValue_NET("AcquisitionFrameRateEnable", ref bBoolVal);
            if (MyCamera.MV_OK != nRet)
            {
                Console.WriteLine("Get AcquisitionFrameRateEnable failed:{0:x8}", nRet);
                return(nRet);
            }
            Console.WriteLine("Current AcquisitionFrameRateEnable:{0:d}", bBoolVal);

            // Get value of String nodes. Such as, 'DeviceUserID' etc.
            MyCamera.MVCC_STRINGVALUE stStrVal = new MyCamera.MVCC_STRINGVALUE();
            nRet = device.MV_CC_GetStringValue_NET("DeviceUserID", ref stStrVal);
            if (MyCamera.MV_OK != nRet)
            {
                Console.WriteLine("Get DeviceUserID failed:{0:x8}", nRet);
                return(nRet);
            }
            Console.WriteLine("Current DeviceUserID:{0:s}", stStrVal.chCurValue);

            return(MyCamera.MV_OK);
        }
Example #17
0
        public int OpenDevices(string CamName)
        {
            if (IsOpen == true)
            {
                return(0);
            }
            foreach (var temp in CameraInfo)
            {
                IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(temp.SpecialInfo.stGigEInfo, 0);
                MyCamera.MV_GIGE_DEVICE_INFO gigeInfo1 = (MyCamera.MV_GIGE_DEVICE_INFO)Marshal.PtrToStructure(buffer, typeof(MyCamera.MV_GIGE_DEVICE_INFO));
                if (gigeInfo1.chUserDefinedName == CamName || gigeInfo1.chSerialNumber == CamName)
                {
                    int nRet = -1;
                    MyCamera.MV_CC_DEVICE_INFO device = temp;

                    bool bRes = MyCamera.MV_CC_IsDeviceAccessible_NET(ref device, 1);
                    if (bRes == false)
                    {
                        return(1);
                    }

                    nRet = m_pMyCamera.MV_CC_CreateDevice_NET(ref device);
                    if (MyCamera.MV_OK != nRet)
                    {
                        return(2);
                    }
                    nRet = m_pMyCamera.MV_CC_OpenDevice_NET();
                    if (MyCamera.MV_OK != nRet)
                    {
                        //MessageBox.Show("Open Device Fail");
                        return(3);
                    }
                    MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
                    // ch:获取包大小 || en: Get Payload Size
                    nRet = m_pMyCamera.MV_CC_GetIntValue_NET("PayloadSize", ref stParam);
                    if (MyCamera.MV_OK != nRet)
                    {
                        MessageBox.Show("Get PayloadSize Fail");
                        return(4);
                    }
                    g_nPayloadSize = stParam.nCurValue;

                    // ch:获取高 || en: Get Height
                    nRet = m_pMyCamera.MV_CC_GetIntValue_NET("Height", ref stParam);
                    if (MyCamera.MV_OK != nRet)
                    {
                        return(5);
                    }
                    uint nHeight = stParam.nCurValue;

                    // ch:获取宽 || en: Get Width
                    nRet = m_pMyCamera.MV_CC_GetIntValue_NET("Width", ref stParam);
                    if (MyCamera.MV_OK != nRet)
                    {
                        return(6);
                    }
                    uint nWidth = stParam.nCurValue;

                    m_pDataForRed   = new byte[nWidth * nHeight];
                    m_pDataForGreen = new byte[nWidth * nHeight];
                    m_pDataForBlue  = new byte[nWidth * nHeight];
                    nRet            = m_pMyCamera.MV_CC_StartGrabbing_NET();
                    if (MyCamera.MV_OK != nRet)
                    {
                        return(7);
                    }
                    IsOpen = true;

                    return(0);
                }
            }
            return(-1);
        }
Example #18
0
        public Bitmap GetFrame()
        {
            int nRet;

            bmp = null;
            MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
            nRet = pCamera.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];

                // Determine the buffer size to save image
                // 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();
            //Get one frame timeout, timeout is 1 sec
            nRet = pCamera.MV_CC_GetOneFrameTimeout_NET(pData, m_nBufSizeForDriver, ref stFrameInfo, 1000);
            if (MyCamera.MV_OK != 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
            {
                return(null);
            }

            IntPtr pImage = Marshal.UnsafeAddrOfPinnedArrayElement(m_pBufForSaveImage, 0);

            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 = pCamera.MV_CC_ConvertPixelType_NET(ref stConverPixelParam);
            if (MyCamera.MV_OK != nRet)
            {
                return(null);
            }

            if (enDstPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8)
            {
                //************************Mono8 转 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;
            }
            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
                {
                    bmp = new Bitmap(stFrameInfo.nWidth, stFrameInfo.nHeight, stFrameInfo.nWidth * 3, PixelFormat.Format24bppRgb, pImage);
                }
                catch
                {
                }
            }
            return(bmp);
        }
Example #19
0
        static void Main(string[] args)
        {
            int      nRet   = MyCamera.MV_OK;
            MyCamera device = new MyCamera();

            do
            {
                // ch:枚举设备 | en:Enum deivce
                MyCamera.MV_CC_DEVICE_INFO_LIST 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)
                {
                    Console.WriteLine("Enum device failed:{0:x8}", nRet);
                    break;
                }
                Console.WriteLine("Enum device count : " + Convert.ToString(stDevList.nDeviceNum));
                if (0 == stDevList.nDeviceNum)
                {
                    break;
                }

                MyCamera.MV_CC_DEVICE_INFO stDevInfo;

                // 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);
                        Console.WriteLine("\n" + i.ToString() + ": [GigE] User Define Name : " + stGigEDeviceInfo.chUserDefinedName);
                        Console.WriteLine("device IP :" + nIp1 + "." + nIp2 + "." + nIp3 + "." + nIp4);
                    }
                    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));
                        Console.WriteLine("\n" + i.ToString() + ": [U3V] User Define Name : " + stUsb3DeviceInfo.chUserDefinedName);
                        Console.WriteLine("\n Serial Number : " + stUsb3DeviceInfo.chSerialNumber);
                        Console.WriteLine("\n Device Number : " + stUsb3DeviceInfo.nDeviceNumber);
                    }
                }

                Int32 nDevIndex = 0;
                Console.Write("\nPlease input index (0 -- {0:d}) : ", stDevList.nDeviceNum - 1);
                try
                {
                    nDevIndex = Convert.ToInt32(Console.ReadLine());
                }
                catch
                {
                    Console.Write("Invalid Input!\n");
                    break;
                }

                if (nDevIndex > stDevList.nDeviceNum - 1 || nDevIndex < 0)
                {
                    Console.Write("Input Error!\n");
                    break;
                }
                stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[nDevIndex], typeof(MyCamera.MV_CC_DEVICE_INFO));

                // ch:创建设备 | en: Create device
                nRet = device.MV_CC_CreateDevice_NET(ref stDevInfo);
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Create device failed:{0:x8}", nRet);
                    break;
                }

                // ch:打开设备 | en:Open device
                nRet = device.MV_CC_OpenDevice_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Open device failed:{0:x8}", nRet);
                    break;
                }

                // 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:获取触发模式的访问模式 | en:Get Access mode of trigger mode
                MyCamera.MV_XML_AccessMode pAccessMode = MyCamera.MV_XML_AccessMode.AM_NI;
                nRet = device.MV_XML_GetNodeAccessMode_NET("TriggerMode", ref pAccessMode);
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Get Access mode of trigger mode fail! nRet [0x%x]\n", nRet);
                }
                else
                {
                    // ch:设置触发模式为off || en:set trigger mode as off
                    nRet = device.MV_CC_SetEnumValue_NET("TriggerMode", 0);
                    if (MyCamera.MV_OK != nRet)
                    {
                        Console.WriteLine("Set TriggerMode failed!");
                        break;
                    }
                }

                // ch:开启抓图 || en: start grab image
                nRet = device.MV_CC_StartGrabbing_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Start grabbing failed:{0:x8}", nRet);
                    break;
                }

                // ch:获取包大小 || en: Get Payload Size
                MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
                nRet = device.MV_CC_GetIntValue_NET("PayloadSize", ref stParam);
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Get PayloadSize failed:{0:x8}", nRet);
                    break;
                }
                UInt32 nPayloadSize = stParam.nCurValue;

                byte[] BufForDriver = new byte[nPayloadSize];
                IntPtr pData        = Marshal.UnsafeAddrOfPinnedArrayElement(BufForDriver, 0);
                MyCamera.MV_FRAME_OUT_INFO_EX stFrameInfo = new MyCamera.MV_FRAME_OUT_INFO_EX();

                HTuple hObjectModel3D = null;
                HTuple hv_X           = new HTuple();
                HTuple hv_Y           = new HTuple();
                HTuple hv_Z           = new HTuple();
                Int32  j = 0;

                nRet = device.MV_CC_GetOneFrameTimeout_NET(pData, nPayloadSize, ref stFrameInfo, 1000);
                // 获取一帧图像
                if (MyCamera.MV_OK == nRet)
                {
                    Console.WriteLine("Get One Frame:" + "Width[" + Convert.ToString(stFrameInfo.nWidth) + "] , Height[" + Convert.ToString(stFrameInfo.nHeight)
                                      + "] , FrameNum[" + Convert.ToString(stFrameInfo.nFrameNum) + "]");
                    switch (stFrameInfo.enPixelType)
                    {
                    case MyCamera.MvGvspPixelType.PixelType_Gvsp_Coord3D_ABC32f:
                    {
                        float[] ConvertData = new float[nPayloadSize];
                        Marshal.Copy(pData, ConvertData, 0, (int)stFrameInfo.nFrameLen);
                        for (j = 0; j < stFrameInfo.nWidth; j++)
                        {
                            hv_X[j] = ConvertData[j * 3];
                            hv_Y[j] = ConvertData[j * 3 + 1];
                            hv_Z[j] = ConvertData[j * 3 + 2];
                        }
                    }
                    break;

                    case MyCamera.MvGvspPixelType.PixelType_Gvsp_Coord3D_ABC32:
                    {
                        Int32[] ConvertData = new Int32[nPayloadSize];
                        Marshal.Copy(pData, ConvertData, 0, (int)stFrameInfo.nFrameLen);
                        for (j = 0; j < stFrameInfo.nWidth; j++)
                        {
                            hv_X[j] = ConvertData[j * 3];
                            hv_Y[j] = ConvertData[j * 3 + 1];
                            hv_Z[j] = ConvertData[j * 3 + 2];
                        }
                    }
                    break;

                    case MyCamera.MvGvspPixelType.PixelType_Gvsp_Coord3D_AB32f:
                    {
                        float[] ConvertData = new float[nPayloadSize];
                        Marshal.Copy(pData, ConvertData, 0, (int)stFrameInfo.nFrameLen);
                        for (j = 0; j < stFrameInfo.nWidth; j++)
                        {
                            hv_X[j] = ConvertData[j * 2];
                            hv_Y[j] = ConvertData[j * 2 + 1];
                            hv_Z[j] = 0.0;
                        }
                    }
                    break;

                    case MyCamera.MvGvspPixelType.PixelType_Gvsp_Coord3D_AB32:
                    {
                        Int32[] ConvertData = new Int32[nPayloadSize];
                        Marshal.Copy(pData, ConvertData, 0, (int)stFrameInfo.nFrameLen);
                        for (j = 0; j < stFrameInfo.nWidth; j++)
                        {
                            hv_X[j] = ConvertData[j * 2];
                            hv_Y[j] = ConvertData[j * 2 + 1];
                            hv_Z[j] = 0;
                        }
                    }
                    break;

                    case MyCamera.MvGvspPixelType.PixelType_Gvsp_Coord3D_AC32f:
                    {
                        float[] ConvertData = new float[nPayloadSize];
                        Marshal.Copy(pData, ConvertData, 0, (int)stFrameInfo.nFrameLen);
                        for (j = 0; j < stFrameInfo.nWidth; j++)
                        {
                            hv_X[j] = ConvertData[j * 2];
                            hv_Y[j] = 0.0;
                            hv_Z[j] = ConvertData[j * 2 + 1];
                        }
                    }
                    break;

                    case MyCamera.MvGvspPixelType.PixelType_Gvsp_Coord3D_AC32:
                    {
                        Int32[] ConvertData = new Int32[nPayloadSize];
                        Marshal.Copy(pData, ConvertData, 0, (int)stFrameInfo.nFrameLen);
                        for (j = 0; j < stFrameInfo.nWidth; j++)
                        {
                            hv_X[j] = ConvertData[j * 2];
                            hv_Y[j] = 0;
                            hv_Z[j] = ConvertData[j * 2 + 1];
                        }
                    }
                    break;

                    default:
                        break;
                    }
                }
                else
                {
                    Console.WriteLine("No data{0:x8}", nRet);
                }

                // ch:停止抓图 | en:Stop grabbing
                nRet = device.MV_CC_StopGrabbing_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Stop grabbing failed{0:x8}", nRet);
                    break;
                }

                // ch:关闭设备 | en:Close device
                nRet = device.MV_CC_CloseDevice_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Close device failed{0:x8}", nRet);
                    break;
                }

                // ch:销毁设备 | en:Destroy device
                nRet = device.MV_CC_DestroyDevice_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Destroy device failed:{0:x8}", nRet);
                    break;
                }

                if (j == 0)
                {
                    Console.Write("Probably not a 3D device!\n");
                    break;
                }

                Console.Write("*********************************************************************\n");
                Console.Write("*  0.ply;  1.obj;  *\n");
                Console.Write("*********************************************************************\n");

                Int32 nFileType = 0;
                Console.Write("Select FileType: ");
                try
                {
                    nFileType = Convert.ToInt32(Console.ReadLine());
                }
                catch
                {
                    Console.Write("Invalid Input!\n");
                    break;
                }

                HOperatorSet.GenObjectModel3dFromPoints(hv_X, hv_Y, hv_Z, out hObjectModel3D);
                switch (nFileType)
                {
                case 0:
                {
                    HOperatorSet.WriteObjectModel3d(hObjectModel3D, "ply", "./3DFile.ply", "invert_normals", "false");
                }
                break;

                case 1:
                {
                    HOperatorSet.WriteObjectModel3d(hObjectModel3D, "obj", "./3DFile.obj", "invert_normals", "false");
                }
                break;

                default:
                    Console.Write("Input error!\n");
                    break;
                }
            } while (false);

            if (MyCamera.MV_OK != nRet)
            {
                // ch:销毁设备 | en:Destroy device
                nRet = device.MV_CC_DestroyDevice_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Destroy device failed:{0:x8}", nRet);
                }
            }

            Console.WriteLine("Press enter to exit");
            Console.ReadKey();
        }
Example #20
0
        static void Main(string[] args)
        {
            int      nRet   = MyCamera.MV_OK;
            MyCamera device = new MyCamera();

            do
            {
                // ch:枚举设备 | en:Enum device
                MyCamera.MV_CC_DEVICE_INFO_LIST 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)
                {
                    Console.WriteLine("Enum device failed:{0:x8}", nRet);
                    break;
                }
                Console.WriteLine("Enum device count : " + Convert.ToString(stDevList.nDeviceNum));
                if (0 == stDevList.nDeviceNum)
                {
                    break;
                }

                MyCamera.MV_CC_DEVICE_INFO stDevInfo;                            // 通用设备信息

                // 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);
                        Console.WriteLine("\n" + i.ToString() + ": [GigE] User Define Name : " + stGigEDeviceInfo.chUserDefinedName);
                        Console.WriteLine("device IP :" + nIp1 + "." + nIp2 + "." + nIp3 + "." + nIp4);
                    }
                    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));
                        Console.WriteLine("\n" + i.ToString() + ": [U3V] User Define Name : " + stUsb3DeviceInfo.chUserDefinedName);
                        Console.WriteLine("\n Serial Number : " + stUsb3DeviceInfo.chSerialNumber);
                        Console.WriteLine("\n Device Number : " + stUsb3DeviceInfo.nDeviceNumber);
                    }
                }

                Int32 nDevIndex = 0;
                Console.Write("\nPlease input index (0 -- {0:d}) : ", stDevList.nDeviceNum - 1);
                try
                {
                    nDevIndex = Convert.ToInt32(Console.ReadLine());
                }
                catch
                {
                    Console.Write("Invalid Input!\n");
                    break;
                }

                if (nDevIndex > stDevList.nDeviceNum - 1 || nDevIndex < 0)
                {
                    Console.Write("Input Error!\n");
                    break;
                }
                stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[nDevIndex], typeof(MyCamera.MV_CC_DEVICE_INFO));

                // ch:创建设备 | en:Create device
                nRet = device.MV_CC_CreateDevice_NET(ref stDevInfo);
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Create device failed:{0:x8}", nRet);
                    break;
                }

                // ch:打开设备 | en:Open device
                nRet = device.MV_CC_OpenDevice_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Open device failed:{0:x8}", nRet);
                    break;
                }

                // 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
                if (MyCamera.MV_OK != device.MV_CC_SetEnumValue_NET("TriggerMode", 0))
                {
                    Console.WriteLine("Set TriggerMode failed!");
                    break;
                }

                // ch:获取包大小 || en: Get Payload Size
                MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
                nRet = device.MV_CC_GetIntValue_NET("PayloadSize", ref stParam);
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Get PayloadSize failed:{0:x8}", nRet);
                    break;
                }
                UInt32 nPayloadSize = stParam.nCurValue;

                // ch:开启抓图 | en:start grab
                nRet = device.MV_CC_StartGrabbing_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Start grabbing failed:{0:x8}", nRet);
                    break;
                }

                int    nCount           = 0;
                IntPtr pBufForDriver    = Marshal.AllocHGlobal((int)nPayloadSize);
                IntPtr pBufForSaveImage = IntPtr.Zero;

                MyCamera.MV_FRAME_OUT_INFO_EX FrameInfo = new MyCamera.MV_FRAME_OUT_INFO_EX();
                while (nCount++ != 10)
                {
                    nRet = device.MV_CC_GetOneFrameTimeout_NET(pBufForDriver, nPayloadSize, ref FrameInfo, 1000);
                    // ch:获取一帧图像 | en:Get one image
                    if (MyCamera.MV_OK == nRet)
                    {
                        Console.WriteLine("Get One Frame:" + "Width[" + Convert.ToString(FrameInfo.nWidth) + "] , Height[" + Convert.ToString(FrameInfo.nHeight)
                                          + "] , FrameNum[" + Convert.ToString(FrameInfo.nFrameNum) + "]");

                        if (pBufForSaveImage == IntPtr.Zero)
                        {
                            pBufForSaveImage = Marshal.AllocHGlobal((int)(FrameInfo.nHeight * FrameInfo.nWidth * 3 + 2048));
                        }

                        MyCamera.MV_SAVE_IMAGE_PARAM_EX stSaveParam = new MyCamera.MV_SAVE_IMAGE_PARAM_EX();
                        stSaveParam.enImageType  = MyCamera.MV_SAVE_IAMGE_TYPE.MV_Image_Bmp;
                        stSaveParam.enPixelType  = FrameInfo.enPixelType;
                        stSaveParam.pData        = pBufForDriver;
                        stSaveParam.nDataLen     = FrameInfo.nFrameLen;
                        stSaveParam.nHeight      = FrameInfo.nHeight;
                        stSaveParam.nWidth       = FrameInfo.nWidth;
                        stSaveParam.pImageBuffer = pBufForSaveImage;
                        stSaveParam.nBufferSize  = (uint)(FrameInfo.nHeight * FrameInfo.nWidth * 3 + 2048);
                        stSaveParam.nJpgQuality  = 80;
                        nRet = device.MV_CC_SaveImageEx_NET(ref stSaveParam);
                        if (MyCamera.MV_OK != nRet)
                        {
                            Console.WriteLine("Save Image failed:{0:x8}", nRet);
                            continue;
                        }

                        // ch:将图像数据保存到本地文件 | en:Save image data to local file
                        byte[] data = new byte[stSaveParam.nImageLen];
                        Marshal.Copy(pBufForSaveImage, data, 0, (int)stSaveParam.nImageLen);
                        FileStream pFile = null;
                        try
                        {
                            pFile = new FileStream("frame" + nCount.ToString() + ".bmp", FileMode.Create);
                            pFile.Write(data, 0, data.Length);
                        }
                        catch
                        {
                            Console.WriteLine("保存图像失败");
                        }
                        finally
                        {
                            pFile.Close();
                        }
                        continue;
                    }
                    else
                    {
                        Console.WriteLine("No data:{0:x8}", nRet);
                    }
                }
                Marshal.FreeHGlobal(pBufForDriver);
                Marshal.FreeHGlobal(pBufForSaveImage);

                // ch:停止抓图 | en:Stop grab image
                nRet = device.MV_CC_StopGrabbing_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Stop grabbing failed{0:x8}", nRet);
                    break;
                }

                // ch:关闭设备 | en:Close device
                nRet = device.MV_CC_CloseDevice_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Close device failed{0:x8}", nRet);
                    break;
                }

                // ch:销毁设备 | en:Destroy device
                nRet = device.MV_CC_DestroyDevice_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Destroy device failed:{0:x8}", nRet);
                    break;
                }
            } while (false);

            if (MyCamera.MV_OK != nRet)
            {
                // ch:销毁设备 | en:Destroy device
                nRet = device.MV_CC_DestroyDevice_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Destroy device failed:{0:x8}", nRet);
                }
            }

            Console.WriteLine("Press enter to exit");
            Console.ReadKey();
        }
Example #21
0
        static void Main(string[] args)
        {
            MyCamera.MV_CC_DEVICE_INFO stDevInfo = new MyCamera.MV_CC_DEVICE_INFO();
            stDevInfo.nTLayerType = MyCamera.MV_GIGE_DEVICE;
            MyCamera.MV_GIGE_DEVICE_INFO stGigEDev = new MyCamera.MV_GIGE_DEVICE_INFO();
            int      nRet   = MyCamera.MV_OK;
            MyCamera device = new MyCamera();

            do
            {
                Console.Write("Please input Device Ip : ");
                string strCurrentIp = Convert.ToString(Console.ReadLine());// ch:需要连接的相机ip(根据实际填充)
                // en:The camera IP that needs to be connected (based on actual padding)
                Console.Write("Please input Net Export Ip : ");
                string strNetExport = Convert.ToString(Console.ReadLine());   // ch:相机对应的网卡ip(根据实际填充)
                // en:The pc IP that needs to be connected (based on actual padding)
                var parts = strCurrentIp.Split('.');
                try
                {
                    int nIp1 = Convert.ToInt32(parts[0]);
                    int nIp2 = Convert.ToInt32(parts[1]);
                    int nIp3 = Convert.ToInt32(parts[2]);
                    int nIp4 = Convert.ToInt32(parts[3]);
                    stGigEDev.nCurrentIp = (uint)((nIp1 << 24) | (nIp2 << 16) | (nIp3 << 8) | nIp4);

                    parts = strNetExport.Split('.');
                    nIp1  = Convert.ToInt32(parts[0]);
                    nIp2  = Convert.ToInt32(parts[1]);
                    nIp3  = Convert.ToInt32(parts[2]);
                    nIp4  = Convert.ToInt32(parts[3]);
                    stGigEDev.nNetExport = (uint)((nIp1 << 24) | (nIp2 << 16) | (nIp3 << 8) | nIp4);
                }
                catch
                {
                    Console.Write("Invalid Input!\n");
                    break;
                }

                // stGigEDev结构体转为stDevInfo.SpecialInfo.stGigEInfo(Byte[])
                IntPtr stGigeInfoPtr = Marshal.AllocHGlobal(Marshal.SizeOf(stGigEDev));
                Marshal.StructureToPtr(stGigEDev, stGigeInfoPtr, false);
                stDevInfo.SpecialInfo.stGigEInfo = new Byte[Marshal.SizeOf(stDevInfo.SpecialInfo)];
                Marshal.Copy(stGigeInfoPtr, stDevInfo.SpecialInfo.stGigEInfo, 0, Marshal.SizeOf(stDevInfo.SpecialInfo));
                Marshal.Release(stGigeInfoPtr);

                // ch:创建设备 | en: Create device
                nRet = device.MV_CC_CreateDevice_NET(ref stDevInfo);
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Create device failed:{0:x8}", nRet);
                    break;
                }

                // ch:打开设备 | en:Open device
                nRet = device.MV_CC_OpenDevice_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Open device failed:{0:x8}", nRet);
                    break;
                }

                // 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", 0);
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Set TriggerMode failed!");
                    break;
                }

                // ch:开启抓图 || en: start grab image
                nRet = device.MV_CC_StartGrabbing_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Start grabbing failed:{0:x8}", nRet);
                    break;
                }

                // ch:获取包大小 || en: Get Payload Size
                MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
                nRet = device.MV_CC_GetIntValue_NET("PayloadSize", ref stParam);
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Get PayloadSize failed:{0:x8}", nRet);
                    break;
                }
                UInt32 nPayloadSize = stParam.nCurValue;

                int    nCount           = 0;
                IntPtr pBufForDriver    = Marshal.AllocHGlobal((int)nPayloadSize);  // ch: 裸数据缓存 | en: raw data buff
                IntPtr pBufForSaveImage = IntPtr.Zero;                              // ch: 图片数据缓存 | en: Image data buff
                MyCamera.MV_FRAME_OUT_INFO_EX FrameInfo = new MyCamera.MV_FRAME_OUT_INFO_EX();
                while (nCount++ != 10)
                {
                    nRet = device.MV_CC_GetOneFrameTimeout_NET(pBufForDriver, nPayloadSize, ref FrameInfo, 1000);
                    // ch:获取一帧图像 | en:Get one image
                    if (MyCamera.MV_OK == nRet)
                    {
                        Console.WriteLine("Get One Frame: Width[{0}] , Height[{1}] , FrameNum[{2}]", FrameInfo.nWidth, FrameInfo.nHeight, FrameInfo.nFrameNum);
                        if (pBufForSaveImage == IntPtr.Zero)
                        {
                            pBufForSaveImage = Marshal.AllocHGlobal((int)(FrameInfo.nHeight * FrameInfo.nWidth * 3 + 2048));
                        }
                        MyCamera.MV_SAVE_IMAGE_PARAM_EX stSaveParam = new MyCamera.MV_SAVE_IMAGE_PARAM_EX();
                        stSaveParam.enImageType  = MyCamera.MV_SAVE_IAMGE_TYPE.MV_Image_Bmp;
                        stSaveParam.enPixelType  = FrameInfo.enPixelType;
                        stSaveParam.pData        = pBufForDriver;
                        stSaveParam.nDataLen     = FrameInfo.nFrameLen;
                        stSaveParam.nHeight      = FrameInfo.nHeight;
                        stSaveParam.nWidth       = FrameInfo.nWidth;
                        stSaveParam.pImageBuffer = pBufForSaveImage;
                        stSaveParam.nBufferSize  = (uint)(FrameInfo.nHeight * FrameInfo.nWidth * 3 + 2048);
                        stSaveParam.nJpgQuality  = 80;
                        nRet = device.MV_CC_SaveImageEx_NET(ref stSaveParam);
                        if (MyCamera.MV_OK != nRet)
                        {
                            Console.WriteLine("Save Image failed:{0:x8}", nRet);
                            continue;
                        }

                        // ch:将图像数据保存到本地文件 | en:Save image data to local file
                        byte[] data = new byte[stSaveParam.nImageLen];
                        Marshal.Copy(pBufForSaveImage, data, 0, (int)stSaveParam.nImageLen);
                        FileStream pFile = null;
                        try
                        {
                            pFile = new FileStream("frame" + nCount.ToString() + ".bmp", FileMode.Create);
                            pFile.Write(data, 0, data.Length);
                        }
                        catch
                        {
                            Console.WriteLine("保存失败");
                            continue;
                        }
                        finally
                        {
                            pFile.Close();
                        }
                    }
                    else
                    {
                        Console.WriteLine("No data:{0:x8}", nRet);
                        break;
                    }
                }
                Marshal.FreeHGlobal(pBufForDriver);
                Marshal.FreeHGlobal(pBufForSaveImage);

                // ch:停止抓图 | en:Stop grabbing
                nRet = device.MV_CC_StopGrabbing_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Stop grabbing failed{0:x8}", nRet);
                    break;
                }

                // ch:关闭设备 | en:Close device
                nRet = device.MV_CC_CloseDevice_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Close device failed{0:x8}", nRet);
                    break;
                }

                // ch:销毁设备 | en:Destroy device
                nRet = device.MV_CC_DestroyDevice_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Destroy device failed:{0:x8}", nRet);
                    break;
                }
            } while (false);

            if (MyCamera.MV_OK != nRet)
            {
                // ch:销毁设备 | en:Destroy device
                nRet = device.MV_CC_DestroyDevice_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Destroy device failed:{0:x8}", nRet);
                }
            }

            Console.WriteLine("Press enter to exit");
            Console.ReadKey();
        }
        private Bitmap ParseRawImageData(IntPtr pData, MyCamera.MV_FRAME_OUT_INFO_EX stFrameInfo)
        {
            int    nRet;
            UInt32 newBufferSize = 0;
            Bitmap output;

            MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();

            nRet = _myCamera.MV_CC_GetIntValue_NET("PayloadSize", ref stParam);
            if (MyCamera.MV_OK != nRet)
            {
                throw new InvalidOperationException("Can not get payload size");
            }

            newBufferSize = stParam.nCurValue;

            if (newBufferSize > _bufferSize)
            {
                _bufferSize = newBufferSize;
                _buffer     = new byte[_bufferSize];

                // 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)
                _buffSizeForSaveImage = _bufferSize * 3 + 2048;
                _bufForSaveImage      = new byte[_buffSizeForSaveImage];
            }

            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
            {
                throw new NotSupportedException("Can not support such pixel type currently");
            }

            IntPtr pImage = Marshal.UnsafeAddrOfPinnedArrayElement(_bufForSaveImage, 0);

            MyCamera.MV_PIXEL_CONVERT_PARAM stConverPixelParam = new MyCamera.MV_PIXEL_CONVERT_PARAM
            {
                nWidth         = stFrameInfo.nWidth,
                nHeight        = stFrameInfo.nHeight,
                pSrcData       = pData,
                nSrcDataLen    = stFrameInfo.nFrameLen,
                enSrcPixelType = stFrameInfo.enPixelType,
                enDstPixelType = enDstPixelType,
                pDstBuffer     = pImage,
                nDstBufferSize = _buffSizeForSaveImage
            };

            nRet = _myCamera.MV_CC_ConvertPixelType_NET(ref stConverPixelParam);
            if (MyCamera.MV_OK != nRet)
            {
                throw new InvalidOperationException("Unable to convert pixel type");
            }

            if (enDstPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8)
            {
                //************************Mono8 转 Bitmap*******************************
                output = new Bitmap(stFrameInfo.nWidth, stFrameInfo.nHeight, stFrameInfo.nWidth * 1,
                                    PixelFormat.Format8bppIndexed, pImage);

                ColorPalette cp = output.Palette;
                // init palette
                for (int i = 0; i < 256; i++)
                {
                    cp.Entries[i] = Color.FromArgb(i, i, i);
                }

                output.Palette = cp;
            }
            else
            {
                //*********************RGB8 转 Bitmap**************************
                for (int i = 0; i < stFrameInfo.nHeight; i++)
                {
                    for (int j = 0; j < stFrameInfo.nWidth; j++)
                    {
                        byte chRed = _bufForSaveImage[i * stFrameInfo.nWidth * 3 + j * 3];
                        _bufForSaveImage[i * stFrameInfo.nWidth * 3 + j * 3] =
                            _bufForSaveImage[i * stFrameInfo.nWidth * 3 + j * 3 + 2];
                        _bufForSaveImage[i * stFrameInfo.nWidth * 3 + j * 3 + 2] = chRed;
                    }
                }

                output = new Bitmap(stFrameInfo.nWidth, stFrameInfo.nHeight, stFrameInfo.nWidth * 3,
                                    PixelFormat.Format24bppRgb, pImage);
            }

            return(output);
        }
Example #23
0
        static void Main(string[] args)
        {
            int      nRet   = MyCamera.MV_OK;
            MyCamera device = new MyCamera();

            do
            {
                // ch:枚举设备 | en:Enum device
                MyCamera.MV_CC_DEVICE_INFO_LIST stDevList = new MyCamera.MV_CC_DEVICE_INFO_LIST();
                nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE, ref stDevList);
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Enum device failed:{0:x8}", nRet);
                    break;
                }
                Console.WriteLine("Enum device count : " + Convert.ToString(stDevList.nDeviceNum));
                if (0 == stDevList.nDeviceNum)
                {
                    break;
                }

                MyCamera.MV_CC_DEVICE_INFO stDevInfo;                            // 通用设备信息

                // 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);
                        Console.WriteLine("\n" + i.ToString() + ": [GigE] User Define Name : " + stGigEDeviceInfo.chUserDefinedName);
                        Console.WriteLine("device IP :" + nIp1 + "." + nIp2 + "." + nIp3 + "." + nIp4);
                    }
                    else
                    {
                        Console.Write("Not Support!\n");
                        break;
                    }
                }

                Int32 nDevIndex = 0;
                Console.Write("\nPlease input index (0 -- {0:d}) : ", stDevList.nDeviceNum - 1);
                try
                {
                    nDevIndex = Convert.ToInt32(Console.ReadLine());
                }
                catch
                {
                    Console.Write("Invalid Input!\n");
                    break;
                }

                if (nDevIndex > stDevList.nDeviceNum - 1 || nDevIndex < 0)
                {
                    Console.Write("Input Error!\n");
                    break;
                }
                stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[nDevIndex], typeof(MyCamera.MV_CC_DEVICE_INFO));

                // ch:创建设备 | en:Create device
                nRet = device.MV_CC_CreateDevice_NET(ref stDevInfo);
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Create device failed:{0:x8}", nRet);
                    break;
                }

                // ch:打开设备 | en:Open device
                nRet = device.MV_CC_OpenDevice_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Open device failed:{0:x8}", nRet);
                    break;
                }

                // 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:设置触发模式为on | en:Set trigger mode as on
                nRet = device.MV_CC_SetEnumValue_NET("TriggerMode", 1);
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Set Trigger Mode failed! {0:x8}", nRet);
                    break;
                }

                // ch:设置触发源为Action1 | en:Set trigger source as Action1
                nRet = device.MV_CC_SetEnumValue_NET("TriggerSource", 9);
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Set Trigger Source failed! {0:x8}", nRet);
                    break;
                }

                // ch:设置Action Device Key | en:Set Action Device Key
                nRet = device.MV_CC_SetIntValue_NET("ActionDeviceKey", g_DeviceKey);
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Set Action Device Key failed! {0:x8}", nRet);
                    break;
                }

                // ch:设置Action Group Key | en:Set Action Group Key
                nRet = device.MV_CC_SetIntValue_NET("ActionGroupKey", g_GroupKey);
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Set Action Group Key failed! {0:x8}", nRet);
                    break;
                }

                // ch:设置Action Group Mask | en:Set Action Group Mask
                nRet = device.MV_CC_SetIntValue_NET("ActionGroupMask", g_GroupMask);
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Set Action Group Mask fail! {0:x8}", nRet);
                    break;
                }

                // ch:获取包大小 || en: Get Payload Size
                MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
                nRet = device.MV_CC_GetIntValue_NET("PayloadSize", ref stParam);
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Get PayloadSize failed:{0:x8}", nRet);
                    break;
                }
                g_nPayloadSize = stParam.nCurValue;

                // ch:开启抓图 | en:start grab
                nRet = device.MV_CC_StartGrabbing_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Start grabbing failed:{0:x8}", nRet);
                    break;
                }

                Thread hActionCommandThreadHandle = new Thread(ActionCommandWorkThread);
                hActionCommandThreadHandle.Start(device);

                Thread hReceiveImageThreadHandle = new Thread(ReceiveImageWorkThread);
                hReceiveImageThreadHandle.Start(device);

                Console.WriteLine("Press enter to exit");
                Console.ReadKey();

                g_bExit = true;
                Thread.Sleep(1000);

                // ch:停止抓图 | en:Stop grab image
                nRet = device.MV_CC_StopGrabbing_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Stop grabbing failed{0:x8}", nRet);
                    break;
                }

                // ch:关闭设备 | en:Close device
                nRet = device.MV_CC_CloseDevice_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Close device failed{0:x8}", nRet);
                    break;
                }

                // ch:销毁设备 | en:Destroy device
                nRet = device.MV_CC_DestroyDevice_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Destroy device failed:{0:x8}", nRet);
                    break;
                }
            } while (false);

            if (MyCamera.MV_OK != nRet)
            {
                // ch:销毁设备 | en:Destroy device
                nRet = device.MV_CC_DestroyDevice_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Destroy device failed:{0:x8}", nRet);
                }
            }

            Console.WriteLine("Press enter to exit");
            Console.ReadKey();
        }
Example #24
0
        private void bnOpen_Click(object sender, EventArgs e)
        {
            if (m_pDeviceList.nDeviceNum == 0 || cbDeviceList.SelectedIndex == -1)
            {
                MessageBox.Show("No device,please select");
                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));

            nRet = m_pMyCamera.MV_CC_CreateDevice_NET(ref device);
            if (MyCamera.MV_OK != nRet)
            {
                return;
            }

            // ch:打开设备 | en:Open device
            nRet = m_pMyCamera.MV_CC_OpenDevice_NET();
            if (MyCamera.MV_OK != nRet)
            {
                MessageBox.Show("Open Device Fail");
                return;
            }

            // ch:获取包大小 || en: Get Payload Size
            MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
            nRet = m_pMyCamera.MV_CC_GetIntValue_NET("PayloadSize", ref stParam);
            if (MyCamera.MV_OK != nRet)
            {
                MessageBox.Show("Get PayloadSize Fail");
                return;
            }
            g_nPayloadSize = stParam.nCurValue;

            // ch:获取高 || en: Get Height
            nRet = m_pMyCamera.MV_CC_GetIntValue_NET("Height", ref stParam);
            if (MyCamera.MV_OK != nRet)
            {
                MessageBox.Show("Get Height Fail");
                return;
            }
            uint nHeight = stParam.nCurValue;

            // ch:获取宽 || en: Get Width
            nRet = m_pMyCamera.MV_CC_GetIntValue_NET("Width", ref stParam);
            if (MyCamera.MV_OK != nRet)
            {
                MessageBox.Show("Get Width Fail");
                return;
            }
            uint nWidth = stParam.nCurValue;

            m_pDataForRed   = new byte[nWidth * nHeight];
            m_pDataForGreen = new byte[nWidth * nHeight];
            m_pDataForBlue  = new byte[nWidth * nHeight];


            // ch:设置触发模式为off || en:set trigger mode as off
            m_pMyCamera.MV_CC_SetEnumValue_NET("AcquisitionMode", 2);
            m_pMyCamera.MV_CC_SetEnumValue_NET("TriggerMode", 0);

            SetCtrlWhenOpen();
        }
Example #25
0
        /// <summary>
        /// 打开设备
        /// </summary>
        public int OpenDevice()
        {
            int ret = (int)ErrorDef.Success;

            do
            {
                if (!IsInitOK)
                {
                    ret = (int)ErrorDef.InitFailedWhenOpen;
                    break;
                }
                if (IsDeviceOpen)
                {
                    return((int)ErrorDef.Success);//CloseDevice();
                }
                ///枚举当前所有GIGE设备
                MyCamera.MV_CC_DEVICE_INFO_LIST stDevList = new MyCamera.MV_CC_DEVICE_INFO_LIST();
                MyCamera.MV_CC_EnumDevices_NET((uint)MyCamera.MV_GIGE_DEVICE, ref stDevList);
                if (stDevList.nDeviceNum == 0)
                {
                    ret = (int)ErrorDef.DevUnExisted;
                    break;
                }
                bool isFindDev = false;
                MyCamera.MV_CC_DEVICE_INFO devCmr = new MyCamera.MV_CC_DEVICE_INFO();;
                for (int i = 0; i < stDevList.nDeviceNum; i++)
                {
                    MyCamera.MV_CC_DEVICE_INFO devInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO));
                    IntPtr gigeBuff = Marshal.UnsafeAddrOfPinnedArrayElement(devInfo.SpecialInfo.stGigEInfo, 0);
                    MyCamera.MV_GIGE_DEVICE_INFO gigeInfo = (MyCamera.MV_GIGE_DEVICE_INFO)Marshal.PtrToStructure(gigeBuff, typeof(MyCamera.MV_GIGE_DEVICE_INFO));
                    //比对已存在的相机设备的IP和参数_cmrIP
                    //IPAddress ipAdd = IPAddress.Parse(_cmrIP);
                    //uint nip = BitConverter.ToUInt32(ipAdd.GetAddressBytes(), 0);
                    //if(nip == gigeInfo.nCurrentIp)
                    //{
                    //    isFindDev = true;
                    //    devCmr = devInfo;
                    //    break;
                    //}
                    uint   nIp1  = ((gigeInfo.nCurrentIp & 0xff000000) >> 24);
                    uint   nIp2  = ((gigeInfo.nCurrentIp & 0x00ff0000) >> 16);
                    uint   nIp3  = ((gigeInfo.nCurrentIp & 0x0000ff00) >> 8);
                    uint   nIp4  = (gigeInfo.nCurrentIp & 0x000000ff);
                    string devIP = nIp1 + "." + nIp2 + "." + nIp3 + "." + nIp4;
                    if (devIP == _cmrIP)
                    {
                        isFindDev = true;
                        devCmr    = devInfo;
                        break;
                    }
                }
                if (!isFindDev)
                {
                    ret = (int)ErrorDef.DevUnExisted;
                    break;
                }
                _hikCmr = new MyCamera();

                int err = _hikCmr.MV_CC_CreateDevice_NET(ref devCmr);
                if (MyCamera.MV_OK != err)
                {
                    ret = (int)ErrorDef.InvokeFailed;
                    break;
                }

                err = _hikCmr.MV_CC_OpenDevice_NET();
                if (MyCamera.MV_OK != err)
                {
                    ret = (int)ErrorDef.InvokeFailed;
                    break;
                }
                MyCamera.MVCC_INTVALUE stVal = new MyCamera.MVCC_INTVALUE();
                int nRet = _hikCmr.MV_CC_GetIntValue_NET("PayloadSize", ref stVal);
                if (MyCamera.MV_OK != nRet)
                {
                    _payloadSize = 0;
                }
                else
                {
                    _payloadSize = (int)stVal.nCurValue;
                }



                //SetBuffSize(5); //设置相机内部帧缓存数量
                err = _hikCmr.MV_CC_SetImageNodeNum_NET(5);
                if (MyCamera.MV_OK == err)
                {
                    _FrameBuffSize = 5;
                }
                else
                {
                    _FrameBuffSize = 0;
                }


                IsDeviceOpen = true;
                //将相机触发模式设置为禁用(默认为软件采图),不检查错误
                SetTrigMode(JFCmrTrigMode.software); //转换一次模式(防止即时采图未设置)
                SetTrigMode(JFCmrTrigMode.disable);


                break;
            } while (false);
            return(ret);
        }