Example #1
0
        private void InitCamera()
        {
            CameraSdkStatus status = 0;

            tSdkCameraDevInfo[] DevList;
            MvApi.CameraEnumerateDevice(out DevList);
            int NumDev = (DevList != null ? DevList.Length : 0);

            if (NumDev < 1)
            {
                MessageBox.Show("未扫描到相机");
                return;
            }
            else if (NumDev == 1)
            {
                status = MvApi.CameraGrabber_Create(out m_Grabber, ref DevList[0]);
            }
            else //选择哪个相机
            {
                status = MvApi.CameraGrabber_CreateFromDevicePage(out m_Grabber);
            }

            if (status == 0)
            {
                //获得相机的描述信息
                MvApi.CameraGrabber_GetCameraDevInfo(m_Grabber, out m_DevInfo);
                //获得相机的句柄
                MvApi.CameraGrabber_GetCameraHandle(m_Grabber, out m_hCamera);
                //sheshe设置相机配置窗口的启动页面
                MvApi.CameraCreateSettingPage(m_hCamera, this.Handle, m_DevInfo.acFriendlyName, null, (IntPtr)0, 0);
                //设置RGB回调函数
                MvApi.CameraGrabber_SetRGBCallback(m_Grabber, m_FrameCallback, IntPtr.Zero);
                tSdkCameraCapbility cap;
                //黑白相机设置ISP输出灰度图像
                //彩色相机ISP默认输出BGR24图像

                //获得相机特性
                MvApi.CameraGetCapability(m_hCamera, out cap);
                //表示该型号相机是否为黑白相机,如果是黑白相机,则颜色相关的功能都无法调节
                if (cap.sIspCapacity.bMonoSensor != 0)
                {
                    MvApi.CameraSetIspOutFormat(m_hCamera, (uint)MVSDK.emImageFormat.CAMERA_MEDIA_TYPE_MONO8);
                    //创建灰度调色板
                    Bitmap Image = new Bitmap(1, 1, PixelFormat.Format8bppIndexed);
                    m_GrayPal = Image.Palette;
                    for (int Y = 0; Y < m_GrayPal.Entries.Length; Y++)
                    {
                        m_GrayPal.Entries[Y] = Color.FromArgb(255, Y, Y, Y);
                    }
                }
                //设置vflip,由于sdk输出的数据默认是从底部到顶部,打开flip后就可以直接转成bitmap
                MvApi.CameraSetMirror(m_hCamera, 1, 1);
                // MvApi.CameraGrabber_StartLive(m_Grabber);
            }
            else
            {
                MessageBox.Show(String.Format("打开相机失败,原因:{0}", status));
            }
        }
Example #2
0
        private int InitCamera()
        {
            int m_hCamera = -1;

            tSdkCameraDevInfo[] m_DevInfo = new tSdkCameraDevInfo[] { new tSdkCameraDevInfo() };
            MvApi.CameraEnumerateDevice(out m_DevInfo);
            if (m_DevInfo != null)
            {
                IntPtr m_Grabber = new IntPtr();

                if (MvApi.CameraGrabber_Create(out m_Grabber, ref m_DevInfo[0]) == CameraSdkStatus.CAMERA_STATUS_SUCCESS)
                {
                    MvApi.CameraGrabber_GetCameraHandle(m_Grabber, out m_hCamera);
                }
            }
            return(m_hCamera);
        }
Example #3
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);
        }
Example #4
0
        private bool InitCamera()
        {
            CameraSdkStatus status = 0;

            tSdkCameraDevInfo[] DevList;
            MvApi.CameraEnumerateDevice(out DevList);
            int NumDev = (DevList != null ? DevList.Length : 0);

            if (NumDev < 1)
            {
                System.Windows.MessageBox.Show("未扫描到相机");
                return(false);
            }
            else if (NumDev == 1)
            {
                status = MvApi.CameraGrabber_Create(out m_Grabber, ref DevList[0]);
            }
            else
            {
                status = MvApi.CameraGrabber_CreateFromDevicePage(out m_Grabber);
            }

            if (status == 0)
            {
                MvApi.CameraGrabber_GetCameraDevInfo(m_Grabber, out m_DevInfo);
                MvApi.CameraGrabber_GetCameraHandle(m_Grabber, out m_hCamera);

                var handle = (new WindowInteropHelper(window)).Handle;
                MvApi.CameraCreateSettingPage(m_hCamera, handle, m_DevInfo.acFriendlyName, null, (IntPtr)0, 0);

                MvApi.CameraGrabber_SetRGBCallback(m_Grabber, m_FrameCallback, IntPtr.Zero);

                // 黑白相机设置ISP输出灰度图像
                // 彩色相机ISP默认会输出BGR24图像
                tSdkCameraCapbility cap;
                MvApi.CameraGetCapability(m_hCamera, out cap);
                if (cap.sIspCapacity.bMonoSensor != 0)
                {
                    MvApi.CameraSetIspOutFormat(m_hCamera, (uint)MVSDK.emImageFormat.CAMERA_MEDIA_TYPE_MONO8);

                    // 创建灰度调色板
                    Bitmap Image = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
                    m_GrayPal = Image.Palette;
                    for (int Y = 0; Y < m_GrayPal.Entries.Length; Y++)
                    {
                        m_GrayPal.Entries[Y] = System.Drawing.Color.FromArgb(255, Y, Y, Y);
                    }
                }

                // 设置VFlip,由于SDK输出的数据默认是从底到顶的,打开VFlip后就可以直接转换为Bitmap
                MvApi.CameraSetMirror(m_hCamera, 1, 1);


                // 设置相机预设分辨率
                tSdkImageResolution t;
                MvApi.CameraGetImageResolution(m_hCamera, out t);
                t.iIndex = 8;//切换预设分辨率, 只需要设定index值就行了。640*480 mono bin
                MvApi.CameraSetImageResolution(m_hCamera, ref t);

                // 为了演示如何在回调中使用相机数据创建Bitmap并显示到PictureBox中,这里不使用SDK内置的绘制操作
                //MvApi.CameraGrabber_SetHWnd(m_Grabber, this.DispWnd.Handle);

                MvApi.CameraGrabber_StartLive(m_Grabber);
                return(true);
            }
            else
            {
                System.Windows.MessageBox.Show(String.Format("打开相机失败,原因:{0}", status));
            }
            return(false);
        }