Пример #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);
                MvApi.CameraCreateSettingPage(m_hCamera, this.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, 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输出的数据默认是从底到顶的,打开VFlip后就可以直接转换为Bitmap
                MvApi.CameraSetMirror(m_hCamera, 1, 1);

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

                MvApi.CameraGrabber_StartLive(m_Grabber);
            }
            else
            {
                MessageBox.Show(String.Format("打开相机失败,原因:{0}", status));
            }
        }
Пример #2
0
        public Form2()
        {
            InitializeComponent();

            m_FrameCallback[0]  = new pfnCameraGrabberFrameCallback(CameraGrabberFrameCallback0);
            m_FrameCallback[1]  = new pfnCameraGrabberFrameCallback(CameraGrabberFrameCallback1);
            m_FrameCallback[2]  = new pfnCameraGrabberFrameCallback(CameraGrabberFrameCallback2);
            m_FrameCallback[3]  = new pfnCameraGrabberFrameCallback(CameraGrabberFrameCallback3);
            m_SaveImageComplete = new pfnCameraGrabberSaveImageComplete(CameraGrabberSaveImageComplete2);

            MvApi.CameraEnumerateDevice(out m_DevInfo);
            int NumDev = (m_DevInfo != null ? Math.Min(m_DevInfo.Length, CAMERA_NUM) : 0);

            IntPtr[] hDispWnds = { this.DispWnd1.Handle, this.DispWnd2.Handle, this.DispWnd3.Handle, this.DispWnd4.Handle };
            for (int i = 0; i < NumDev; ++i)
            {
                if (MvApi.CameraGrabber_Create(out m_Grabber[i], ref m_DevInfo[i]) == CameraSdkStatus.CAMERA_STATUS_SUCCESS)
                {
                    MvApi.CameraGrabber_GetCameraHandle(m_Grabber[i], out m_hCamera[i]);
                    MvApi.CameraCreateSettingPage(m_hCamera[i], this.Handle, m_DevInfo[i].acFriendlyName, null, (IntPtr)0, 0);

                    MvApi.CameraGrabber_SetRGBCallback(m_Grabber[i], m_FrameCallback[i], IntPtr.Zero);
                    MvApi.CameraGrabber_SetSaveImageCompleteCallback(m_Grabber[i], m_SaveImageComplete, IntPtr.Zero);

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

                    MvApi.CameraGrabber_SetHWnd(m_Grabber[i], hDispWnds[i]);
                }
            }
            for (int i = 0; i < NumDev; ++i)
            {
                if (m_Grabber[i] != IntPtr.Zero)
                {
                    MvApi.CameraGrabber_StartLive(m_Grabber[i]);
                }
            }
        }
Пример #3
0
        public bool cameraopen()
        {
            return(false);

#if USE_CALL_BACK
            CAMERA_SNAP_PROC pCaptureCallOld = null;
#endif
            if (m_hCamera > 0)
            {
                return(true);
            }
            if ((tCameraDevInfoList != null) && (tCameraDevInfoList.Length > 0))
            {
                //已经初始化过,直接返回 true
                return(true);
            }
            status = MvApi.CameraEnumerateDevice(out tCameraDevInfoList);
            //hfgon = true;
            if (status == CameraSdkStatus.CAMERA_STATUS_SUCCESS)
            {
                //hfgon = true;
                if (tCameraDevInfoList != null)//此时iCameraCounts返回了实际连接的相机个数。如果大于1,则初始化第一个相机
                {
                    status = MvApi.CameraInit(ref tCameraDevInfoList[0], -1, -1, ref m_hCamera);
                    if (status == CameraSdkStatus.CAMERA_STATUS_SUCCESS)
                    {
                        //获得相机特性描述
                        MvApi.CameraGetCapability(m_hCamera, out tCameraCapability);

                        m_ImageBuffer         = Marshal.AllocHGlobal(tCameraCapability.sResolutionRange.iWidthMax * tCameraCapability.sResolutionRange.iHeightMax * 3 + 1024);
                        m_ImageBufferSnapshot = Marshal.AllocHGlobal(tCameraCapability.sResolutionRange.iWidthMax * tCameraCapability.sResolutionRange.iHeightMax * 3 + 1024);

                        if (tCameraCapability.sIspCapacity.bMonoSensor != 0)
                        {
                            // 黑白相机输出8位灰度数据
                            MvApi.CameraSetIspOutFormat(m_hCamera, (uint)MVSDK.emImageFormat.CAMERA_MEDIA_TYPE_MONO8);
                        }

                        //设置抓拍通道的分辨率。
                        tSdkImageResolution tResolution;
                        tResolution.uSkipMode       = 0;
                        tResolution.uBinAverageMode = 0;
                        tResolution.uBinSumMode     = 0;
                        tResolution.uResampleMask   = 0;
                        tResolution.iVOffsetFOV     = 0;
                        tResolution.iHOffsetFOV     = 0;
                        tResolution.iWidthFOV       = tCameraCapability.sResolutionRange.iWidthMax;
                        tResolution.iHeightFOV      = tCameraCapability.sResolutionRange.iHeightMax;
                        tResolution.iWidth          = tResolution.iWidthFOV;
                        tResolution.iHeight         = tResolution.iHeightFOV;
                        //tResolution.iIndex = 0xff;表示自定义分辨率,如果tResolution.iWidth和tResolution.iHeight
                        //定义为0,则表示跟随预览通道的分辨率进行抓拍。抓拍通道的分辨率可以动态更改。
                        //本例中将抓拍分辨率固定为最大分辨率。
                        tResolution.iIndex        = 0xff;
                        tResolution.acDescription = new byte[32];//描述信息可以不设置
                        tResolution.iWidthZoomHd  = 0;
                        tResolution.iHeightZoomHd = 0;
                        tResolution.iWidthZoomSw  = 0;
                        tResolution.iHeightZoomSw = 0;

                        MvApi.CameraSetResolutionForSnap(m_hCamera, ref tResolution);
                        //try { if (exposuretime > 0) MvApi.CameraSetExposureTime(m_hCamera, exposuretime);  } catch { }
                        //try { if (gain > 0) MvApi.CameraSetGain(m_hCamera, Convert.ToInt32(gain / 100), Convert.ToInt32(gain / 100), Convert.ToInt32(gain / 100)); } catch { }
                        //try { if (contrast > 0) MvApi.CameraSetContrast(m_hCamera, contrast); } catch { }
                        //try { if (gamma > 0) MvApi.CameraSetGamma(m_hCamera, Convert.ToInt32(gamma)); } catch { }

                        //两种方式来获得预览图像,设置回调函数或者使用定时器或者独立线程的方式,
                        //主动调用CameraGetImageBuffer接口来抓图。
                        //本例中仅演示了两种的方式,注意,两种方式也可以同时使用,但是在回调函数中,
                        //不要使用CameraGetImageBuffer,否则会造成死锁现象。
#if USE_CALL_BACK
                        m_CaptureCallback = new CAMERA_SNAP_PROC(ImageCaptureCallback);
                        MvApi.CameraSetCallbackFunction(m_hCamera, m_CaptureCallback, m_iCaptureCallbackCtx, ref pCaptureCallOld);
#else //如果需要采用多线程,使用下面的方式
                        m_bExitCaptureThread = false;
                        //m_tCaptureThread = new Thread(new ThreadStart(CaptureThreadProc));
                        //m_tCaptureThread.Start();
                        return(true);
#endif
                    }
                    else
                    {
                        m_hCamera = 0;
                        String errstr    = string.Format("相机初始化错误,错误码{0},错误原因是", status);
                        String errstring = MvApi.CameraGetErrorString(status);
                        // string str1
                        MessageBox.Show(errstr + errstring, "ERROR");
                        Environment.Exit(0);
                        return(false);
                    }
                }
            }
            else
            {
                MessageBox.Show("没有找到相机,如果已经接上相机,可能是权限不够,请尝试使用管理员权限运行程序。");
                Environment.Exit(0);
            }
            return(false);

            //InitCamera();
            //if (cname == "") return;


            //hfgon = false;
            //try
            //{
            //    hfg = new HFramegrabber();
            //    //open_framegrabber ('GigEVision', 0, 0, 0, 0, 0, 0, 'default', -1, 'default', -1, 'false', 'default', 'xiangji1', 0, -1, AcqHandle)
            //    hfg.OpenFramegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "default", -1, "default", -1, "false", "default", cname, 0, -1);

            //    Thread.Sleep(300);
            //    try
            //    {
            //        if (cname.IndexOf("MindVision") > -1)
            //        {
            //            HOperatorSet.SetFramegrabberParam(hfg, "AcquisitionMode", "SingleFrame");
            //            HOperatorSet.SetFramegrabberParam(hfg, "AcquisitionFrameRate", "Mid");
            //        }
            //        try { if (exposuretime > 0) hfg.SetFramegrabberParam("ExposureTime", exposuretime); } catch { }
            //        try { if (gain > 0) hfg.SetFramegrabberParam("Gain", gain / 100); } catch { }
            //        try { if (contrast > 0) hfg.SetFramegrabberParam("Contrast", contrast); } catch { }
            //        try { if (gamma > 0) hfg.SetFramegrabberParam("Gamma", gamma); } catch { }
            //    }
            //    catch { }

            //    hfg.GrabImageStart(-1);
            //    //#himg = hfg.GrabImageAsync(-1);
            //    //#string ctype = "";
            //    //#himg.GetImagePointer1(out ctype, out iw, out ih);

            //    hfgon = true;
            //}
            //catch
            //{
            //    //MessageBox.Show("相机未连接!");
            //}
        }