Пример #1
0
        public override Object Read()
        {
            VisionCode2dParam tempvision2dCodeParam = null;
            string            strPath = VisionMgr.GetInstance().CurrentVisionProcessDir + "\\" + m_strStepName + "\\" + m_strStepName + ".xml";

            //tempvision2dCodeParam = (VisionCode2dParam)AccessXmlSerializer.XmlToObject(strPath, vision2dCodeParam.GetType());
            strPath = VisionMgr.GetInstance().CurrentVisionProcessDir + "\\" + m_strStepName + "\\" + m_strStepName + ".json";
            Vision2dCode vision2DCode = (Vision2dCode)AccessJosnSerializer.JsonToObject(strPath, this.GetType());

            if (vision2DCode == null || vision2DCode.vision2dCodeParam == null)
            {
                _logger.Warn(m_strStepName + ": 视觉处理项目加载失败,请检查");
                MessageBox.Show(m_strStepName + ": 视觉处理项目加载失败,请检查", "Err", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(null);
            }
            vision2dCodeParam = tempvision2dCodeParam = vision2DCode.vision2dCodeParam;

            string Mode2dcodePath = VisionMgr.GetInstance().CurrentVisionProcessDir + "\\" + m_strStepName + "\\" + m_strStepName + "_data_code_model.dcm";;

            if (vision2dCodeParam != null && Mode2dcodePath != null && File.Exists(Mode2dcodePath))
            {
                if (Mode2dCode != null)
                {
                    HOperatorSet.ClearDataCode2dModel(Mode2dCode);
                }
                Mode2dCode = null;
                HOperatorSet.ReadDataCode2dModel(Mode2dcodePath, out Mode2dCode);
                if (Mode2dCode == null || Mode2dCode.Length <= 0)
                {
                    _logger.Warn(m_strStepName + ": 2维码模板读取失败,请检查");
                    MessageBox.Show(m_strStepName + ": 2维码模板读取失败,请检查", "Err", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                // MessageBox.Show(m_strStepName + ": 2维码模板读取失败,请检查", "Err", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            if (vision2dCodeParam != null && File.Exists(vision2dCodeParam.Mode2dcodeSearchPath))
            {
                if (Mode2dCodeSearch != null && Mode2dCodeSearch.IsInitialized())
                {
                    Mode2dCodeSearch.Dispose();
                }
                HOperatorSet.ReadRegion(out Mode2dCodeSearch, vision2dCodeParam.Mode2dcodeSearchPath);
                if (Mode2dCodeSearch == null || Mode2dCodeSearch.IsInitialized())
                {
                    _logger.Warn(m_strStepName + ": 2维码搜索区域读取失败,请检查");
                }
                //  MessageBox.Show(m_strStepName + ": 2维码搜索区域读取失败,请检查", "Err", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                _logger.Warn(m_strStepName + ": 2维码搜索区域读取失败,请检查");
                //  MessageBox.Show(m_strStepName + ": 2维码搜索区域读取失败,请检查", "Err", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            GC.Collect();

            return(vision2dCodeParam);
        }
Пример #2
0
        public override bool GenObj(HObject image, VisionControl visionControl)
        {
            try
            {
                if (Mode2dCode != null)
                {
                    HOperatorSet.ClearDataCode2dModel(Mode2dCode);
                }
                HTuple hv_DataCodeHandleLow = null;
                HOperatorSet.CreateDataCode2dModel(vision2dCodeParam.Code2dSystem, "default_parameters", "standard_recognition", out hv_DataCodeHandleLow);

                Mode2dCode = hv_DataCodeHandleLow;

                string Mode2dcodePath = VisionMgr.GetInstance().CurrentVisionProcessDir + "\\" + m_strStepName + "\\" + m_strStepName + "_data_code_model.dcm";
                HOperatorSet.WriteDataCode2dModel(Mode2dCode, Mode2dcodePath);

                string imgpath = VisionMgr.GetInstance().CurrentVisionProcessDir + "\\" + m_strStepName + "\\" + m_strStepName + ".bmp";
                if (image != null && image.IsInitialized())
                {
                    HOperatorSet.WriteImage(image, "bmp", 0, imgpath);
                }
            }
            catch (HalconException e)
            {
                MessageBox.Show(m_strStepName + "创建2CodeModle失败:" + e.Message, "Err", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }

            return(true);
        }
Пример #3
0
        // Procedures
        public static void ReadData(HObject ho_Image, out HObject ho_SymbolXLDs, out HTuple hv_flag,
                                    out HTuple hv_DecodedDataStrings, out HTuple hv_Row, out HTuple hv_Column)
        {
            // Local iconic variables

            HObject ho_GrayImage1 = null;

            // Local control variables

            HTuple hv_Grayval = null, hv_channels = null;
            HTuple hv_DataCodeHandle = null, hv_ResultHandles = null;
            HTuple hv_Area = new HTuple(), hv_PointOrder = new HTuple();

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_SymbolXLDs);
            HOperatorSet.GenEmptyObj(out ho_GrayImage1);
            hv_Row    = new HTuple();
            hv_Column = new HTuple();
            hv_flag   = 0;
            HOperatorSet.GetGrayval(ho_Image, 0, 0, out hv_Grayval);
            hv_channels = new HTuple(hv_Grayval.TupleLength());
            if ((int)(new HTuple(hv_channels.TupleNotEqual(1))) != 0)
            {
                ho_GrayImage1.Dispose();
                HOperatorSet.Rgb1ToGray(ho_Image, out ho_GrayImage1);
            }
            else
            {
                ho_GrayImage1.Dispose();
                HOperatorSet.CopyImage(ho_Image, out ho_GrayImage1);
            }
            HOperatorSet.CreateDataCode2dModel("Data Matrix ECC 200", "default_parameters",
                                               "enhanced_recognition", out hv_DataCodeHandle);
            ho_SymbolXLDs.Dispose();
            HOperatorSet.FindDataCode2d(ho_GrayImage1, out ho_SymbolXLDs, hv_DataCodeHandle,
                                        new HTuple(), new HTuple(), out hv_ResultHandles, out hv_DecodedDataStrings);
            HOperatorSet.ClearDataCode2dModel(hv_DataCodeHandle);



            if ((int)(new HTuple((new HTuple(hv_DecodedDataStrings.TupleLength())).TupleNotEqual(
                                     0))) != 0)
            {
                hv_flag = 1;
                HOperatorSet.AreaCenterXld(ho_SymbolXLDs, out hv_Area, out hv_Row, out hv_Column,
                                           out hv_PointOrder);
            }
            else
            {
                hv_flag = 0;
            }
            ho_GrayImage1.Dispose();

            return;
        }
Пример #4
0
 private void Dispse(bool disposing)
 {
     if (m_hasdispose)
     {
         return;
     }
     if (disposing)
     {
         HOperatorSet.ClearDataCode2dModel(m_codehandle);
     }
     m_hasdispose = true;
 }
Пример #5
0
        private void findCode(HObject hObj, HTuple codeType, out string result)
        {
            // Local iconic variables
            HObject ho_Image, ho_SymbolXLDs;
            //HObject ho_ImageZoomed = null;
            // Local control variables
            HTuple hv_DataCodeHandle = null, hv_ResultHandles = null;
            HTuple hv_DecodedDataStrings = null;
            HTuple hv_Width = null, hv_Height = null;

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_Image);
            //HOperatorSet.GenEmptyObj(out ho_ImageZoomed);
            HOperatorSet.GenEmptyObj(out ho_SymbolXLDs);

            HOperatorSet.CreateDataCode2dModel(codeType, "default_parameters", "maximum_recognition", out hv_DataCodeHandle);
            ho_Image.Dispose();

            ho_Image = hObj.Clone();
            //HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
            ////int width = hv_Width;
            //int height = hv_Height;
            //if (hv_Width>2000 || height>2000)
            //{
            //    ho_ImageZoomed.Dispose();
            //    HOperatorSet.ZoomImageFactor(ho_Image, out ho_ImageZoomed, 0.5, 0.5, "bilinear");
            //}
            //else
            //{
            //    ho_ImageZoomed.Dispose();
            //    HOperatorSet.ZoomImageFactor(ho_Image, out ho_ImageZoomed, 1, 1, "bilinear");
            //}
            //Read the symbol in the image
            ho_SymbolXLDs.Dispose();
            HOperatorSet.FindDataCode2d(ho_Image, out ho_SymbolXLDs, hv_DataCodeHandle, new HTuple(),
                                        new HTuple(), out hv_ResultHandles, out hv_DecodedDataStrings);
            //Clear the model
            HOperatorSet.ClearDataCode2dModel(hv_DataCodeHandle);
            ho_Image.Dispose();
            //ho_ImageZoomed.Dispose();
            ho_SymbolXLDs.Dispose();

            result = hv_DecodedDataStrings.ToString();
        }
        /// <summary>
        /// 条码识别
        /// </summary>
        /// <param name="img"></param>
        /// <param name="Roi"></param>
        /// <param name="type"></param>
        /// <param name="Barcode"></param>
        /// <returns></returns>
        public static string ReadCode(VisionImage img, Roi Roi, CodeType type)
        {
            //HObject img,HObject Roi,BarcodeType type,out string Barcode,out HObject Box
            HObject Box, Himage, HRoi, imgreduced;
            string  Barcode = "";
            HTuple  BarCodeHandle = null, DecodedDataStrings;

            img.Type = ImageType.U8; img.BorderWidth = 0;
            HOperatorSet.GenEmptyObj(out Box);
            HOperatorSet.GenEmptyObj(out Himage);
            HOperatorSet.GenEmptyObj(out HRoi);
            HOperatorSet.GenEmptyObj(out imgreduced);
            try
            {
                HOperatorSet.GenImage1(out Himage, "byte", img.Width, img.Height, img.StartPtr);
                HOperatorSet.GenRectangle1(out HRoi, ((RectangleContour)Roi[0].Shape).Top, ((RectangleContour)Roi[0].Shape).Left, ((RectangleContour)Roi[0].Shape).Top + ((RectangleContour)Roi[0].Shape).Height, ((RectangleContour)Roi[0].Shape).Left + ((RectangleContour)Roi[0].Shape).Width);
                HOperatorSet.ReduceDomain(Himage, HRoi, out imgreduced);//image_rectified
                switch (type)
                {
                case CodeType.Code_1D129:
                    HOperatorSet.CreateBarCodeModel(new HTuple(), new HTuple(), out BarCodeHandle);
                    HOperatorSet.FindBarCode(Himage, out Box, BarCodeHandle,
                                             "Code 128", out DecodedDataStrings);
                    HOperatorSet.ClearBarCodeModel(BarCodeHandle);
                    if (DecodedDataStrings.Length > 0)
                    {
                        Barcode = DecodedDataStrings.SArr[0];
                    }
                    break;

                case CodeType.Code_1D39:
                    HOperatorSet.CreateBarCodeModel(new HTuple(), new HTuple(), out BarCodeHandle);
                    HOperatorSet.FindBarCode(Himage, out Box, BarCodeHandle,
                                             "Code 39", out DecodedDataStrings);
                    HOperatorSet.ClearBarCodeModel(BarCodeHandle);
                    if (DecodedDataStrings.Length > 0)
                    {
                        Barcode = DecodedDataStrings.SArr[0];
                    }
                    break;

                case CodeType.Code_2D_Mat:
                    HTuple ResultHandles_DataMatrix = null;
                    HOperatorSet.CreateDataCode2dModel("Data Matrix ECC 200", new HTuple(), new HTuple(), out BarCodeHandle);
                    HOperatorSet.SetDataCode2dParam(BarCodeHandle, "default_parameters", "maximum_recognition");
                    HOperatorSet.FindDataCode2d(Himage, out Box, BarCodeHandle, "stop_after_result_num",
                                                1 /*找寻1个*/, out ResultHandles_DataMatrix, out DecodedDataStrings);
                    HOperatorSet.ClearDataCode2dModel(BarCodeHandle);
                    if (DecodedDataStrings.Length > 0)
                    {
                        Barcode = DecodedDataStrings.SArr[0];
                    }
                    break;

                case CodeType.Code_QR:
                    HTuple ResultHandles_QR = null;
                    HOperatorSet.CreateDataCode2dModel("QR Code", new HTuple(), new HTuple(), out BarCodeHandle);
                    HOperatorSet.FindDataCode2d(Himage, out Box, BarCodeHandle, new HTuple(), new HTuple(), out ResultHandles_QR, out DecodedDataStrings);
                    HOperatorSet.ClearDataCode2dModel(BarCodeHandle);
                    if (DecodedDataStrings.Length > 0)
                    {
                        Barcode = DecodedDataStrings.SArr[0];
                    }
                    break;
                }
                if (Box != null)
                {
                    Box.Dispose();
                }
                if (Himage != null)
                {
                    Himage.Dispose();
                }
                if (HRoi != null)
                {
                    HRoi.Dispose();
                }
                if (imgreduced != null)
                {
                    imgreduced.Dispose();
                }
                return(Barcode);
            }
            catch
            {
                if (Box != null)
                {
                    Box.Dispose();
                }
                if (Himage != null)
                {
                    Himage.Dispose();
                }
                if (HRoi != null)
                {
                    HRoi.Dispose();
                }
                if (imgreduced != null)
                {
                    imgreduced.Dispose();
                }
                return("");
            }
        }