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;
            }
        }
Exemple #3
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 DoBarCode()
        {
            try
            {
                if (MvBarCode.MvBarCodeGlobalVar.LocalImageMode)
                {
                    _ImageWidth  = 4000;
                    _ImageHeight = 3000;
                }

                // 初始化算法

                MvBarCodeGlobalVar.Log.Info("DoBarCode:开始初始化算法");
                MvBarCodeCore magic = new MvBarCodeCore();
                lock (locker)
                {
                    if (!magic.InitAlgorithm(this.Width, this.Height))
                    {
                        MvBarCodeGlobalVar.Log.Error("InitAlgorithm failed");
                        Debug.WriteLine("InitAlgorithm failed");
                        return;
                    }

                    MvBarCodeGlobalVar.Log.Info("DoBarCode:结束初始化一维码算法");

                    if (MvBarCode.MvBarCodeGlobalVar.OpenGetSheet)
                    {
                        COcrMatting = new MvOcrMat();
                        COcrMatting.InitAlgorithm(this.Width, this.Height);
                    }

                    CSharpnessMeasure = new MvSharpnessMeasure();
                    CSharpnessMeasure.InitAlgorithm(this.Width, this.Height);
                    CSharpnessMeasure.SetConfig();

                    MvBarCodeGlobalVar.Log.Info("DoBarCode:结束初始化清晰度算法");
                }

                MvBarCodeGlobalVar.Log.Info("DoBarCode:结束初始化算法");


                // 初始化算法配参
                MvBarCodeCore.MvSBcConfigParam param = new MvBarCodeCore.MvSBcConfigParam();
                param.Initialization();
                magic.GetConfig(ref param);
                param.CodeType[0] = 2;
                param.CodeType[1] = 1;

                param.codeNum            = MvBarCodeGlobalVar.maxNum;
                param.max1DCodeNum       = MvBarCodeGlobalVar.maxNum;
                param.segmentationMethod = MvBarCodeGlobalVar.segmentationMethod;
                param.ElemMaxWidth       = MvBarCodeGlobalVar.ElemMaxWidth;
                param.ElemMinWidth       = MvBarCodeGlobalVar.ElemMinWidth;

                param.MinHeight = MvBarCodeGlobalVar.MinHeight;
                param.MinWidth  = MvBarCodeGlobalVar.MinWidth;
                param.MaxHeight = MvBarCodeGlobalVar.MaxHeight;
                param.MaxWidth  = MvBarCodeGlobalVar.MaxWidth;

                magic.SetConfig(ref param);

                // 记录最新条形码信息
                List <string> newCodes = new List <string>();


                // 算法运行标识
                WaitHandle[] handles = new WaitHandle[] { _DetectBarCodeEvent, _ExitHandle };
                //截取面单标识符
                cutSheetHanless = new WaitHandle[] { _CutSheetHanle, _ExitHandle };
                //结束截取面单标识符
                doneCutSheetHandless = new WaitHandle[] { _DoneCutSheetHandle, _ExitHandle };

                MvBarCodeGlobalVar.Log.Info("DoBarCode:结束算法配置参数");
                // 算法运算
                while (_bAppStatus)
                {
                    // 等待采样结束
                    if (WaitHandle.WaitAny(handles) != 0)
                    {
                        break;
                    }

                    // 若当前无检测数据,线程休息1ms
                    if (Interlocked.Read(ref _bAlgorithmRunning) == 0)
                    {
                        continue;
                    }

                    // 检测指定图片
                    MvCodeInfo[] ci = null;
                    using (MvsImageParam img = new MvsImageParam())
                    {
                        // 赋值图像样本参数
                        img.Width    = _FrameData.Width;
                        img.Height   = _FrameData.Height;
                        img.DataType = Convert.ToInt32(MvsImgDataType.MVS_IMGDTP_U8);
                        img.Type     = Convert.ToInt32(MvsImgType.MVS_IMGTP_UITL_Y);

                        if (!MvBarCode.MvBarCodeGlobalVar.LocalImageMode)
                        {
                            img.ImageData = _FrameData.Image;
                        }
                        else
                        {
                            img.ImagePointer = _FrameData.Raw;
                        }

                        // 检测算法
                        magic.Process(img, false, ref ci);
                    }

                    // 返回结果为空,继续算法运算
                    if (ci == null)
                    {
                        if (MvBarCodeGlobalVar.OpenLive)
                        {
                            BarCodeHandle(this, new BarCodeEventArgs(new BarCodeDescribe(_FrameData.ToBitmap(false), null, false, _DeviceIndex)));
                        }
                        // 重置检测状态,重新采样
                        Interlocked.CompareExchange(ref _bAlgorithmRunning, 0, 1);
                        continue;
                    }

                    // 重置所有检测标识
                    mci.Clear();
                    newCodes.Clear();

                    // 捕获条形码数据
                    foreach (var item in ci)
                    {
                        if (_barcodeRuleFilter.CheckAndCatchBarcode(item, ref newCodes, MvBarCodeGlobalVar.BarcodeRuleType.common))
                        {
                            mci.Add(item);
                        }
                    }

                    if (newCodes.Count > 0)
                    {
                        try
                        {
                            //开启截取面单才需要等待
                            if (COcrMatting != null)
                            {
                                //_DoneCutSheetHandle.WaitOne();
                                if (WaitHandle.WaitAny(doneCutSheetHandless) != 0)
                                {
                                    break;
                                }
                            }

                            // 检测是否有多个条码数据
                            if (newCodes.Count > 1)
                            {
                                _CutSheetHanle.Set();

                                if (BarCodeHandle != null)
                                {
                                    BarCodeHandle(this, new BarCodeEventArgs(new BarCodeDescribe(_FrameData.ToBitmap(false),
                                                                                                 DetectExceptionType.MulBarCodeExceptionType, newCodes.ToArray(), mci, _DeviceIndex)));
                                }
                            }
                            // 判断重复条码(暂时去掉相机缓存)
                            else if (0 != string.Compare(_LastBarCode, newCodes[0]))
                            {
                                //相机是否需要自己的缓存
                                if (MvBarCode.MvBarCodeGlobalVar.CameraBarcodeCache)
                                {
                                    _LastBarCode = newCodes[0];
                                }

                                Interlocked.Exchange(ref _bNewBarcode, 1);

                                //通知截取面单可以开始
                                _CutSheetHanle.Set();

                                if (BarCodeHandle != null)
                                {
                                    BarCodeHandle(this, new BarCodeEventArgs(new BarCodeDescribe(_FrameData.ToBitmap(false), mci[0], true, _DeviceIndex)));
                                }
                            }
                            //有一个条码,但是是重复条码
                            else
                            {
                                _CutSheetHanle.Set();

                                if (MvBarCodeGlobalVar.OpenLive)
                                {
                                    if (BarCodeHandle != null)
                                    {
                                        BarCodeHandle(this, new BarCodeEventArgs(new BarCodeDescribe(_FrameData.ToBitmap(false), mci[0], false, _DeviceIndex)));
                                    }
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            MvBarCodeGlobalVar.Log.Error(e);
                        }
                    }
                    //实时传图片
                    else
                    {
                        if (MvBarCodeGlobalVar.OpenLive)
                        {
                            BarCodeHandle(this, new BarCodeEventArgs(new BarCodeDescribe(_FrameData.ToBitmap(false), null, false, _DeviceIndex)));
                        }
                    }

                    // 重新采样
                    Interlocked.CompareExchange(ref _bAlgorithmRunning, 0, 1);
                    Thread.Sleep(1);
                }
            }
            catch (Exception e1)
            {
                MvBarCodeGlobalVar.Log.Error(e1);
            }
        }