Exemplo n.º 1
0
        public bool InitCamera()
        {
            const int MAX_CAMS = 2;

            tSdkCameraDevInfo[] sCameraList = null;
            int iCameraNums = MAX_CAMS;
            tSdkCameraCapbility sCameraInfo = new tSdkCameraCapbility();

            CameraSdkStatus status = MvApi.CameraEnumerateDevice(out sCameraList);

            if (status != CameraSdkStatus.CAMERA_STATUS_SUCCESS || iCameraNums == 0)
            {
                return(false);
            }

            //只假设连接了一个相机。
            if ((status = MvApi.CameraInit(ref sCameraList[0], -1, -1, ref m_hCamera)) != CameraSdkStatus.CAMERA_STATUS_SUCCESS)
            {
                string msg = string.Format("***Failed to init the camera! Error code is {0}", status);
                WinCall.TraceMessage(msg);
                return(false);
            }

            //Get properties description for this camera.
            MvApi.CameraGetCapability(m_hCamera, out sCameraInfo);
            m_ImageBufferSnapshot = Marshal.AllocHGlobal(sCameraInfo.sResolutionRange.iWidthMax * sCameraInfo.sResolutionRange.iHeightMax * 3 + 1024);
            return(true);
        }
Exemplo n.º 2
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));
            }
        }
        protected override void GetCameraSettingData()
        {
            try
            {
                tSdkCameraCapbility cap;
                MvApi.CameraGetCapability(m_hCamera, out cap);

                if (cap.sIspCapacity.bMonoSensor == 1)
                {
                    MvApi.CameraSetIspOutFormat(m_hCamera, (uint)emImageFormat.CAMERA_MEDIA_TYPE_MONO8);
                }
                else
                {
                    MvApi.CameraSetIspOutFormat(m_hCamera, (uint)emImageFormat.CAMERA_MEDIA_TYPE_RGB8);
                }
                //long max, min, cur;
                gainMin = cap.sExposeDesc.uiAnalogGainMin;
                gainMax = cap.sExposeDesc.uiAnalogGainMax;
                int piAnalogGain = 0;
                MvApi.CameraGetAnalogGain(m_hCamera, ref piAnalogGain);

                gainCur  = piAnalogGain;
                gainUnit = "";

                shuterUnit = "us";

                shuterMin = cap.sExposeDesc.uiExposeTimeMin;
                shuterMax = cap.sExposeDesc.uiExposeTimeMax;
                double pfLineTime = 0;
                MvApi.CameraGetExposureLineTime(m_hCamera, ref pfLineTime);

                shuterCur = (long)pfLineTime;

                triggerDelayAbsMin = 0;
                triggerDelayAbsMax = 1000000;
                uint puDelayTimeUs = 0;
                MvApi.CameraGetTriggerDelayTime(m_hCamera, ref puDelayTimeUs);

                triggerDelayAbs = puDelayTimeUs;

                lineDebouncerTimeAbsMin = 0;
                lineDebouncerTimeAbsMax = 5000;
                lineDebouncerTimeAbs    = 0;

                byte[] psCameraName = new byte[255];
                MvApi.CameraGetFriendlyName(m_hCamera, psCameraName);
                CameraName = System.Text.Encoding.ASCII.GetString(psCameraName);
            }
            catch (Exception ex)
            {
                Util.WriteLog(this.GetType(), ex);
                Util.Notify("相机设置信息获取异常");
            }
        }
Exemplo n.º 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);
        }