Exemple #1
0
        /// <summary>
        /// 图像采集函数 ,线程
        /// </summary>
        private void ShowThread()
        {
            while (true)
            {
                if (frameList.Count == 0)
                {
                    Thread.Sleep(10);
                    continue;
                }

                /* 图像队列取最新帧 */
                mutex.WaitOne();
                IGrabbedRawData frame = frameList.ElementAt(frameList.Count - 1);
                frameList.Clear();
                mutex.ReleaseMutex();

                /* 主动调用回收垃圾 */
                GC.Collect();

                /* 控制显示最高帧率为25FPS */
                if (false == IsTimeToDisplay())
                {
                    continue;
                }

                try
                {
                    /* 图像转码成bitmap图像 */
                    var     bitmap = frame.ToBitmap(false);
                    HObject image  = BitmapToHImage(bitmap);
                    OnImageAcqed(image);//触发事件
                    try
                    {
                        image.Dispose();
                        image = null;
                    }
                    catch { }
                    try
                    {
                        bitmap.Dispose();
                        bitmap = null;
                    }
                    catch { }
                }
                catch (Exception exception)
                {
                    Catcher.Show(exception);

                    /* MessageBox.Show("Dahua:系统异常");
                     * Environment.Exit(0);*/
                }
            }
        }
Exemple #2
0
        private void ShowThread()  // 开启线程采集
        {
            try
            {
                while (m_bShowLoop)
                {
                    if (m_frameList.Count == 0)
                    {
                        Thread.Sleep(10);
                        continue;
                    }

                    /* 图像队列取最新帧 */
                    m_mutex.WaitOne();
                    IGrabbedRawData frame = m_frameList.ElementAt(m_frameList.Count - 1);
                    m_frameList.Clear();
                    m_mutex.ReleaseMutex();

                    /* 主动调用回收垃圾 */
                    GC.Collect();

                    /* 控制显示最高帧率为25FPS */
                    if (false == isTimeToDisplay())
                    {
                        continue;
                    }

                    try
                    {
                        /* 图像转码成bitmap图像 */
                        var bitmap = frame.ToBitmap(false);
                        //   Bitmap bit = bitmap.Clone();
                        m_bShowByGDI = true;
                        if (m_bShowByGDI)
                        {
                            HImage himage = BitmapToHImage(bitmap);
                            /// 添加事件通过绑定事件的方式进行图像传播
                            DoSomething(bitmap, himage);
                        }
                    }
                    catch (Exception exception)
                    {
                        Catcher.Show(exception);
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        // 码流数据回调
        private void OnImageGrabbed(Object sender, GrabbedEventArgs e)
        {
            // 转换帧数据为Bitmap
            var bitmap = e.GrabResult.ToBitmap(false);

            /*
             * //转Bitmap图像为Emgucv图像,并进行二值化
             * Image<Rgb, byte> original_img = new Image<Rgb, byte>( bitmap );
             * var gray_img = original_img.Convert<Gray, Byte>();
             * var threshold_img = original_img.Convert<Gray, Byte>();
             *
             * CvInvoke.cvThreshold( gray_img, threshold_img, 20, 255, Emgu.CV.CvEnum.THRESH.CV_THRESH_BINARY);
             *
             * //转Emgucv图像为Bitmap图像
             * var show_img = threshold_img.ToBitmap();
             */



            // 显示图片数据
            if (InvokeRequired)
            {
                BeginInvoke(new MethodInvoker(() =>
                {
                    try
                    {
                        if (_g == null)
                        {
                            _g = pbImage.CreateGraphics();
                        }

                        /*
                         * _g.DrawImage( show_img, new Rectangle(0, 0, pbImage.Width, pbImage.Height),
                         *  new Rectangle(0, 0, show_img.Width, show_img.Height), GraphicsUnit.Pixel);
                         * show_img.Dispose();
                         */

                        _g.DrawImage(bitmap, new Rectangle(0, 0, pbImage.Width, pbImage.Height),
                                     new Rectangle(0, 0, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);
                        bitmap.Dispose();

                        //pbImage.Image = bitmap;
                    }
                    catch (Exception exception)
                    {
                        Catcher.Show(exception);
                    }
                }));
            }
        }
        // 执行软触发
        private void btnSoftwareTrigger_Click(object sender, EventArgs e)
        {
            if (m_dev == null)
            {
                throw new InvalidOperationException("Device is invalid");
            }

            try
            {
                m_dev.ExecuteSoftwareTrigger();
            }
            catch (Exception exception)
            {
                Catcher.Show(exception);
            }
        }
Exemple #5
0
        /* 执行软触发 */
        private void btnSoftwareTrigger_Click(object sender, EventArgs e)
        {
            if (m_dev == null)
            {
                throw new InvalidOperationException("Device is invalid");
            }

            try
            {
                if (bVideoMode)
                {
                    m_dev.TriggerSet.Close();
                }
                else
                {
                    m_dev.ExecuteSoftwareTrigger();
                }

                String strOldFilePath = strSavePath + strFileName;
                if (File.Exists(strOldFilePath))
                {
                    File.Delete(strOldFilePath); // delete the last frame image
                }

                System.DateTime currentTime = new System.DateTime();
                currentTime = System.DateTime.Now;
                strFileName = currentTime.ToString("yyyy-MM-dd HH:mm:ss").Replace(" ", "_").Replace(":", "-") + ".png";
                // strFileName = "test.jpg";
                String strFullPath = strSavePath + strFileName;

                // openFileDialog1.Filter = "BMP文件|*.bmp|JPEG文件|*.jpg";

                if (strFullPath != "") // openFileDialog1.ShowDialog() == DialogResult.OK
                {
                    // imgOri = Image.FromFile(strFullPath);
                    // pbImage.Image = imgOri;
                    hWindowCtrl.Visible = false;
                    pbImage.Visible     = true;
                    lblResult.Text      = "UN";
                    lblResult.BackColor = Color.White;
                }
            }
            catch (Exception exception)
            {
                Catcher.Show(exception);
            }
        }
        // 关闭相机
        private void btnClose_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_dev == null)
                {
                    throw new InvalidOperationException("Device is invalid");
                }

                m_dev.ShutdownGrab();
                m_dev.Close();
            }
            catch (Exception exception)
            {
                Catcher.Show(exception);
            }
        }
Exemple #7
0
        /* 停止码流 */
        private void btnClose_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_dev == null)
                {
                    throw new InvalidOperationException("Device is invalid");
                }

                m_dev.StreamGrabber.ImageGrabbed -= OnImageGrabbed;         /* 反注册回调 */
                m_dev.ShutdownGrab();                                       /* 停止码流 */
                m_dev.Close();                                              /* 关闭相机 */
            }
            catch (Exception exception)
            {
                Catcher.Show(exception);
            }
        }
Exemple #8
0
        public override void Close()
        {
            try
            {
                if (objDev == null)
                {
                    throw new InvalidOperationException("Device is invalid");
                }

                objDev.StreamGrabber.ImageGrabbed -= OnImageGrabbed;         // 反注册回调 | unregister grab event callback
                objDev.ShutdownGrab();                                       // 停止码流 | stop grabbing
                objDev.Close();                                              // 关闭相机 | close camera
            }
            catch (Exception exception)
            {
                Catcher.Show(exception);
            }
        }
        public void Close()
        {
            try
            {
                if (_mDev == null)
                {
                    throw new InvalidOperationException("Device is invalid");
                }

                _mDev.StreamGrabber.ImageGrabbed -= OnImageGrabbedN;        /* 反注册回调 */
                _mDev.ShutdownGrab();                                       /* 停止码流 */
                _mDev.Close();                                              /* 关闭相机 */
                IsOpen = false;
            }
            catch (Exception exception)
            {
                Catcher.Show(exception);
            }
        }
Exemple #10
0
        /// <summary>
        /// 软触发相机
        /// </summary>
        /// <returns></returns>
        public bool ExecuteSoftwareTrigger()
        {
            bool ok = false;

            if (m_dev == null)
            {
                throw new InvalidOperationException("Device is invalid");
                return(ok);
            }

            try
            {
                m_dev.ExecuteSoftwareTrigger();
            }
            catch (Exception exception)
            {
                Catcher.Show(exception);
            }
            ok = true;
            return(ok);
        }
Exemple #11
0
        /// <summary>
        /// 关闭相机
        /// </summary>
        /// <param name="IDa"></param>
        /// <returns></returns>
        public bool close_camer(IDaHua IDa)
        {
            bool ok = false;

            try
            {
                if (IDa.Dev == null)
                {
                    throw new InvalidOperationException("Device is invalid");
                }

                IDa.Dev.ShutdownGrab();

                IDa.Dev.Close();
                ok = true;
            }
            catch (Exception exception)
            {
                Catcher.Show(exception);
            }
            ok = true;
            return(ok);
        }
Exemple #12
0
        /* 执行软触发 */
        private void btnSoftwareTrigger_Click(object sender, EventArgs e)
        {
            try
            {
                if (ParamSetting.SnapMode == ImageMode.Online)
                {
                    if (m_dev == null)
                    {
                        throw new InvalidOperationException("Device is invalid");
                    }

                    if (bVideoMode)
                    {
                        m_dev.TriggerSet.Close();
                    }
                    else
                    {
                        m_dev.ExecuteSoftwareTrigger();
                    }

                    String strOldFilePath = ParamSetting.ImageSavePath + "\\" + strFileName;
                    if (File.Exists(strOldFilePath))
                    {
                        File.Delete(strOldFilePath); // delete the last frame image
                    }

                    System.DateTime currentTime = new System.DateTime();
                    currentTime = System.DateTime.Now;
                    strFileName = currentTime.ToString("yyyy-MM-dd HH:mm:ss").Replace(" ", "_").Replace(":", "-") + ".png";
                    // strFileName = "test.jpg";
                    String strFullPath = ParamSetting.ImageSavePath + "\\" + strFileName;

                    // openFileDialog1.Filter = "BMP文件|*.bmp|JPEG文件|*.jpg";

                    if (strFullPath != "") // openFileDialog1.ShowDialog() == DialogResult.OK
                    {
                        // imgOri = Image.FromFile(strFullPath);
                        // pbImage.Image = imgOri;
                        hWindowCtrl.Visible = false;
                        pbImage.Visible     = true;
                        lblResult.Text      = "UN";
                        lblResult.BackColor = Color.White;
                    }
                }
                else if (ParamSetting.SnapMode == ImageMode.Offline)
                {
                    using (OpenFileDialog opdialog = new OpenFileDialog())
                    {
                        opdialog.Multiselect = false;
                        opdialog.Title       = "选择检测图片";
                        opdialog.Filter      = "图片|*.png;*.PNG";
                        if (opdialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            imgOri        = Image.FromFile(opdialog.FileName);
                            pbImage.Image = imgOri;
                        }
                        strFileName = (System.DateTime.Now).ToString("yyyy-MM-dd HH:mm:ss").Replace(" ", "_").Replace(":", "-") + ".png";
                    }
                }
            }
            catch (Exception exception)
            {
                Catcher.Show(exception);
            }
        }
Exemple #13
0
        /* 打开相机按钮事件 */
        private void btnOpen_Click(object sender, EventArgs e)
        {
            try
            {
                /* 设备搜索 */
                List <IDeviceInfo> li = Enumerator.EnumerateDevices();
                if (li.Count > 0)
                {
                    /* 获取搜索到的第一个设备 */
                    m_dev = Enumerator.GetDeviceByIndex(0);

                    /* 注册链接时间 */
                    m_dev.CameraOpened   += OnCameraOpen;
                    m_dev.ConnectionLost += OnConnectLoss;
                    m_dev.CameraClosed   += OnCameraClose;

                    /* 打开设备 */
                    if (!m_dev.Open())
                    {
                        MessageBox.Show(@"连接相机失败");
                        return;
                    }

                    /* 打开Software Trigger */
                    if (bVideoMode)
                    {
                        m_dev.TriggerSet.Close();
                    }
                    else
                    {
                        m_dev.TriggerSet.Open(TriggerSourceEnum.Software);
                    }

                    /* 设置图像格式 */
                    using (IEnumParameter p = m_dev.ParameterCollection[ParametrizeNameSet.ImagePixelFormat])
                    {
                        p.SetValue("Mono8");
                    }

                    /* 设置曝光 */
                    using (IFloatParameter p = m_dev.ParameterCollection[ParametrizeNameSet.ExposureTime])
                    {
                        p.SetValue(nExpTime);
                    }

                    /* 设置增益 */
                    using (IFloatParameter p = m_dev.ParameterCollection[ParametrizeNameSet.GainRaw])
                    {
                        p.SetValue(fCamGain);
                    }

                    /* 设置缓存个数为8(默认值为16) */
                    m_dev.StreamGrabber.SetBufferCount(8);

                    /* 注册码流回调事件 */
                    m_dev.StreamGrabber.ImageGrabbed += OnImageGrabbed;

                    /* 开启码流 */
                    if (!m_dev.GrabUsingGrabLoopThread())
                    {
                        MessageBox.Show(@"开启码流失败");
                        return;
                    }
                }
            }
            catch (Exception exception)
            {
                Catcher.Show(exception);
            }
        }
Exemple #14
0
        /* 转码显示线程 */
        private void ShowThread()
        {
            while (m_bShowLoop)
            {
                if (m_frameList.Count == 0)
                {
                    Thread.Sleep(10);
                    continue;
                }

                /* 图像队列取最新帧 */
                m_mutex.WaitOne();
                IGrabbedRawData frame = m_frameList.ElementAt(m_frameList.Count - 1);
                m_frameList.Clear();
                m_mutex.ReleaseMutex();

                /* 主动调用回收垃圾 */
                GC.Collect();

                /* 控制显示最高帧率为25FPS */
                if (false == isTimeToDisplay())
                {
                    continue;
                }

                try
                {
                    /* 图像转码成bitmap图像 */
                    bitmap       = frame.ToBitmap(false);
                    m_bShowByGDI = true;
                    if (m_bShowByGDI)
                    {
                        /* 使用GDI绘图 */
                        if (_g == null)
                        {
                            _g = pbImage.CreateGraphics();
                        }
                        _g.DrawImage(bitmap, new Rectangle(0, 0, pbImage.Width, pbImage.Height),
                                     new Rectangle(0, 0, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);
                        if (bitmap != null)
                        {
                            imgOri = bitmap;
                        }
                        // bitmap.Save(strSavePath + strFileName);
                        // bitmap.Dispose();
                    }
                    else
                    {
                        /* 使用控件绘图,会造成主界面卡顿 */
                        if (InvokeRequired)
                        {
                            BeginInvoke(new MethodInvoker(() =>
                            {
                                try
                                {
                                    pbImage.Image = bitmap;
                                }
                                // save captured image
                                // bitmap.Save("C:\\Users\\rocki\\Desktop\\captured\\shot.jpg");

                                catch (Exception exception)
                                {
                                    Catcher.Show(exception);
                                }
                            }));
                        }
                    }
                }
                catch (Exception exception)
                {
                    Catcher.Show(exception);
                }
            }
        }
        // 打开相机
        private void btnOpen_Click(object sender, EventArgs e)
        {
            try
            {
                // 设备搜索
                List <IDeviceInfo> li = Enumerator.EnumerateDevices();
                if (li.Count > 0)
                {
                    // 获取搜索到的第一个设备
                    m_dev = Enumerator.GetDeviceByIndex(0);

                    // 注册链接时间
                    m_dev.CameraOpened   += OnCameraOpen;
                    m_dev.ConnectionLost += OnConnectLoss;
                    m_dev.CameraClosed   += OnCameraClose;

                    // 打开设备
                    if (!m_dev.Open())
                    {
                        MessageBox.Show(@"连接相机失败");
                        return;
                    }

                    // 打开Software Trigger
                    m_dev.TriggerSet.Open(TriggerSourceEnum.Software);

                    // 设置图像格式
                    using (IEnumParameter p = m_dev.ParameterCollection[ParametrizeNameSet.ImagePixelFormat])
                    {
                        p.SetValue("BayerRG12Packed");
                    }
                    // 设置图片亮度
                    using (IIntegraParameter p = m_dev.ParameterCollection[ParametrizeNameSet.Brightness])
                    {
                        p.SetValue(100);
                    }
                    //设置曝光时间
                    using (IFloatParameter p = m_dev.ParameterCollection[ParametrizeNameSet.ExposureTime])
                    {
                        p.SetValue(50000);
                    }

                    /*
                     * // 设置图片高度
                     * using (IIntegraParameter p = m_dev.ParameterCollection[ParametrizeNameSet.ImageHeight])
                     * {
                     *  p.SetValue(600);
                     * }
                     * // 设置图片宽度
                     * using (IIntegraParameter p = m_dev.ParameterCollection[ParametrizeNameSet.ImageWidth])
                     * {
                     *  p.SetValue(600);
                     * }
                     */

                    // 注册码流回调事件
                    m_dev.StreamGrabber.ImageGrabbed += OnImageGrabbed;

                    // 开启码流
                    if (!m_dev.GrabUsingGrabLoopThread())
                    {
                        MessageBox.Show(@"开启码流失败");
                        return;
                    }

                    m_dev.ExecuteSoftwareTrigger();
                }
            }
            catch (Exception exception)
            {
                Catcher.Show(exception);
            }
        }
Exemple #16
0
        private void LoadFormInfo()
        {
            try
            {
                halconHelper = new HalconHelper(hWindowControl1.HalconWindow);
                List <IDeviceInfo> li = Enumerator.EnumerateDevices();
                //if (Convert.ToInt32(this.Tag) != -1)
                if (li.Count > 0)
                {
                    m_dev = Enumerator.GetDeviceByIndex(0);
                    //m_dev = Enumerator.GetDeviceByIndex(Convert.ToInt32(this.Tag));
                    // 注册链接时间

                    m_dev.CameraOpened   += OnCameraOpen;
                    m_dev.ConnectionLost += OnConnectLoss;
                    m_dev.CameraClosed   += OnCameraClose;
                    // 打开设备
                    if (!m_dev.Open())
                    {
                        MessageBox.Show(@"连接相机失败");
                        return;
                    }

                    // 关闭Trigger
                    //m_dev.TriggerSet.Close();
                    // 打开Software Trigger
                    m_dev.TriggerSet.Open(TriggerSourceEnum.Software);
                    // 设置图像格式
                    using (IEnumParameter p = m_dev.ParameterCollection[ParametrizeNameSet.ImagePixelFormat])
                    {
                        p.SetValue("Mono8");
                    }

                    // 注册码流回调事件
                    m_dev.StreamGrabber.ImageGrabbed += OnImageGrabbed;

                    // 开启码流
                    if (!m_dev.GrabUsingGrabLoopThread())
                    {
                        MessageBox.Show(@"开启码流失败");
                        return;
                    }
                    else
                    {
                        m_dev.ExecuteSoftwareTrigger();
                    }
                }
                else
                {
                }
                //注册一维码识别完成事件
                halconHelper.Complete += OnComplete;
                halconHelper.Error    += OnError;
                //注册串口接收事件
                serialComm.DateReceived += new Comm.EventHandle(OnDataReceived);
                weightComm.DateReceived += new Comm.EventHandle(OnWeightDataReceived);
            }
            catch (Exception ex)
            {
                Catcher.Show(ex);
            }
        }