예제 #1
0
        private void Form1_Load(object sender, EventArgs e)
        {
            //1打开窗口、赋值线程状态
            //2枚举设备
            //3.初始化设备
            //3.申请内存  获取特性 获取位数
            //4.设置触发模式
            //5.开启绑定线程
            //6开始采集
            //6开始采集(默认连续,不设置采集模式的话)
            Control.CheckForIllegalCrossThreadCalls = false;
            HOperatorSet.OpenWindow(0, 0, pictureBox1.Width, pictureBox1.Height, pictureBox1.Handle, "", "", out hv_WindowHandle);
            HDevWindowStack.Push(hv_WindowHandle);
            status = MvApi.CameraEnumerateDevice(out tCameraDevInfoList);
            //枚举设备
            if (tCameraDevInfoList != null)//此时iCameraCounts返回了实际连接的相机个数。如果大于1,则初始化第一个相机
            {
                status = MvApi.CameraInit(ref tCameraDevInfoList[0], -1, -1, ref m_hCamera);
                //初始化第一个相机

                //获得相机特性描述
                MvApi.CameraGetCapability(m_hCamera, out tCameraCapability);

                //申请内存
                m_ImageBuffer = Marshal.AllocHGlobal(tCameraCapability.sResolutionRange.iWidthMax * tCameraCapability.sResolutionRange.iHeightMax * 3 + 1024);
                //MvApi.CameraSetTriggerMode(m_hCamera, 0);
                m_bExitCaptureThread = false;
                m_tCaptureThread     = new Thread(new ThreadStart(CaptureThreadProc));
                m_tCaptureThread.Start();
                MvApi.CameraPlay(m_hCamera);
            }
        }
예제 #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);
                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));
            }
        }
예제 #3
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]);
                }
            }
        }
예제 #4
0
        /// <summary>
        /// 根据设备列表,初始化单个相机
        /// </summary>
        /// <param name="tCameraDevInfoList">相机设备列表</param>
        /// <param name="cameraDevID">第i个相机</param>
        /// <param name="PreviewBox">相机对应的PictureBox</param>
        /// <param name="CaptureThreadProc1">相机对应的线程处理函数</param>
        /// <param name="cameraObj">相机对象</param>
        /// <returns></returns>
        protected bool InitSingleCamera(tSdkCameraDevInfo[] tCameraDevInfoList, int cameraDevID,
                                        ThreadStart CaptureThreadProc1,
                                        PictureBox PreviewBox, Button BtnPlay, Label StateLabel,
                                        string errMsg,
                                        ref tpvCameraObj cameraObj)
        {
            if (MvApi.CameraInit(ref tCameraDevInfoList[cameraDevID], -1, -1, ref cameraObj.m_hCamera) == CameraSdkStatus.CAMERA_STATUS_SUCCESS)
            {
                //获得相机特性描述
                MvApi.CameraGetCapability(cameraObj.m_hCamera, out cameraObj.tCameraCapability);

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

                //初始化显示模块,使用SDK内部封装好的显示接口
                MvApi.CameraDisplayInit(cameraObj.m_hCamera, PreviewBox.Handle);
                MvApi.CameraSetDisplaySize(cameraObj.m_hCamera, PreviewBox.Width, PreviewBox.Height);

                //让SDK来根据相机的型号动态创建该相机的配置窗口。
                MvApi.CameraCreateSettingPage(cameraObj.m_hCamera, this.Handle, tCameraDevInfoList[cameraDevID].acFriendlyName,
                                              /*SettingPageMsgCalBack*/ null, /*m_iSettingPageMsgCallbackCtx*/ (IntPtr)null, 0);

                MvApi.CameraPlay(cameraObj.m_hCamera);
                BtnPlay.Text = "Pause";

                //两种方式来获得预览图像,设置回调函数或者使用定时器或者独立线程的方式,
                //主动调用CameraGetImageBuffer接口来抓图。
                //本例中仅演示了两种的方式,注意,两种方式也可以同时使用,但是在回调函数中,
                //不要使用CameraGetImageBuffer,否则会造成死锁现象。

                cameraObj.m_bExitCaptureThread = false;
                cameraObj.m_tCaptureThread     = new Thread(new ThreadStart(CaptureThreadProc1));
                cameraObj.m_tCaptureThread.Start();
            }
            else
            {
                cameraObj.m_hCamera = 0;
                //StateLabel.Text = "Camera 1 init error";
                StateLabel.Text = errMsg;
                return(false);
            }
            return(true);
        }
예제 #5
0
        private void Form1_Load(object sender, EventArgs e)
        {
            //1打开窗口
            //2枚举设备
            //3初始化设备
            //4.获得位数,申请内存
            //5绑定回调事件  并在回调事件中加入halcon显示代码
            //6开始采集(默认连续,不设置采集模式的话)
            HOperatorSet.OpenWindow(0, 0, pictureBox1.Width, pictureBox1.Height, pictureBox1.Handle, "", "", out hv_WindowHandle);
            HDevWindowStack.Push(hv_WindowHandle);
            status = MvApi.CameraEnumerateDevice(out tCameraDevInfoList);
            if (tCameraDevInfoList != null)//此时iCameraCounts返回了实际连接的相机个数。如果大于1,则初始化第一个相机
            {
                status = MvApi.CameraInit(ref tCameraDevInfoList[0], -1, -1, ref m_hCamera);
            }
            MvApi.CameraGetCapability(m_hCamera, out tCameraCapability);

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

            m_CaptureCallback = new CAMERA_SNAP_PROC(ImageCaptureCallback);
            MvApi.CameraSetCallbackFunction(m_hCamera, m_CaptureCallback, m_iCaptureCallbackCtx, ref pCaptureCallOld);
            MvApi.CameraPlay(m_hCamera);
        }
예제 #6
0
        private bool InitCamera()
        {
            CameraSdkStatus status;

            tSdkCameraDevInfo[] tCameraDevInfoList;
            //IntPtr ptr;
            //int i;
#if USE_CALL_BACK
            CAMERA_SNAP_PROC pCaptureCallOld = null;
#endif
            if (m_hCamera > 0)
            {
                //已经初始化过,直接返回 true
                return(true);
            }

            status = MvApi.CameraEnumerateDevice(out tCameraDevInfoList);
            if (status == CameraSdkStatus.CAMERA_STATUS_SUCCESS)
            {
                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);

                        //初始化显示模块,使用SDK内部封装好的显示接口
                        MvApi.CameraDisplayInit(m_hCamera, PreviewBox.Handle);
                        MvApi.CameraSetDisplaySize(m_hCamera, PreviewBox.Width, PreviewBox.Height);

                        //设置抓拍通道的分辨率。
                        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);

                        //让SDK来根据相机的型号动态创建该相机的配置窗口。
                        MvApi.CameraCreateSettingPage(m_hCamera, this.Handle, tCameraDevInfoList[0].acFriendlyName, /*SettingPageMsgCalBack*/ null, /*m_iSettingPageMsgCallbackCtx*/ (IntPtr)null, 0);

                        //两种方式来获得预览图像,设置回调函数或者使用定时器或者独立线程的方式,
                        //主动调用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();
#endif
                        //MvApi.CameraReadSN 和 MvApi.CameraWriteSN 用于从相机中读写用户自定义的序列号或者其他数据,32个字节
                        //MvApi.CameraSaveUserData 和 MvApi.CameraLoadUserData用于从相机中读取自定义数据,512个字节
                        return(true);
                    }
                    else
                    {
                        m_hCamera       = 0;
                        StateLabel.Text = "Camera init error";
                        return(false);
                    }
                }
            }

            return(false);
        }
예제 #7
0
        //根据相机的当前参数,刷新界面上控件,做到同步。
        public void UpdateControls()
        {
            //根据相机描述信息,设定控件的参数调节范围.
            tSdkCameraCapbility tCameraDeviceInfo;

            MvApi.CameraGetCapability(m_hCamera, out tCameraDeviceInfo);

            //获取RGB三个同道的数字增益后更新界面
            trackBar_RedGain.SetRange(0, 399);
            trackBar_GreenGain.SetRange(0, 399);
            trackBar_BlueGain.SetRange(0, 399);
            UpdateRgbGainControls();

            //饱和度
            int saturation = 0;

            MvApi.CameraGetSaturation(m_hCamera, ref saturation);
            trackBar4.SetRange(0, 100);
            trackBar4.Value         = saturation;
            textBox_Saturation.Text = saturation.ToString();

            //彩色转黑白模式
            uint bEnable = 0;

            MvApi.CameraGetMonochrome(m_hCamera, ref bEnable);
            checkBox_MonoMode.Checked = (bEnable == 1?true:false);

            //反色模式
            MvApi.CameraGetInverse(m_hCamera, ref bEnable);
            checkBox_InverseImage.Checked = (bEnable == 1?true:false);

            //对比度
            int contrast = 0;

            MvApi.CameraGetContrast(m_hCamera, ref contrast);
            trackBar_Contrast.SetRange(0, 200);
            trackBar_Contrast.Value = contrast;
            textBox_Contrast.Text   = contrast.ToString();

            //分辨率
            comboBox_RresPreset.Items.Clear();

            // 填充分辨率列表
            tSdkImageResolution[] infos = new tSdkImageResolution[tCameraDeviceInfo.iImageSizeDesc];
            IntPtr ptr = tCameraDeviceInfo.pImageSizeDesc;

            for (int i = 0; i < infos.Length; i++)
            {
                infos[i] = (tSdkImageResolution)Marshal.PtrToStructure((IntPtr)((Int64)ptr + i * Marshal.SizeOf(new tSdkImageResolution())), typeof(tSdkImageResolution));
                string sDescription = System.Text.Encoding.Default.GetString(infos[i].acDescription);
                comboBox_RresPreset.Items.Insert(comboBox_RresPreset.Items.Count, sDescription);
            }
            //Marshal.FreeHGlobal (ptr);
            UpdateResolution();
            comboBox_RresPreset.SelectedIndex    = m_iResolutionIndex;
            radioButton_ResolutionROI.Checked    = !comboBox_RresPreset.Enabled;
            radioButton_ResolutionPreset.Checked = comboBox_RresPreset.Enabled;
            MvApi.CameraGetImageResolution(m_hCamera, out m_tRoiResolution);
            m_tRoiResolution.iIndex = 0xff;

            //伽马
            int gamma = 0;

            MvApi.CameraGetGamma(m_hCamera, ref gamma);
            trackBar_Gamma.SetRange(0, 1000);
            trackBar_Gamma.Value = gamma;
            double fGamma = (((double)gamma) / 100.0);//为了好理解,界面上1.0对应SDK的伽马值100,表示1倍的意思,伽马为1的时候,是默认值,相当于没有开启伽马校正。

            textBox_Gamma.Text = fGamma.ToString();

            //锐度
            int sharpness = 0;

            MvApi.CameraGetSharpness(m_hCamera, ref sharpness);
            trackBar_Sharpness.SetRange(0, 200);
            trackBar_Sharpness.Value = sharpness;
            textBox_Sharpness.Text   = sharpness.ToString();

            //2D降噪
            MvApi.CameraGetNoiseFilterState(m_hCamera, ref bEnable);
            if (bEnable == 1)
            {
                checkBox_2DDenoise.Checked = true;
            }
            else
            {
                checkBox_2DDenoise.Checked = false;
            }

            //3D降噪
            int bUseWeight;
            int counts;
            int iEnable;

            MvApi.CameraGetDenoise3DParams(m_hCamera, out iEnable, out counts, out bUseWeight, null);
            //填充列表
            comboBox_3DDenoise.Items.Clear();
            comboBox_3DDenoise.Items.Insert(comboBox_3DDenoise.Items.Count, "禁用");
            int j;

            for (j = 2; j <= 8; j++)
            {
                comboBox_3DDenoise.Items.Insert(comboBox_3DDenoise.Items.Count, j.ToString());
            }

            if (iEnable == 0)
            {
                comboBox_3DDenoise.SelectedIndex = 0;
            }
            else
            {
                comboBox_3DDenoise.SelectedIndex = counts - 1;
            }


            //水平镜像
            MvApi.CameraGetMirror(m_hCamera, 0, ref bEnable);
            if (bEnable == 1)
            {
                checkBox_HFlip.Checked = true;
            }
            else
            {
                checkBox_HFlip.Checked = false;
            }
            //垂直镜像
            MvApi.CameraGetMirror(m_hCamera, 1, ref bEnable);
            if (bEnable == 1)
            {
                checkBox_VFlip.Checked = true;
            }
            else
            {
                checkBox_VFlip.Checked = false;
            }

            //旋转图像
            int iRotate;

            MvApi.CameraGetRotate(m_hCamera, out iRotate);
            radioButton_Rotate90.Checked   = (iRotate == 1 ? true : false);
            radioButton_Rotate180.Checked  = (iRotate == 2 ? true : false);
            radioButton__Rotate270.Checked = (iRotate == 3 ? true : false);
            radioButton_Forbbiden.Checked  = (iRotate == 0 ? true : false);

            //图像采集模式
            int iGrabMode = 0;

            MvApi.CameraGetTriggerMode(m_hCamera, ref iGrabMode);
            comboBox_TriggerMode.Items.Clear();
            comboBox_TriggerMode.Items.Insert(comboBox_TriggerMode.Items.Count, "连续采集模式");
            comboBox_TriggerMode.Items.Insert(comboBox_TriggerMode.Items.Count, "软触发模式");
            comboBox_TriggerMode.Items.Insert(comboBox_TriggerMode.Items.Count, "硬触发模式");
            comboBox_TriggerMode.SelectedIndex = iGrabMode;
            button_SwTrigger.Enabled           = (iGrabMode == 1 ? true : false);

            //外触发信号模式
            int iSignalMode = 0;

            MvApi.CameraGetExtTrigSignalType(m_hCamera, ref iSignalMode);
            comboBox_ExtSignalMode.Items.Clear();
            comboBox_ExtSignalMode.Items.Insert(comboBox_ExtSignalMode.Items.Count, "上升沿触发");
            comboBox_ExtSignalMode.Items.Insert(comboBox_ExtSignalMode.Items.Count, "下降沿触发");
            comboBox_ExtSignalMode.SelectedIndex = iSignalMode;

            //外触发信号去抖时间
            uint uJitterTime = 0;

            MvApi.CameraGetExtTrigJitterTime(m_hCamera, ref uJitterTime);
            textBox_JetterTime.Text = uJitterTime.ToString();

            //闪光灯信号模式
            int iStrobMode = 0;

            MvApi.CameraGetStrobeMode(m_hCamera, ref iStrobMode);
            radioButton_StrobeModeAuto.Checked  = (iStrobMode == 0 ? true : false);
            radioButton_StrobeModeManul.Checked = (iStrobMode == 1 ? true : false);

            //闪光灯半自动模式下的有效极性
            int uPriority = 0;

            MvApi.CameraGetStrobePolarity(m_hCamera, ref uPriority);
            comboBox_StrobePriority.Items.Clear();
            comboBox_StrobePriority.Items.Insert(comboBox_StrobePriority.Items.Count, "高电平有效");
            comboBox_StrobePriority.Items.Insert(comboBox_StrobePriority.Items.Count, "低电平有效");
            comboBox_StrobePriority.SelectedIndex = uPriority;

            //闪光灯半自动模式下的延时时间
            uint uDelayTime = 0;

            MvApi.CameraGetStrobeDelayTime(m_hCamera, ref uDelayTime);
            textBox_StrobeDelayTime.Text = uDelayTime.ToString();

            //闪光灯半自动模式下的脉冲宽度
            uint uPluseWidth = 0;

            MvApi.CameraGetStrobePulseWidth(m_hCamera, ref uPluseWidth);
            textBox_StrobePulseWidth.Text = uPluseWidth.ToString();


            //相机的模拟增益值

            trackBar_AnalogGain.SetRange((int)tCameraDeviceInfo.sExposeDesc.uiAnalogGainMin, (int)tCameraDeviceInfo.sExposeDesc.uiAnalogGainMax);

            //相机的曝光时间
            double dCameraExpTimeMin = 0;                                      //最小时间,单位为
            double dCameraExpTimeMax = 0;                                      //最大时间

            MvApi.CameraGetExposureLineTime(m_hCamera, ref dCameraExpTimeMin); //相机的曝光时间,最小值为一行时间像素。
            dCameraExpTimeMax = (dCameraExpTimeMin * (double)tCameraDeviceInfo.sExposeDesc.uiExposeTimeMax);
            label_ExpMin.Text = "曝光时间最小值:" + dCameraExpTimeMin.ToString() + "微秒";
            label_ExpMax.Text = "曝光时间最大值:" + dCameraExpTimeMax.ToString() + "微秒";

            uint uState = 0;

            MvApi.CameraGetAeState(m_hCamera, ref uState);

            radioButton_AutoExp.Checked  = (uState == 1?true:false);
            radioButton_ManulExp.Checked = (uState == 0 ? true : false);

            UpdateExpsoureControls();
        }
        private bool InitCamera()
        {
            tSdkCameraDevInfo[] tCameraDevInfoList = new tSdkCameraDevInfo[12];
            IntPtr ptr;
            int    i;

#if USE_CALL_BACK
            CAMERA_SNAP_PROC pCaptureCallOld = null;
#endif
            ptr = Marshal.AllocHGlobal(Marshal.SizeOf(new tSdkCameraDevInfo()) * 12);
            int iCameraCounts = 12;//如果有多个相机时,表示最大只获取最多12个相机的信息列表。该变量必须初始化,并且大于1
            if (m_hCamera > 0)
            {
                //已经初始化过,直接返回 true

                return(true);
            }
            if (MvApi.CameraEnumerateDevice(ptr, ref iCameraCounts) == CameraSdkStatus.CAMERA_STATUS_SUCCESS)
            {
                for (i = 0; i < 12; i++)
                {
                    tCameraDevInfoList[i] = (tSdkCameraDevInfo)Marshal.PtrToStructure((IntPtr)((int)ptr + i * Marshal.SizeOf(new tSdkCameraDevInfo())), typeof(tSdkCameraDevInfo));
                }
                Marshal.FreeHGlobal(ptr);

                if (iCameraCounts >= 1)//此时iCameraCounts返回了实际连接的相机个数。如果大于1,则初始化第一个相机
                {
                    if (MvApi.CameraInit(ref tCameraDevInfoList[0], -1, -1, ref m_hCamera) == CameraSdkStatus.CAMERA_STATUS_SUCCESS)
                    {
                        //获得相机特性描述
                        ptr = Marshal.AllocHGlobal(Marshal.SizeOf(new tSdkCameraCapbility()));
                        MvApi.CameraGetCapability(m_hCamera, ptr);
                        tCameraCapability = (tSdkCameraCapbility)Marshal.PtrToStructure(ptr, typeof(tSdkCameraCapbility));
                        Marshal.FreeHGlobal(ptr);
                        Marshal.FreeHGlobal(m_ImageBuffer);
                        m_ImageBuffer         = Marshal.AllocHGlobal(tCameraCapability.sResolutionRange.iWidthMax * tCameraCapability.sResolutionRange.iHeightMax * 3 + 1024);
                        m_ImageBufferSnapshot = Marshal.AllocHGlobal(tCameraCapability.sResolutionRange.iWidthMax * tCameraCapability.sResolutionRange.iHeightMax * 3 + 1024);

                        //初始化显示模块,使用SDK内部封装好的显示接口
                        MvApi.CameraDisplayInit(m_hCamera, pic_Image.Handle);
                        MvApi.CameraSetDisplaySize(m_hCamera, pic_Image.Width, pic_Image.Height);

                        //设置抓拍通道的分辨率。
                        tSdkImageResolution tResolution;
                        tResolution.fZoomScale = 1.0F;
                        tResolution.iVOffset   = 0;
                        tResolution.iHOffset   = 0;
                        tResolution.uBinMode   = 0;
                        tResolution.uSkipMode  = 0;
                        tResolution.iWidth     = tCameraCapability.sResolutionRange.iWidthMax;
                        tResolution.iHeight    = tCameraCapability.sResolutionRange.iHeightMax;
                        //tResolution.iIndex = 0xff;表示自定义分辨率,如果tResolution.iWidth和tResolution.iHeight
                        //定义为0,则表示跟随预览通道的分辨率进行抓拍。抓拍通道的分辨率可以动态更改。
                        //本例中将抓拍分辨率固定为最大分辨率。
                        tResolution.iIndex        = 0xff;
                        tResolution.acDescription = new byte[32];//描述信息可以不设置
                        MvApi.CameraSetResolutionForSnap(m_hCamera, ref tResolution);

                        //让SDK来根据相机的型号动态创建该相机的配置窗口。
                        MvApi.CameraCreateSettingPage(m_hCamera, this.Handle, tCameraDevInfoList[0].acFriendlyName, /*SettingPageMsgCalBack*/ null, /*m_iSettingPageMsgCallbackCtx*/ (IntPtr)null, 0);

                        //两种方式来获得预览图像,设置回调函数或者使用定时器或者独立线程的方式,
                        //主动调用CameraGetImageBuffer接口来抓图。
                        //本例中仅演示了两种的方式,注意,两种方式也可以同时使用,但是在回调函数中,
                        //不要使用CameraGetImageBuffer,否则会造成死锁现象。
#if USE_CALL_BACK
                        MvApi.CameraSetCallbackFunction(m_hCamera, ImageCaptureCallback, m_iCaptureCallbackCtx, ref pCaptureCallOld);
#else //如果需要采用多线程,使用下面的方式
                        m_bExitCaptureThread = false;
                        m_tCaptureThread     = new Thread(new ThreadStart(CaptureThreadProc));
                        m_tCaptureThread.Start();
#endif
                        return(true);
                    }
                    else
                    {
                        m_hCamera = 0;
                        //StateLabel.Text = "相机初始化失败";
                        return(false);
                    }
                }
            }

            return(false);
        }
예제 #9
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("相机未连接!");
            //}
        }
예제 #10
0
        private bool InitCamera()
        {
            CameraSdkStatus status;

            tSdkCameraDevInfo[] tCameraDevInfoList;

            status = MvApi.CameraEnumerateDevice(out tCameraDevInfoList);
            int iCameraCounts = (tCameraDevInfoList != null ? tCameraDevInfoList.Length : 0);

            if (iCameraCounts >= 2)//此时iCameraCounts返回了实际连接的相机个数。如果大于2,则初始化前2个相机
            {
                if (MvApi.CameraInit(ref tCameraDevInfoList[0], -1, -1, ref m_hCamera1) == CameraSdkStatus.CAMERA_STATUS_SUCCESS)
                {
                    //获得相机特性描述
                    MvApi.CameraGetCapability(m_hCamera1, out tCameraCapability1);

                    m_ImageBuffer1 = Marshal.AllocHGlobal(tCameraCapability1.sResolutionRange.iWidthMax * tCameraCapability1.sResolutionRange.iHeightMax * 3 + 1024);

                    //初始化显示模块,使用SDK内部封装好的显示接口
                    MvApi.CameraDisplayInit(m_hCamera1, PreviewBox.Handle);
                    MvApi.CameraSetDisplaySize(m_hCamera1, PreviewBox.Width, PreviewBox.Height);

                    //让SDK来根据相机的型号动态创建该相机的配置窗口。
                    MvApi.CameraCreateSettingPage(m_hCamera1, this.Handle, tCameraDevInfoList[0].acFriendlyName, /*SettingPageMsgCalBack*/ null, /*m_iSettingPageMsgCallbackCtx*/ (IntPtr)null, 0);

                    MvApi.CameraPlay(m_hCamera1);
                    BtnPlay.Text = "Pause";

                    //两种方式来获得预览图像,设置回调函数或者使用定时器或者独立线程的方式,
                    //主动调用CameraGetImageBuffer接口来抓图。
                    //本例中仅演示了两种的方式,注意,两种方式也可以同时使用,但是在回调函数中,
                    //不要使用CameraGetImageBuffer,否则会造成死锁现象。

                    m_bExitCaptureThread1 = false;
                    m_tCaptureThread1     = new Thread(new ThreadStart(CaptureThreadProc1));
                    m_tCaptureThread1.Start();
                }
                else
                {
                    m_hCamera1      = 0;
                    StateLabel.Text = "Camera 1 init error";
                    return(false);
                }

                if (MvApi.CameraInit(ref tCameraDevInfoList[1], -1, -1, ref m_hCamera2) == CameraSdkStatus.CAMERA_STATUS_SUCCESS)
                {
                    //获得相机特性描述
                    MvApi.CameraGetCapability(m_hCamera2, out tCameraCapability2);

                    m_ImageBuffer2 = Marshal.AllocHGlobal(tCameraCapability2.sResolutionRange.iWidthMax * tCameraCapability2.sResolutionRange.iHeightMax * 3 + 1024);

                    //初始化显示模块,使用SDK内部封装好的显示接口
                    MvApi.CameraDisplayInit(m_hCamera2, PreviewBox2.Handle);
                    MvApi.CameraSetDisplaySize(m_hCamera2, PreviewBox2.Width, PreviewBox2.Height);

                    //让SDK来根据相机的型号动态创建该相机的配置窗口。
                    MvApi.CameraCreateSettingPage(m_hCamera2, this.Handle, tCameraDevInfoList[1].acFriendlyName, /*SettingPageMsgCalBack*/ null, /*m_iSettingPageMsgCallbackCtx*/ (IntPtr)null, 0);

                    MvApi.CameraPlay(m_hCamera2);
                    BtnPlay2.Text = "Pause";

                    m_bExitCaptureThread2 = false;
                    m_tCaptureThread2     = new Thread(new ThreadStart(CaptureThreadProc2));
                    m_tCaptureThread2.Start();
                }
                else
                {
                    m_hCamera2      = 0;
                    StateLabel.Text = "Camera 2 init error";
                    return(false);
                }

                return(true);
            }
            return(false);
        }