コード例 #1
0
        //打开设备
        private void button_OpenCarmer_Click(object sender, RoutedEventArgs e)
        {
            if (m_pDeviceList.nDeviceNum == 0 || m_CameraDeviceList.SelectedIndex == -1)
            {
                MessageBox.Show("无设备,请选择");
                return;
            }

            //获取选择的设备信息
            MyCamera.MV_CC_DEVICE_INFO device = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_pDeviceList.pDeviceInfo[m_CameraDeviceList.SelectedIndex], typeof(MyCamera.MV_CC_DEVICE_INFO));
            int nRet = m_pOperator.Open(ref device);   //打开设备

            if (MyCamera.MV_OK != nRet)
            {
                MessageBox.Show("设备打开失败!");
                return;
            }

            //设置采集连续模式
            m_pOperator.SetEnumValue("AcquisitionMode", 2); // 工作在连续模式
            m_pOperator.SetEnumValue("TriggerMode", 0);     // 连续模式
            CarmeraStartGrab();

            SetCtrlWhenOpen();
        }
コード例 #2
0
        // 相机初始化
        public MyCamera CameraInit(int index)
        {
            MyCamera.MV_CC_DEVICE_INFO device =
                (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(deviceList.pDeviceInfo[index],
                                                                   typeof(MyCamera.MV_CC_DEVICE_INFO));
            if (null == camera)
            {
                camera = new MyCamera();
            }
            // 创建设备
            camera.MV_CC_CreateDevice_NET(ref device);
            // 打开设备
            camera.MV_CC_OpenDevice_NET();
            // 探测网络最佳包大小(只对GigE相机有效)
            if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)
            {
                int nPacketSize = camera.MV_CC_GetOptimalPacketSize_NET();
                if (nPacketSize > 0)
                {
                    camera.MV_CC_SetIntValue_NET("GevSCPSPacketSize", (uint)nPacketSize);
                }
            }
            // 设置连续采集模式
            camera.MV_CC_SetEnumValue_NET("AcquisitionMode", (uint)MyCamera.MV_CAM_ACQUISITION_MODE.MV_ACQ_MODE_CONTINUOUS);
            camera.MV_CC_SetEnumValue_NET("TriggerMode", (uint)MyCamera.MV_CAM_TRIGGER_MODE.MV_TRIGGER_MODE_OFF);

            return(camera);
        }
コード例 #3
0
        // 获取设备名 为 name 的设备 返回数量
        public int CameraCount(string name)
        {
            m_hCamera = -1;
            int count = 0;
            int nRet;

            /*创建设备列表*/
            System.GC.Collect();
            CameraOperator_HK.EnumDevices(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref m_pDeviceList);
            //在窗体列表中显示设备名
            for (int i = 0; i < m_pDeviceList.nDeviceNum; i++)
            {
                MyCamera.MV_CC_DEVICE_INFO device = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_pDeviceList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO));
                if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)
                {
                    IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(device.SpecialInfo.stGigEInfo, 0);
                    MyCamera.MV_GIGE_DEVICE_INFO gigeInfo = (MyCamera.MV_GIGE_DEVICE_INFO)Marshal.PtrToStructure(buffer, typeof(MyCamera.MV_GIGE_DEVICE_INFO));
                    //if (name == gigeInfo.chModelName)
                    //{
                    bitmapInfo.CameraName = name;
                    m_hCamera             = i;
                    //}
                }
            }
            count = (int)m_pDeviceList.nDeviceNum;
            return(count);
        }
コード例 #4
0
ファイル: CameraOperator.cs プロジェクト: fb-jince/Jet_System
        public MyCamera.MV_CC_DEVICE_INFO GetInfoByIP(string ip)
        {
            MyCamera.MV_CC_DEVICE_INFO      stDevInfo    = new MyCamera.MV_CC_DEVICE_INFO();
            MyCamera.MV_CC_DEVICE_INFO_LIST stDeviceList = new MyCamera.MV_CC_DEVICE_INFO_LIST();;
            int nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref stDeviceList);

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



            for (Int32 i = 0; i < stDeviceList.nDeviceNum; i++)
            {
                stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDeviceList.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);


                    string ip1 = nIp1 + "." + nIp2 + "." + nIp3 + "." + nIp4;
                    if (ip == ip1)
                    {
                        return(stDevInfo);
                    }
                }
            }
            return(stDevInfo);
        }
コード例 #5
0
        /****************************************************************************
        * @fn           Open
        * @brief        连接设备
        * @param        stDeviceInfo       IN       设备信息结构体
        * @return       成功:0;错误:-1
        ****************************************************************************/
        public int Open(ref MyCamera.MV_CC_DEVICE_INFO stDeviceInfo)
        {
            if (null == m_pCSI)
            {
                m_pCSI = new MyCamera();
                if (null == m_pCSI)
                {
                    return(CO_FAIL);
                }
            }

            int nRet;

            nRet = m_pCSI.MV_CC_CreateDevice_NET(ref stDeviceInfo);
            if (MyCamera.MV_OK != nRet)
            {
                return(CO_FAIL);
            }

            nRet = m_pCSI.MV_CC_OpenDevice_NET();
            if (MyCamera.MV_OK != nRet)
            {
                return(CO_FAIL);
            }
            return(CO_OK);
        }
コード例 #6
0
        private void button_Open_Click(object sender, RoutedEventArgs e)
        {
            if (m_pDeviceList.nDeviceNum == 0 || m_CameraDeviceList.SelectedIndex == -1)
            {
                MessageBox.Show("无设备,请选择");
                return;
            }

            //获取选择的设备信息
            MyCamera.MV_CC_DEVICE_INFO device = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_pDeviceList.pDeviceInfo[m_CameraDeviceList.SelectedIndex], typeof(MyCamera.MV_CC_DEVICE_INFO));
            int nRet = m_pOperator.Open(ref device);   //打开设备

            if (MyCamera.MV_OK != nRet)
            {
                MessageBox.Show("设备打开失败!");
                return;
            }


            ImageCallback = new MyCamera.cbOutputdelegate(SaveImageCallBack);
            nRet          = m_pOperator.RegisterImageCallBack(ImageCallback, IntPtr.Zero);
            if (CameraOperator.CO_OK != nRet)
            {
                MessageBox.Show("注册回调失败!");
            }
            button_Open.IsEnabled = false;
        }
コード例 #7
0
        public static int EnumDevices()
        {
            int nRet;

            // ch:创建设备列表 || en: Create device list
            System.GC.Collect();
            nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref m_pDeviceList);
            if (MyCamera.MV_OK != nRet)
            {
                MessageBox.Show("Enum Devices Fail");
                return(0);
            }
            for (int i = 0; i < m_pDeviceList.nDeviceNum; i++)
            {
                MyCamera.MV_CC_DEVICE_INFO device = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_pDeviceList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO));
                IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(device.SpecialInfo.stGigEInfo, 0);
                MyCamera.MV_GIGE_DEVICE_INFO gigeInfo = (MyCamera.MV_GIGE_DEVICE_INFO)Marshal.PtrToStructure(buffer, typeof(MyCamera.MV_GIGE_DEVICE_INFO));
                bool sign = true;
                foreach (var temp in CameraInfo)
                {
                    IntPtr buffer1 = Marshal.UnsafeAddrOfPinnedArrayElement(temp.SpecialInfo.stGigEInfo, 0);
                    MyCamera.MV_GIGE_DEVICE_INFO gigeInfo1 = (MyCamera.MV_GIGE_DEVICE_INFO)Marshal.PtrToStructure(buffer1, typeof(MyCamera.MV_GIGE_DEVICE_INFO));
                    if (gigeInfo1.chUserDefinedName == gigeInfo.chUserDefinedName || gigeInfo1.chSerialNumber == gigeInfo.chSerialNumber)
                    {
                        sign = false;
                    }
                }
                if (sign == true)
                {
                    CameraInfo.Add(device);
                }
            }
            return((int)m_pDeviceList.nDeviceNum);
        }
コード例 #8
0
        private bool DeviceListAcq()
        {
            System.GC.Collect();
            m_stDeviceList.nDeviceNum = 0;
            int nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE, ref m_stDeviceList);

            if (0 != nRet)
            {
                StrErrorMsg = "枚举设备失败!";
                return(false);
            }

            for (int i = 0; i < m_stDeviceList.nDeviceNum; i++)
            {
                MyCamera.MV_CC_DEVICE_INFO device =
                    (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_stDeviceList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO));
                if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)
                {
                    MyCamera.MV_GIGE_DEVICE_INFO gigeInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(device.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_GIGE_DEVICE_INFO));

                    if (gigeInfo.chUserDefinedName != "")
                    {
                        m_listUserDefinedName.Add(i, gigeInfo.chUserDefinedName);
                    }
                    else
                    {
                        StrErrorMsg = "没有给相机命名!" + "GigE: " + gigeInfo.chManufacturerName + " " + gigeInfo.chModelName + " (" + gigeInfo.chSerialNumber + ")";
                        return(false);
                    }
                }
            }
            return(true);
        }
コード例 #9
0
        /// <summary>
        /// Search all device run on your computer with usb type or ethernet.
        /// </summary>
        /// <returns>string[]</returns>
        public string[] SearchDevice()
        {
            string[] _ListDevice;
            int      nRet;

            GC.Collect();
            nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref pDeviceList);
            if (0 != nRet)
            {
                return(null);
            }
            _ListDevice = new string[pDeviceList.nDeviceNum];
            for (int i = 0; i < pDeviceList.nDeviceNum; i++)
            {
                MyCamera.MV_CC_DEVICE_INFO device = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(pDeviceList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO));
                if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)
                {
                    IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(device.SpecialInfo.stGigEInfo, 0);
                    MyCamera.MV_GIGE_DEVICE_INFO gigeInfo = (MyCamera.MV_GIGE_DEVICE_INFO)Marshal.PtrToStructure(buffer, typeof(MyCamera.MV_GIGE_DEVICE_INFO));
                    _ListDevice[i] = (gigeInfo.chSerialNumber);
                }
                else if (device.nTLayerType == MyCamera.MV_USB_DEVICE)
                {
                    IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(device.SpecialInfo.stUsb3VInfo, 0);
                    MyCamera.MV_USB3_DEVICE_INFO usbInfo = (MyCamera.MV_USB3_DEVICE_INFO)Marshal.PtrToStructure(buffer, typeof(MyCamera.MV_USB3_DEVICE_INFO));
                    _ListDevice[i] = (usbInfo.chSerialNumber);
                }
            }
            return(_ListDevice);
        }
コード例 #10
0
        public override bool Open(string ip)
        {
            this.device_Info = m_pOperator.GetInfoByIP(ip);



            return(Open());
        }
コード例 #11
0
        public bool FindCameraDevice()
        {
            bool bRe = false;

            //System.GC.Collect();
            m_CameraListComboBox.Items.Clear();
            int nRet = CameraOperator.EnumDevices(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref m_CameraList);

            if (0 != nRet)
            {
                MessageBox.Show("枚举设备失败!");
                bRe = false;
            }

            //在窗体列表中显示设备名
            for (int i = 0; i < m_CameraList.nDeviceNum; i++)
            {
                MyCamera.MV_CC_DEVICE_INFO device = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_CameraList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO));
                if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)
                {
                    IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(device.SpecialInfo.stGigEInfo, 0);
                    MyCamera.MV_GIGE_DEVICE_INFO gigeInfo = (MyCamera.MV_GIGE_DEVICE_INFO)Marshal.PtrToStructure(buffer, typeof(MyCamera.MV_GIGE_DEVICE_INFO));
                    if (gigeInfo.chUserDefinedName != "")
                    {
                        m_CameraListComboBox.Items.Add("GigE: " + gigeInfo.chUserDefinedName + " (" + gigeInfo.chSerialNumber + ")");
                    }
                    else
                    {
                        m_CameraListComboBox.Items.Add("GigE: " + gigeInfo.chManufacturerName + " " + gigeInfo.chModelName + " (" + gigeInfo.chSerialNumber + ")");
                    }
                }
                else if (device.nTLayerType == MyCamera.MV_USB_DEVICE)
                {
                    IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(device.SpecialInfo.stUsb3VInfo, 0);
                    MyCamera.MV_USB3_DEVICE_INFO usbInfo = (MyCamera.MV_USB3_DEVICE_INFO)Marshal.PtrToStructure(buffer, typeof(MyCamera.MV_USB3_DEVICE_INFO));
                    if (usbInfo.chUserDefinedName != "")
                    {
                        m_CameraListComboBox.Items.Add("USB: " + usbInfo.chUserDefinedName + " (" + usbInfo.chSerialNumber + ")");
                    }
                    else
                    {
                        m_CameraListComboBox.Items.Add("USB: " + usbInfo.chManufacturerName + " " + usbInfo.chModelName + " (" + usbInfo.chSerialNumber + ")");
                    }
                }
            }

            //选择第一项
            if (m_CameraList.nDeviceNum != 0)
            {
                m_CameraListComboBox.SelectedIndex = 0;
                bRe = true;
            }

            return(bRe);
        }
コード例 #12
0
        private void DeviceListAcq()
        {
            int nRet;

            // ch:创建设备列表 || en: Create device list
            System.GC.Collect();
            cbDeviceList.Items.Clear();
            nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref m_pDeviceList);
            if (MyCamera.MV_OK != nRet)
            {
                MessageBox.Show("Enum Devices Fail");
                return;
            }

            // ch:在窗体列表中显示设备名 || Display the device'name on window's list
            for (int i = 0; i < m_pDeviceList.nDeviceNum; i++)
            {
                MyCamera.MV_CC_DEVICE_INFO device = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_pDeviceList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO));
                if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)
                {
                    IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(device.SpecialInfo.stGigEInfo, 0);
                    MyCamera.MV_GIGE_DEVICE_INFO gigeInfo = (MyCamera.MV_GIGE_DEVICE_INFO)Marshal.PtrToStructure(buffer, typeof(MyCamera.MV_GIGE_DEVICE_INFO));
                    if (gigeInfo.chUserDefinedName != "")
                    {
                        cbDeviceList.Items.Add("GigE: " + gigeInfo.chUserDefinedName + " (" + gigeInfo.chSerialNumber + ")");
                    }
                    else
                    {
                        cbDeviceList.Items.Add("GigE: " + gigeInfo.chManufacturerName + " " + gigeInfo.chModelName + " (" + gigeInfo.chSerialNumber + ")");
                    }
                }
                else if (device.nTLayerType == MyCamera.MV_USB_DEVICE)
                {
                    IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(device.SpecialInfo.stUsb3VInfo, 0);
                    MyCamera.MV_USB3_DEVICE_INFO usbInfo = (MyCamera.MV_USB3_DEVICE_INFO)Marshal.PtrToStructure(buffer, typeof(MyCamera.MV_USB3_DEVICE_INFO));
                    if (usbInfo.chUserDefinedName != "")
                    {
                        cbDeviceList.Items.Add("USB: " + usbInfo.chUserDefinedName + " (" + usbInfo.chSerialNumber + ")");
                    }
                    else
                    {
                        cbDeviceList.Items.Add("USB: " + usbInfo.chManufacturerName + " " + usbInfo.chModelName + " (" + usbInfo.chSerialNumber + ")");
                    }
                }
            }

            //.ch: 选择第一项 || en: Select the first item
            if (m_pDeviceList.nDeviceNum != 0)
            {
                cbDeviceList.SelectedIndex = 0;
            }
        }
コード例 #13
0
        private void DeviceListAcq()
        {
            // ch:创建设备列表 | en:Create Device List
            System.GC.Collect();
            cbDeviceList.Items.Clear();
            m_stDeviceList.nDeviceNum = 0;
            int nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref m_stDeviceList);

            if (0 != nRet)
            {
                ShowErrorMsg("Enumerate devices fail!", 0);
                return;
            }

            // ch:在窗体列表中显示设备名 | en:Display device name in the form list
            for (int i = 0; i < m_stDeviceList.nDeviceNum; i++)
            {
                MyCamera.MV_CC_DEVICE_INFO device = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_stDeviceList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO));
                if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)
                {
                    MyCamera.MV_GIGE_DEVICE_INFO gigeInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(device.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_GIGE_DEVICE_INFO));

                    if (gigeInfo.chUserDefinedName != "")
                    {
                        cbDeviceList.Items.Add("GEV: " + gigeInfo.chUserDefinedName + " (" + gigeInfo.chSerialNumber + ")");
                    }
                    else
                    {
                        cbDeviceList.Items.Add("GEV: " + gigeInfo.chManufacturerName + " " + gigeInfo.chModelName + " (" + gigeInfo.chSerialNumber + ")");
                    }
                }
                else if (device.nTLayerType == MyCamera.MV_USB_DEVICE)
                {
                    MyCamera.MV_USB3_DEVICE_INFO usbInfo = (MyCamera.MV_USB3_DEVICE_INFO)MyCamera.ByteToStruct(device.SpecialInfo.stUsb3VInfo, typeof(MyCamera.MV_USB3_DEVICE_INFO));
                    if (usbInfo.chUserDefinedName != "")
                    {
                        cbDeviceList.Items.Add("U3V: " + usbInfo.chUserDefinedName + " (" + usbInfo.chSerialNumber + ")");
                    }
                    else
                    {
                        cbDeviceList.Items.Add("U3V: " + usbInfo.chManufacturerName + " " + usbInfo.chModelName + " (" + usbInfo.chSerialNumber + ")");
                    }
                }
            }

            // ch:选择第一项 | en:Select the first item
            if (m_stDeviceList.nDeviceNum != 0)
            {
                cbDeviceList.SelectedIndex = 0;
            }
        }
コード例 #14
0
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public static bool                              GetHikDeviceInfo(CInfo_Camera_Factory CameraInfo, out MyCamera.MV_CC_DEVICE_INFO stDevInfo)
        {
            stDevInfo = new MyCamera.MV_CC_DEVICE_INFO();

            var stDevList = new MyCamera.MV_CC_DEVICE_INFO_LIST();
            var nRet      = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref stDevList);

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

            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));

                switch (CameraInfo.m_teinterface)
                {
                case EINTERFACE.eUSB:
                    if (MyCamera.MV_USB_DEVICE == stDevInfo.nTLayerType)
                    {
                        var stUsb3DeviceInfo = (MyCamera.MV_USB3_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stUsb3VInfo, typeof(MyCamera.MV_USB3_DEVICE_INFO));

                        if (CameraInfo.m_strmodel == stUsb3DeviceInfo.chModelName &&
                            CameraInfo.m_strserial == stUsb3DeviceInfo.chSerialNumber)
                        {
                            return(true);
                        }
                    }

                    break;

                case EINTERFACE.eGIGE:
                    if (MyCamera.MV_GIGE_DEVICE == stDevInfo.nTLayerType)
                    {
                        var stGigEDeviceInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_GIGE_DEVICE_INFO));

                        if (CameraInfo.m_strmodel == stGigEDeviceInfo.chModelName &&
                            CameraInfo.m_strserial == stGigEDeviceInfo.chSerialNumber)
                        {
                            return(true);
                        }
                    }

                    break;
                }
            }

            return(false);
        }
コード例 #15
0
ファイル: CameraOperator.cs プロジェクト: fb-jince/Jet_System
        /****************************************************************************
        * @fn           Open
        * @brief        连接设备
        * @param        stDeviceInfo       IN       设备信息结构体
        * @return       成功:0;错误:-1
        ****************************************************************************/
        /// <summary>
        /// 连接设备
        /// </summary>
        /// <param name="stDeviceInfo">设备信息结构体</param>
        /// <returns>成功:0;错误:-1</returns>
        public int Open(MyCamera.MV_CC_DEVICE_INFO index)
        {
            MyCamera.MV_CC_DEVICE_INFO_LIST stDeviceList = new MyCamera.MV_CC_DEVICE_INFO_LIST();
            int nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref stDeviceList);

            MyCamera.MV_CC_DEVICE_INFO stDeviceInfo = new MyCamera.MV_CC_DEVICE_INFO();

            //stDeviceInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDeviceList.pDeviceInfo[index], typeof(MyCamera.MV_CC_DEVICE_INFO));

            if (null == m_pCSI)
            {
                m_pCSI = new MyCamera();
                if (null == m_pCSI)
                {
                    return(CO_FAIL);
                }
            }


            nRet = m_pCSI.MV_CC_CreateDevice_NET(ref index);


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


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

            if (stDeviceInfo.nTLayerType == MyCamera.MV_GIGE_DEVICE)
            {
                int nPacketSize = m_pCSI.MV_CC_GetOptimalPacketSize_NET();

                if (nPacketSize > 0)
                {
                    nRet = SetIntValue("GevSCPSPacketSize", (UInt32)nPacketSize);
                    return(nRet);
                }
            }


            return(CO_OK);
        }
コード例 #16
0
        private static bool GetHikDeviceInfo(CameraInfo cameraInfo, out MyCamera.MV_CC_DEVICE_INFO stDevInfo)
        {
            stDevInfo = new MyCamera.MV_CC_DEVICE_INFO();

            var stDevList = new MyCamera.MV_CC_DEVICE_INFO_LIST();
            var nRet      = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref stDevList);

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

            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));

                switch (cameraInfo.CameraType)
                {
                case ECameraType.USB:
                    if (MyCamera.MV_USB_DEVICE == stDevInfo.nTLayerType)
                    {
                        var stUsb3DeviceInfo = (MyCamera.MV_USB3_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stUsb3VInfo, typeof(MyCamera.MV_USB3_DEVICE_INFO));

                        if (cameraInfo.ModelName == stUsb3DeviceInfo.chModelName &&
                            cameraInfo.SerialNo == stUsb3DeviceInfo.chSerialNumber)
                        {
                            return(true);
                        }
                    }
                    break;

                case ECameraType.GIGE:
                    if (MyCamera.MV_GIGE_DEVICE == stDevInfo.nTLayerType)
                    {
                        var stGigEDeviceInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_GIGE_DEVICE_INFO));

                        if (cameraInfo.ModelName == stGigEDeviceInfo.chModelName &&
                            cameraInfo.SerialNo == stGigEDeviceInfo.chSerialNumber)
                        {
                            return(true);
                        }
                    }
                    break;
                }
            }

            return(false);
        }
コード例 #17
0
        //打开相机设置 工作模式 triggerSource为触发模式(若为软触发时使用)
        public int OpenMVS(uint triggerSource)
        {
            if (m_bitmap != null)
            {
                m_bitmap.Dispose();
                m_bitmap = null;
            }
            if (m_pDeviceList.nDeviceNum == 0)
            {
                return(-1);  //没有相机
            }
            int nRet = -1;

            //获取选择的设备信息
            MyCamera.MV_CC_DEVICE_INFO device =
                (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_pDeviceList.pDeviceInfo[m_hCamera],
                                                                   typeof(MyCamera.MV_CC_DEVICE_INFO));
            //打开设备
            nRet = Open(ref device);
            if (MyCamera.MV_OK != nRet)
            {
                return(-2); //打开相机失败
            }
            //设置采集连续模式
            SetEnumValue("AcquisitionMode", 2);// 工作在连续模式
            SetEnumValue("TriggerMode", 0);
            // 连续模式
            //触发源选择:0 - Line0;
            //           1 - Line1;
            //           2 - Line2;
            //           3 - Line3;
            //           4 - Counter;
            //           7 - Software;
            // SetEnumValue("TriggerSource", triggerSource);    //触发源
            //return cameraOperator.SetROI(rect, widthMax, heightMax, increase);
            //Size maxSize = GetCameraROIMax();
            //SetROI(RectROI, maxSize.Width, maxSize.Height, 4);
            SetExposureTime(ExpourseTime);
            //SetGain(Gain);
            IsCameraOpen = true;
            return(0);
        }
コード例 #18
0
ファイル: ReconnectDemo.cs プロジェクト: HUT-17/HUT
        // ch:回调函数 | en:Callback function
        private void cbExceptiondelegate(uint nMsgType, IntPtr pUser)
        {
            if (nMsgType == MyCamera.MV_EXCEPTION_DEV_DISCONNECT)
            {
                DeInitCamera();

                // ch:获取选择的设备信息 | en:Get Used Device Info
                MyCamera.MV_CC_DEVICE_INFO device =
                    (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_stDeviceList.pDeviceInfo[cbDeviceList.SelectedIndex],
                                                                       typeof(MyCamera.MV_CC_DEVICE_INFO));

                // ch:打开设备 | en:Open Device
                while (true)
                {
                    int nRet = m_MyCamera.MV_CC_CreateDevice_NET(ref device);
                    if (MyCamera.MV_OK != nRet)
                    {
                        Thread.Sleep(5);
                        continue;
                    }

                    nRet = m_MyCamera.MV_CC_OpenDevice_NET();
                    if (MyCamera.MV_OK != nRet)
                    {
                        Thread.Sleep(5);
                        m_MyCamera.MV_CC_DestroyDevice_NET();
                        continue;
                    }
                    else
                    {
                        nRet = InitCamera();
                        if (MyCamera.MV_OK != nRet)
                        {
                            Thread.Sleep(5);
                            m_MyCamera.MV_CC_DestroyDevice_NET();
                            continue;
                        }
                        break;
                    }
                }
            }
        }
コード例 #19
0
        private void cbDeviceList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (m_pDeviceList.nDeviceNum == 0)
            {
                ShowErrorMsg("No Device", 0);
                return;
            }
            MyCamera.MV_CC_DEVICE_INFO   device   = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_pDeviceList.pDeviceInfo[cbDeviceList.SelectedIndex], typeof(MyCamera.MV_CC_DEVICE_INFO));
            MyCamera.MV_GIGE_DEVICE_INFO gigeInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(device.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_GIGE_DEVICE_INFO));
            UInt32 nNetIp1 = (gigeInfo.nNetExport & 0xFF000000) >> 24;
            UInt32 nNetIp2 = (gigeInfo.nNetExport & 0x00FF0000) >> 16;
            UInt32 nNetIp3 = (gigeInfo.nNetExport & 0x0000FF00) >> 8;
            UInt32 nNetIp4 = (gigeInfo.nNetExport & 0x000000FF);

            // ch:显示IP | en:Display IP
            UInt32 nIp1 = (gigeInfo.nCurrentIp & 0xFF000000) >> 24;
            UInt32 nIp2 = (gigeInfo.nCurrentIp & 0x00FF0000) >> 16;
            UInt32 nIp3 = (gigeInfo.nCurrentIp & 0x0000FF00) >> 8;
            UInt32 nIp4 = (gigeInfo.nCurrentIp & 0x000000FF);

            lbTip.Text = "Notice: Recommend IP range (" + nNetIp1.ToString() + "." + nNetIp2.ToString() + "." + nNetIp3.ToString() + "." + "0" + "~" + nNetIp1.ToString() + "." + nNetIp2.ToString() + "." + nIp3.ToString() + "." + "255)";

            tbIP.Text = nIp1.ToString() + "." + nIp2.ToString() + "." + nIp3.ToString() + "." + nIp4.ToString();

            // ch:显示掩码 | en:Display mask
            nIp1 = (gigeInfo.nCurrentSubNetMask & 0xFF000000) >> 24;
            nIp2 = (gigeInfo.nCurrentSubNetMask & 0x00FF0000) >> 16;
            nIp3 = (gigeInfo.nCurrentSubNetMask & 0x0000FF00) >> 8;
            nIp4 = (gigeInfo.nCurrentSubNetMask & 0x000000FF);

            tbMask.Text = nIp1.ToString() + "." + nIp2.ToString() + "." + nIp3.ToString() + "." + nIp4.ToString();

            // ch:显示网关 | en:Display gateway
            nIp1 = (gigeInfo.nDefultGateWay & 0xFF000000) >> 24;
            nIp2 = (gigeInfo.nDefultGateWay & 0x00FF0000) >> 16;
            nIp3 = (gigeInfo.nDefultGateWay & 0x0000FF00) >> 8;
            nIp4 = (gigeInfo.nDefultGateWay & 0x000000FF);

            tbDefaultWay.Text = nIp1.ToString() + "." + nIp2.ToString() + "." + nIp3.ToString() + "." + nIp4.ToString();
        }
コード例 #20
0
ファイル: HIKVISION.cs プロジェクト: 22860364/Xvision
 public override bool InitialCamera(string _serialNumber)
 {
     index           = Convert.ToInt32(_serialNumber.Split('_')[1]);
     deviceInfo      = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[index], typeof(MyCamera.MV_CC_DEVICE_INFO));
     hikvisionCamera = new MyCamera();
     if (hikvisionCamera == null)
     {
         return(false);
     }
     if (hikvisionCamera.MV_CC_CreateDevice_NET(ref deviceInfo) != MyCamera.MV_OK)
     {
         return(false);
     }
     if (hikvisionCamera.MV_CC_OpenDevice_NET() != MyCamera.MV_OK)
     {
         hikvisionCamera.MV_CC_DestroyDevice_NET();
         return(false);
     }
     MyCamera.cbOutputExdelegate ImageCallback = new MyCamera.cbOutputExdelegate(StreamGrabber_ImageGrabber);
     if (hikvisionCamera.MV_CC_RegisterImageCallBackEx_NET(ImageCallback, IntPtr.Zero) != MyCamera.MV_OK)
     {
         return(false);
     }
     if (deviceInfo.nTLayerType == MyCamera.MV_GIGE_DEVICE)//探测网络最佳包大小(只对Gige相机有效)
     {
         int nPacketSize = hikvisionCamera.MV_CC_GetOptimalPacketSize_NET();
         if (nPacketSize > 0)
         {
             if (hikvisionCamera.MV_CC_SetIntValue_NET("GevSCPSPacketSize", (uint)nPacketSize) != MyCamera.MV_OK)
             {
                 return(false);
             }
         }
         else
         {
             return(false);
         }
     }
     return(true);
 }
コード例 #21
0
        private void bnOpen_Click(object sender, EventArgs e)
        {
            if (m_pDeviceList.nDeviceNum == 0 || cbDeviceList.SelectedIndex == -1)
            {
                ShowErrorMsg("No device, please select", 0);
                return;
            }
            int nRet = -1;

            //ch:获取选择的设备信息 | en:Get selected device information
            MyCamera.MV_CC_DEVICE_INFO device =
                (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_pDeviceList.pDeviceInfo[cbDeviceList.SelectedIndex],
                                                                   typeof(MyCamera.MV_CC_DEVICE_INFO));

            //ch:打开设备 | en:Open device
            //ch:打开设备 | en:Open Device
            nRet = m_pMyCamera.MV_CC_CreateDevice_NET(ref device);
            if (MyCamera.MV_OK != nRet)
            {
                ShowErrorMsg("Create Camera failed", nRet);
                m_pMyCamera.MV_CC_CloseDevice_NET();
                return;
            }

            nRet = m_pMyCamera.MV_CC_OpenDevice_NET();
            if (MyCamera.MV_OK != nRet)
            {
                ShowErrorMsg("Device open fail!", nRet);
                m_pMyCamera.MV_CC_CloseDevice_NET();
                return;
            }

            //ch:设置采集连续模式 | en:Set Continues Aquisition Mode
            m_pMyCamera.MV_CC_SetEnumValue_NET("AcquisitionMode", 2);
            m_pMyCamera.MV_CC_SetEnumValue_NET("TriggerMode", 0);

            //ch:控件操作 | en:Control Operation
            SetCtrlWhenOpen();
        }
コード例 #22
0
 public MyCamera.MV_CC_DEVICE_INFO CameraDevice(string name)
 {
     /*创建设备列表*/
     System.GC.Collect();
     EnumDevices(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref m_pDeviceList);
     //在窗体列表中显示设备名
     for (int i = 0; i < m_pDeviceList.nDeviceNum; i++)
     {
         MyCamera.MV_CC_DEVICE_INFO device = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_pDeviceList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO));
         if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)
         {
             IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(device.SpecialInfo.stGigEInfo, 0);
             MyCamera.MV_GIGE_DEVICE_INFO gigeInfo = (MyCamera.MV_GIGE_DEVICE_INFO)Marshal.PtrToStructure(buffer, typeof(MyCamera.MV_GIGE_DEVICE_INFO));
             if (name == gigeInfo.chUserDefinedName || name == gigeInfo.chSerialNumber)
             {
                 bitmapInfo.CameraName = name;
                 return(device);
             }
         }
     }
     throw new ArgumentException("未找到相机: " + name);
 }
コード例 #23
0
        public void OpenCameraDevice()
        {
            if (m_CameraList.nDeviceNum == 0 || m_CameraListComboBox.SelectedIndex == -1)
            {
                MessageBox.Show("无设备可选择!", "警告");
                return;
            }

            //获取选择的设备信息
            MyCamera.MV_CC_DEVICE_INFO device = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_CameraList.pDeviceInfo[m_CameraListComboBox.SelectedIndex], typeof(MyCamera.MV_CC_DEVICE_INFO));
            int nRet = m_CameraOperator.Open(ref device);

            if (MyCamera.MV_OK != nRet)
            {
                MessageBox.Show("设备打开失败!", "警告");
                return;
            }

            m_CameraOperator.SetEnumValue("AcquisitionMode", 2); // 工作在连续模式
            m_CameraOperator.SetEnumValue("TriggerMode", 0);     // 连续模式

            CameraStartGrab(m_CameraPictureBox);
        }
コード例 #24
0
        public int OpenDevice(int index)
        {
            int nRet = HIK_ER;

            if (nRet == index)
            {
                return(HIK_ER);
            }
            MyCamera.MV_CC_DEVICE_INFO device = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(pDeviceList.pDeviceInfo[index], typeof(MyCamera.MV_CC_DEVICE_INFO));
            if (null == pCamera)
            {
                pCamera = new MyCamera();
                if (null == pCamera)
                {
                    return(HIK_ER);
                }
            }
            nRet = pCamera.MV_CC_CreateDevice_NET(ref device);
            if (MyCamera.MV_OK != nRet)
            {
                return(HIK_ER);
            }
            nRet = pCamera.MV_CC_OpenDevice_NET();
            if (MyCamera.MV_OK != nRet)
            {
                pCamera.MV_CC_DestroyDevice_NET();
                return(HIK_ER);
            }
            pCamera.MV_CC_SetEnumValue_NET("AcquisitionMode", 2);
            pCamera.MV_CC_SetEnumValue_NET("TriggerMode", 0);
            nRet = pCamera.MV_CC_StartGrabbing_NET();
            if (MyCamera.MV_OK != nRet)
            {
                return(HIK_ER);
            }
            return(HIK_OK);
        }
コード例 #25
0
        // 枚举相机
        public List <string> EnumDevices()
        {
            uint          num            = CameraNum();
            List <string> allCameraInfos = new List <string>();

            for (int i = 0; i < num; i++)
            {
                MyCamera.MV_CC_DEVICE_INFO device = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(deviceList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO));
                if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)
                {
                    MyCamera.MV_GIGE_DEVICE_INFO gigeInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(device.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_GIGE_DEVICE_INFO));
                    if (gigeInfo.chUserDefinedName != "")
                    {
                        allCameraInfos.Add("GEV: " + gigeInfo.chUserDefinedName);
                    }
                    else
                    {
                        allCameraInfos.Add("GEV: " + gigeInfo.chManufacturerName + " " + gigeInfo.chModelName);
                    }
                }
                else if (device.nTLayerType == MyCamera.MV_USB_DEVICE)
                {
                    MyCamera.MV_USB3_DEVICE_INFO usbInfo = (MyCamera.MV_USB3_DEVICE_INFO)MyCamera.ByteToStruct(device.SpecialInfo.stUsb3VInfo, typeof(MyCamera.MV_USB3_DEVICE_INFO));

                    if (usbInfo.chUserDefinedName != "")
                    {
                        allCameraInfos.Add("U3V: " + usbInfo.chUserDefinedName);
                    }
                    else
                    {
                        allCameraInfos.Add("U3V: " + usbInfo.chManufacturerName + " " + usbInfo.chModelName);
                    }
                }
            }
            return(allCameraInfos);
        }
コード例 #26
0
        public static bool Open()
        {
            try
            {
                int cameraCount = 0;
                if ((CameraFlag & CameraFlag.Basler) == CameraFlag.Basler)
                {
                    //#region basler相机遍历查询

                    //List<Basler.Pylon.ICameraInfo> g_allCameras = Basler.Pylon.CameraFinder.Enumerate();

                    //if (g_allCameras.Count > 0)
                    //{
                    //    for (int i = 0; i < g_allCameras.Count; i++)
                    //    {
                    //        Basler.Pylon.Camera g_camera = new Basler.Pylon.Camera(g_allCameras[i]);
                    //        if (true == g_camera.IsOpen)
                    //            g_camera.Close();

                    //        g_camera.Open();
                    //        string id = g_camera.Parameters[Basler.Pylon.PLCamera.DeviceUserID].GetValue();
                    //        int id1;
                    //        if (int.TryParse(id, out id1))
                    //        {
                    //            if (CameraDic.Keys.Contains(id1) == false)
                    //            {
                    //                CameraDic.Add(id1, new BaslerCamera(g_camera, id1));
                    //                cameraCount++;
                    //            }
                    //        }
                    //        else
                    //        {
                    //            Util.Notify("相机ID未设置");
                    //        }
                    //    }
                    //}

                    //#endregion
                }
                if ((CameraFlag & CameraFlag.Hikvision) == CameraFlag.Hikvision)
                {
                    #region 海康相机遍历枚举
                    MyCamera.MV_CC_DEVICE_INFO_LIST m_pDeviceList = new MyCamera.MV_CC_DEVICE_INFO_LIST();
                    int nRet;


                    nRet = CameraOperator.EnumDevices(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref m_pDeviceList);
                    if (0 == nRet && m_pDeviceList.nDeviceNum > 0)
                    {
                        for (int i = 0; i < m_pDeviceList.nDeviceNum; i++)
                        {
                            MyCamera.MV_CC_DEVICE_INFO device =
                                (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_pDeviceList.pDeviceInfo[i],
                                                                                   typeof(MyCamera.MV_CC_DEVICE_INFO));

                            if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)
                            {
                                IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(device.SpecialInfo.stGigEInfo, 0);
                                MyCamera.MV_GIGE_DEVICE_INFO gigeInfo = (MyCamera.MV_GIGE_DEVICE_INFO)Marshal.PtrToStructure(buffer, typeof(MyCamera.MV_GIGE_DEVICE_INFO));

                                int key;

                                if (int.TryParse(gigeInfo.chUserDefinedName, out key))
                                {
                                    if (CameraDic.Keys.Contains(key) == false)
                                    {
                                        CameraOperator m_pOperator = new CameraOperator();
                                        //打开设备
                                        nRet = m_pOperator.Open(ref device);
                                        //Util.Notify("打开相机");
                                        if (MyCamera.MV_OK == nRet)
                                        {
                                            CameraDic.Add(key, new HikvisionCamera(m_pOperator, key));
                                            cameraCount++;
                                        }
                                    }
                                }
                                else
                                {
                                    Util.Notify("相机ID未设置");
                                }
                            }
                            else if (device.nTLayerType == MyCamera.MV_USB_DEVICE)
                            {
                                IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(device.SpecialInfo.stUsb3VInfo, 0);
                                MyCamera.MV_USB3_DEVICE_INFO usbInfo = (MyCamera.MV_USB3_DEVICE_INFO)Marshal.PtrToStructure(buffer, typeof(MyCamera.MV_USB3_DEVICE_INFO));

                                int key;

                                if (int.TryParse(usbInfo.chUserDefinedName, out key))
                                {
                                    if (CameraDic.Keys.Contains(key) == false)
                                    {
                                        CameraOperator m_pOperator = new CameraOperator();
                                        //打开设备
                                        nRet = m_pOperator.Open(ref device);
                                        if (MyCamera.MV_OK == nRet)
                                        {
                                            CameraDic.Add(key, new HikvisionCamera(m_pOperator, key));
                                            cameraCount++;
                                        }
                                    }
                                    else
                                    {
                                        Util.Notify("相机ID未设置");
                                    }
                                }
                            }
                        }
                    }

                    #endregion
                }
                if ((CameraFlag & CameraFlag.Mindvision) == CameraFlag.Mindvision)
                {
                    #region 迈徳威视相机遍历枚举

                    CameraSdkStatus     status;
                    tSdkCameraDevInfo[] tCameraDevInfoList;

                    status = MvApi.CameraEnumerateDevice(out tCameraDevInfoList);
                    if (status == CameraSdkStatus.CAMERA_STATUS_SUCCESS)
                    {
                        if (tCameraDevInfoList != null && tCameraDevInfoList.Count() > 0)//此时iCameraCounts返回了实际连接的相机个数。如果大于1,则初始化第一个相机
                        {
                            for (int i = 0; i < tCameraDevInfoList.Count(); i++)
                            {
                                IntPtr m_Grabber;
                                status = MvApi.CameraGrabber_Create(out m_Grabber, ref tCameraDevInfoList[i]);
                                if (status == CameraSdkStatus.CAMERA_STATUS_SUCCESS)
                                {
                                    CameraHandle m_hCamera = 0;             // 句柄
                                    MvApi.CameraGrabber_GetCameraHandle(m_Grabber, out m_hCamera);
                                    byte[] pName = new byte[255];
                                    MvApi.CameraGetFriendlyName(m_hCamera, pName);
                                    string str = System.Text.Encoding.ASCII.GetString(pName).Substring(0, 1);

                                    int key = -1;

                                    if (int.TryParse(str, out key) && CameraDic.Keys.Contains(key) == false)
                                    {
                                        CameraDic.Add(key, new MindCamera(m_hCamera, m_Grabber, key));
                                        cameraCount++;
                                    }
                                    //else
                                    //{
                                    //    Util.Notify("相机ID未设置");
                                    //}
                                }
                            }
                        }
                    }

                    #endregion
                }
                if ((CameraFlag & CameraFlag.Microvision) == CameraFlag.Microvision)
                {
                    #region 维视图像相机相机遍历枚举

                    //个数枚举
                    MVGigE.MVInitLib();
                    int      CamNum = 0;
                    MVSTATUS r      = MVGigE.MVGetNumOfCameras(out CamNum);
                    for (int i = 0; i < CamNum; i++)
                    {
                        IntPtr m_hCam;
                        byte   index = (byte)i;
                        r = MVGigE.MVOpenCamByIndex(index, out m_hCam);
                        if (m_hCam != IntPtr.Zero)
                        {
                            MVCamInfo pCamInfo;
                            MVGigE.MVGetCameraInfo(index, out pCamInfo);

                            string str = pCamInfo.mUserDefinedName;

                            int key = -1;

                            if (int.TryParse(str, out key) && CameraDic.Keys.Contains(key) == false)
                            {
                                CameraDic.Add(key, new MicrovisionCamera(m_hCam, key));
                                cameraCount++;
                            }
                            else
                            {
                                Util.Notify("相机ID未设置");
                            }
                        }
                    }

                    #endregion
                }
                if ((CameraFlag & CameraFlag.ICImaging) == CameraFlag.ICImaging)
                {
                    #region  美精相机遍历查询
                    TIS.Imaging.ICImagingControl cameraFind = new TIS.Imaging.ICImagingControl();

                    foreach (TIS.Imaging.Device Item in cameraFind.Devices)
                    {
                        string cameraName = Item.Name;
                        int    index1     = cameraName.IndexOf("[");
                        if (index1 > -1)
                        {
                            string dat  = cameraName.Substring(index1);
                            string dat1 = dat.Replace("[", "").Replace("]", "");
                            int    id1;
                            if (int.TryParse(dat1, out id1))
                            {
                                if (CameraDic.Keys.Contains(id1) == false)
                                {
                                    TIS.Imaging.ICImagingControl cameraTmp = new TIS.Imaging.ICImagingControl();
                                    cameraTmp.Device = Item.Name;
                                    bool x = cameraTmp.DeviceValid;
                                    CameraDic.Add(id1, new ICImagingCamera(cameraTmp, id1));
                                    cameraCount++;
                                }
                            }
                        }
                    }

                    #endregion
                }
                if ((CameraFlag & CameraFlag.DirectShow) == CameraFlag.DirectShow)
                {
                    #region DirectShow相机 当前只支持一个相机
                    HFramegrabber framegrabber = new HFramegrabber();
                    //HTuple Information = null;
                    //HTuple ValueList = null;
                    //HOperatorSet.InfoFramegrabber("DirectShow", "info_boards",out Information,out ValueList);
                    framegrabber.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, DirectShowColorSpace,
                                                  -1, "false", DirectShowCameraType, DirectShowIndex, 0, -1);
                    CameraDic.Add(1, new DirectShowCamera(framegrabber, 1));
                    #endregion
                }
                if ((CameraFlag & CameraFlag.DaHeng) == CameraFlag.DaHeng)
                {
                    //#region 大恒相机
                    //List<IGXDeviceInfo> listGXDeviceInfo = new List<IGXDeviceInfo>();
                    //IGXFactory IGXFactory1 = IGXFactory.GetInstance();
                    //IGXFactory1.Init();
                    //IGXFactory1.UpdateDeviceList(200, listGXDeviceInfo);
                    //foreach (var item in listGXDeviceInfo)
                    //{
                    //    string name = item.GetUserID();
                    //    int key;

                    //    if (int.TryParse(name, out key))
                    //    {
                    //        if (CameraDic.Keys.Contains(key) == false)
                    //        {
                    //            IGXDevice IGXDevice1 = null;
                    //            IGXDevice1 = IGXFactory1.OpenDeviceBySN(item.GetSN(), GX_ACCESS_MODE.GX_ACCESS_EXCLUSIVE);
                    //            CameraDic.Add(key, new DHCamera(IGXFactory1, IGXDevice1));

                    //        }
                    //    }

                    //}

                    //#endregion
                }

                if ((CameraFlag & CameraFlag.Gige) == CameraFlag.Gige)
                {
                    //#region Gige相机遍历查询

                    HTuple Information = null;
                    HTuple ValueList   = null;
                    HOperatorSet.InfoFramegrabber("GigEVision", "info_boards", out Information, out ValueList);
                    for (int i = 0; i < ValueList.Length; i++)
                    {
                        string valueTmp = ValueList[i];
                        string name     = GetGigeValue(valueTmp, "user_name:");

                        string device = GetGigeValue(valueTmp, "device:");
                        if (name != null && device != null)
                        {
                            int id1;
                            if (int.TryParse(name, out id1))
                            {
                                if (CameraDic.Keys.Contains(id1) == false)
                                {
                                    HFramegrabber grabber = new HFramegrabber();
                                    grabber.OpenFramegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "default", -1,
                                                             "default", -1, "false", "default", device, 0, -1);
                                    CameraDic.Add(id1, new GigeCamera(grabber, id1));
                                    cameraCount++;
                                }
                            }
                        }
                    }
                    //List<ICameraInfo> g_allCameras = CameraFinder.Enumerate();

                    //if (g_allCameras.Count > 0)
                    //{
                    //    for (int i = 0; i < g_allCameras.Count; i++)
                    //    {
                    //        Basler.Pylon.Camera g_camera = new Basler.Pylon.Camera(g_allCameras[i]);
                    //        if (true == g_camera.IsOpen)
                    //            g_camera.Close();

                    //        g_camera.Open();
                    //        string id = g_camera.Parameters[PLCamera.DeviceUserID].GetValue();
                    //        int id1;
                    //        if (int.TryParse(id, out id1))
                    //        {
                    //            if (CameraDic.Keys.Contains(id1) == false)
                    //            {
                    //                CameraDic.Add(id1, new BaslerCamera(g_camera, id1));
                    //                cameraCount++;
                    //            }
                    //        }
                    //        else
                    //        {
                    //            Util.Notify("相机ID未设置");
                    //        }
                    //    }
                    //}

                    //#endregion
                }
                //打开相机
                foreach (var item in CameraDic.Values)
                {
                    item.Open();
                    Thread.Sleep(30);
                }

                if (CameraInitFinishEvent != null)
                {
                    CameraInitFinishEvent(null, null);
                }
                //else
                //{
                //    Util.Notify("无相机连接");
                //    return false;
                //}
            }
            catch (Exception ex)
            {
                Util.WriteLog(typeof(CameraManger), ex);
                Util.Notify("相机打开出现异常");
                Close();
                return(false);
            }
            return(true);
        }
コード例 #27
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();
        }
コード例 #28
0
ファイル: ReconnectDemo.cs プロジェクト: HUT-17/HUT
        private void bnOpen_Click(object sender, EventArgs e)
        {
            if (m_stDeviceList.nDeviceNum == 0 || cbDeviceList.SelectedIndex == -1)
            {
                ShowErrorMsg("No Device,please Select", 0);
                return;
            }

            //ch:获取选择的设备信息 | en:Get Used Device Info
            MyCamera.MV_CC_DEVICE_INFO device =
                (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_stDeviceList.pDeviceInfo[cbDeviceList.SelectedIndex],
                                                                   typeof(MyCamera.MV_CC_DEVICE_INFO));

            //ch:打开设备 | en:Open Device
            if (null == m_MyCamera)
            {
                m_MyCamera = new MyCamera();
                if (null == m_MyCamera)
                {
                    return;
                }
            }

            int nRet = m_MyCamera.MV_CC_CreateDevice_NET(ref device);

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

            nRet = m_MyCamera.MV_CC_OpenDevice_NET();
            if (MyCamera.MV_OK != nRet)
            {
                m_MyCamera.MV_CC_DestroyDevice_NET();
                ShowErrorMsg("Device open fail!", nRet);
                return;
            }

            // ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
            if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)
            {
                int nPacketSize = m_MyCamera.MV_CC_GetOptimalPacketSize_NET();
                if (nPacketSize > 0)
                {
                    nRet = m_MyCamera.MV_CC_SetIntValue_NET("GevSCPSPacketSize", (uint)nPacketSize);
                    if (nRet != MyCamera.MV_OK)
                    {
                        ShowErrorMsg("Set Packet Size failed!", nRet);
                    }
                }
                else
                {
                    ShowErrorMsg("Set Packet Size failed!", nRet);
                }
            }

            // ch:注册异常回调函数 | en:Register Exception Callback
            nRet = m_MyCamera.MV_CC_RegisterExceptionCallBack_NET(pCallBackFunc, IntPtr.Zero);
            if (MyCamera.MV_OK != nRet)
            {
                ShowErrorMsg("Register expection callback failed!", nRet);
            }
            GC.KeepAlive(pCallBackFunc);

            //ch:控件操作 | en:Control Operation
            SetCtrlWhenOpen();
            cbDeviceList.Enabled = false;
            bnEnum.Enabled       = false;
        }
コード例 #29
0
        private void bnOpen_Click(object sender, EventArgs e)
        {
            if (m_stDeviceList.nDeviceNum == 0 || cbDeviceList.SelectedIndex == -1)
            {
                ShowErrorMsg("No device, please select", 0);
                return;
            }

            // ch:获取选择的设备信息 | en:Get selected device information
            MyCamera.MV_CC_DEVICE_INFO device =
                (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_stDeviceList.pDeviceInfo[cbDeviceList.SelectedIndex],
                                                                   typeof(MyCamera.MV_CC_DEVICE_INFO));

            // ch:打开设备 | en:Open device
            if (null == m_MyCamera)
            {
                m_MyCamera = new MyCamera();
                if (null == m_MyCamera)
                {
                    return;
                }
            }

            int nRet = m_MyCamera.MV_CC_CreateDevice_NET(ref device);

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

            nRet = m_MyCamera.MV_CC_OpenDevice_NET();
            if (MyCamera.MV_OK != nRet)
            {
                m_MyCamera.MV_CC_DestroyDevice_NET();
                ShowErrorMsg("Device open fail!", nRet);
                return;
            }

            // ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
            if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)
            {
                int nPacketSize = m_MyCamera.MV_CC_GetOptimalPacketSize_NET();
                if (nPacketSize > 0)
                {
                    nRet = m_MyCamera.MV_CC_SetIntValue_NET("GevSCPSPacketSize", (uint)nPacketSize);
                    if (nRet != MyCamera.MV_OK)
                    {
                        ShowErrorMsg("Set Packet Size failed!", nRet);
                    }
                }
                else
                {
                    ShowErrorMsg("Get Packet Size failed!", nPacketSize);
                }
            }

            // ch:设置采集连续模式 | en:Set Continues Aquisition Mode
            m_MyCamera.MV_CC_SetEnumValue_NET("AcquisitionMode", (uint)MyCamera.MV_CAM_ACQUISITION_MODE.MV_ACQ_MODE_CONTINUOUS);
            m_MyCamera.MV_CC_SetEnumValue_NET("TriggerMode", (uint)MyCamera.MV_CAM_TRIGGER_MODE.MV_TRIGGER_MODE_OFF);

            bnGetParam_Click(null, null);// ch:获取参数 | en:Get parameters

            // ch:控件操作 | en:Control operation
            SetCtrlWhenOpen();
        }
コード例 #30
0
        // ch:初始化、打开相机 | en:Initialization and open devices
        private void bnOpen_Click(object sender, EventArgs e)
        {
            bool bOpened = false;

            // ch:判断输入格式是否正确 | en:Determine whether the input format is correct
            try
            {
                int.Parse(tbUseNum.Text);
            }
            catch
            {
                ShowErrorMsg("Please enter correct format!", 0);
                return;
            }
            // ch:获取使用设备的数量 | en:Get Used Device Number
            int nCameraUsingNum = int.Parse(tbUseNum.Text);

            // ch:参数检测 | en:Parameters inspection
            if (nCameraUsingNum <= 0)
            {
                nCameraUsingNum = 1;
            }
            if (nCameraUsingNum > 4)
            {
                nCameraUsingNum = 4;
            }

            int nRet = -1;

            for (int i = 0, j = 0; j < m_nDevNum; ++i, ++j)
            {
                //ch:获取选择的设备信息 | en:Get Selected Device Information
                MyCamera.MV_CC_DEVICE_INFO device =
                    (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_pDeviceList.pDeviceInfo[j],
                                                                       typeof(MyCamera.MV_CC_DEVICE_INFO));

                //ch:打开设备 | en:Open Device
                if (null == m_pMyCamera[i])
                {
                    m_pMyCamera[i] = new MyCamera();
                    if (null == m_pMyCamera[i])
                    {
                        return;
                    }
                }

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

                nRet = m_pMyCamera[i].MV_CC_OpenDevice_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    i--;
                }
                else
                {
                    m_nCanOpenDeviceNum++;
                    m_pDeviceInfo[i] = device;
                    // ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
                    if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)
                    {
                        int nPacketSize = m_pMyCamera[i].MV_CC_GetOptimalPacketSize_NET();
                        if (nPacketSize > 0)
                        {
                            nRet = m_pMyCamera[i].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);
                        }
                    }

                    m_pMyCamera[i].MV_CC_SetEnumValue_NET("TriggerMode", (uint)MyCamera.MV_CAM_TRIGGER_MODE.MV_TRIGGER_MODE_OFF);
                    m_pMyCamera[i].MV_CC_RegisterImageCallBackEx_NET(cbImage, (IntPtr)i);
                    bOpened = true;
                    if (m_nCanOpenDeviceNum == nCameraUsingNum)
                    {
                        break;
                    }
                }
            }

            // ch:只要有一台设备成功打开 | en:As long as there is a device successfully opened
            if (bOpened)
            {
                tbUseNum.Text = m_nCanOpenDeviceNum.ToString();
                SetCtrlWhenOpen();
            }
        }