public override bool GenObj(HObject image, VisionControl visionControl) { try { HTuple hv_BarCodeModel = null; if (Mode1dCode != null) { HOperatorSet.ClearBarCodeModel(Mode1dCode); } HOperatorSet.CreateBarCodeModel(new HTuple(), new HTuple(), out hv_BarCodeModel); Mode1dCode = hv_BarCodeModel; string Mode1dcodePath = VisionMgr.GetInstance().CurrentVisionProcessDir + "\\" + m_strStepName + "\\" + m_strStepName + "_bar_code_model.bcm"; HOperatorSet.WriteBarCodeModel(Mode1dCode, Mode1dcodePath); 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 + "创建1CodeModle失败:" + e.Message, "Err", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } return(true); }
public override Object Read(string strPath) { Code1dParam tempvision1dCodeParam = (Code1dParam)AccessXmlSerializer.XmlToObject(strPath, vision1dCodeParam.GetType()); if (tempvision1dCodeParam == null) { _logger.Warn(m_strStepName + ": 视觉处理项目加载失败,请检查"); MessageBox.Show(m_strStepName + ": 视觉处理项目加载失败,请检查", "Err", MessageBoxButtons.OK, MessageBoxIcon.Error); return(null); } vision1dCodeParam = tempvision1dCodeParam; string Mode1dcodePath = VisionMgr.GetInstance().CurrentVisionProcessDir + "\\" + m_strStepName + "\\" + m_strStepName + "_bar_code_model.bcm";; if (Mode1dcodePath != null && File.Exists(Mode1dcodePath)) { if (Mode1dCode != null) { HOperatorSet.ClearBarCodeModel(Mode1dCode); } HOperatorSet.ReadBarCodeModel(Mode1dcodePath, out Mode1dCode); } if (File.Exists(vision1dCodeParam.Mode1dcodeSearchPath)) { if (Mode1dCodeSearch != null && Mode1dCodeSearch.IsInitialized()) { Mode1dCodeSearch.Dispose(); } HOperatorSet.ReadRegion(out Mode1dCodeSearch, vision1dCodeParam.Mode1dcodeSearchPath); } GC.Collect(); return(vision1dCodeParam); }
public override void Disopose() { if (Mode1dCode != null) { HOperatorSet.ClearBarCodeModel(Mode1dCode); } if (Mode1dCodeSearch != null && Mode1dCodeSearch.IsInitialized()) { Mode1dCodeSearch.Dispose(); } }
public void Decode(Bitmap input) { Bitmap2HImage(input); HObject ho_SymbolRegions = null; HOperatorSet.GenEmptyObj(out ho_SymbolRegions); ho_SymbolRegions.Dispose(); HTuple hv_BarCodeHandle = null, hv_DecodedDataStrings = new HTuple(); HOperatorSet.CreateBarCodeModel(new HTuple(), new HTuple(), out hv_BarCodeHandle); //HObject test_inv; //HOperatorSet.InvertImage(test, out test_inv); //HOperatorSet.FindBarCode(test_inv, out ho_SymbolRegions, hv_BarCodeHandle, "auto", out hv_DecodedDataStrings); var watch = System.Diagnostics.Stopwatch.StartNew(); try { HOperatorSet.FindBarCode(this.source, out ho_SymbolRegions, hv_BarCodeHandle, "auto", out hv_DecodedDataStrings); } catch (Exception) { } watch.Stop(); Time = (int)watch.ElapsedMilliseconds; HTuple hv_Reference = new HTuple(), hv_String = new HTuple(), hv_J = new HTuple(), hv_Char = new HTuple(); if (hv_DecodedDataStrings.Length > 0) { IsFound = true; using (HRegion region = new HRegion(ho_SymbolRegions)) { HTuple row = new HTuple(), column = new HTuple(), phi = new HTuple(), length1 = new HTuple(), length2 = new HTuple(); region.SmallestRectangle2(out row, out column, out phi, out length1, out length2); for (int i = 0; i < 1; i++) { double angle = phi.DArr[i] / 0.0174532F; // if (angle < 1) // { int x = (int)(column.DArr[i] - length1.DArr[i]); int y = (int)(row.DArr[i] - length2.DArr[i]); int width = (int)(2 * length1.DArr[i]); int height = (int)(2 * length2.DArr[i]); Rectangle roi = new Rectangle(x, y, width, height); resultPoints[0] = new PointF(roi.Left, roi.Top); resultPoints[1] = new PointF(roi.Right, roi.Top); resultPoints[2] = new PointF(roi.Right, roi.Bottom); resultPoints[3] = new PointF(roi.Left, roi.Bottom); //HOperatorSet.GetBarCodeResult(hv_BarCodeHandle, 0, "decoded_types", out hv_Reference); Result = hv_DecodedDataStrings[0].S; } } } else { IsFound = false; } HOperatorSet.ClearBarCodeModel(hv_BarCodeHandle); //test.Dispose(); ho_SymbolRegions.Dispose(); }
// Main procedure private void action() { // Local iconic variables HObject ho_Image, ho_SymbolRegions = null, ho_CandidateRegions = null; HObject ho_ScanlinesAll = null, ho_ScanlinesValid = null; // Local control variables HTuple hv_UserDefinedMessages, hv_StatusID = new HTuple(); HTuple hv_Status = new HTuple(), hv_ImagePrefix, hv_ImageSuffix; HTuple hv_WindowHandle, hv_BarCodeHandle, hv_CodeType; HTuple hv_Count, hv_IndexExposure, hv_DecodedDataStrings = new HTuple(); HTuple hv_NumInterestingCand = new HTuple(), hv_IndexCandidate = new HTuple(); HTuple hv_String = new HTuple(), hv_StatusIDSplit = new HTuple(); HTuple hv_CenterRows = new HTuple(), hv_CenterColumns = new HTuple(); HTuple hv_Box = new HTuple(), hv_IndexStatus = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_SymbolRegions); HOperatorSet.GenEmptyObj(out ho_CandidateRegions); HOperatorSet.GenEmptyObj(out ho_ScanlinesAll); HOperatorSet.GenEmptyObj(out ho_ScanlinesValid); try { //This example presents the bar code parameters 'status' //and 'status_id' that can be used within get_bar_code_result //to get additional information about possibly occuring //errors when reading a bar code. // //Remark: UserDefinedMessages set to false disables additional //diagnostic messages (overexposure) in this example program hv_UserDefinedMessages = 1; if ((int)(new HTuple(hv_UserDefinedMessages.TupleEqual(0))) != 0) { hv_StatusID = "-1"; } if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } // dev_close_inspect_ctrl(...); only in hdevelop // dev_update_pc(...); only in hdevelop // dev_update_window(...); only in hdevelop hv_ImagePrefix = "barcode/25interleaved/25interleaved_exposure_"; hv_ImageSuffix = ".png"; ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, (hv_ImagePrefix + "01") + hv_ImageSuffix); dev_open_window_fit_image(ho_Image, 0, 0, -1, -1, out hv_WindowHandle); set_display_font(hv_WindowHandle, 14, "mono", "true", "false"); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 2); } // //If get_bar_code_result is called with the parameter 'status', //'persistence' must be set for the bar code model HOperatorSet.CreateBarCodeModel("persistence", 1, out hv_BarCodeHandle); // //For the visualization, the number of scanlines is reduced //(default is 10) HOperatorSet.SetBarCodeParam(hv_BarCodeHandle, "num_scanlines", 5); HOperatorSet.SetBarCodeParam(hv_BarCodeHandle, "check_char", "present"); // // hv_CodeType = "2/5 Interleaved"; hv_Count = 5; for (hv_IndexExposure = 1; hv_IndexExposure.Continue(hv_Count, 1); hv_IndexExposure = hv_IndexExposure.TupleAdd(1)) { ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, (hv_ImagePrefix + (hv_IndexExposure.TupleString( "02"))) + hv_ImageSuffix); // //Try decoding the bar code in the input image ho_SymbolRegions.Dispose(); HOperatorSet.FindBarCode(ho_Image, out ho_SymbolRegions, hv_BarCodeHandle, hv_CodeType, out hv_DecodedDataStrings); ho_CandidateRegions.Dispose(); HOperatorSet.GetBarCodeObject(out ho_CandidateRegions, hv_BarCodeHandle, "all", "candidate_regions"); if ((int)(new HTuple((new HTuple(hv_DecodedDataStrings.TupleLength())).TupleGreater( 0))) != 0) { //If a bar code was found, display only scanlines of //successfully decoded regions (which are the first //regions of all candidate regions) HOperatorSet.CountObj(ho_SymbolRegions, out hv_NumInterestingCand); } else { //If no bar code could be found, display the error //information for all candidate regions HOperatorSet.CountObj(ho_CandidateRegions, out hv_NumInterestingCand); } // //Iterate over all symbol regions or candidate regions //if no symbol could be decoded for (hv_IndexCandidate = 0; hv_IndexCandidate.Continue(hv_NumInterestingCand - 1, 1); hv_IndexCandidate = hv_IndexCandidate.TupleAdd(1)) { // //Display valid scanlines in green, invalid //scanlines in red ho_ScanlinesAll.Dispose(); HOperatorSet.GetBarCodeObject(out ho_ScanlinesAll, hv_BarCodeHandle, hv_IndexCandidate, "scanlines_all"); ho_ScanlinesValid.Dispose(); HOperatorSet.GetBarCodeObject(out ho_ScanlinesValid, hv_BarCodeHandle, hv_IndexCandidate, "scanlines_valid"); if (HDevWindowStack.IsOpen()) { HOperatorSet.ClearWindow(HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_ScanlinesAll, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_ScanlinesValid, HDevWindowStack.GetActive()); } // //Display the index number of the current candidate disp_message(hv_WindowHandle, (("Candidate " + (hv_IndexCandidate + 1)) + " of ") + hv_NumInterestingCand, "image", 72, 12, "forest green", "true"); // if ((int)(hv_UserDefinedMessages) != 0) { // //Get a tuple of strings containing multiple status //identifiers, separated by a semicolon HOperatorSet.GetBarCodeResult(hv_BarCodeHandle, hv_IndexCandidate, "status_id", out hv_StatusID); // //This user-defined procedure can be modified to //translate status identifiers to customized status //messages translate_bar_code_status_id(hv_StatusID, out hv_Status); } else { // //Get tuple of strings containing status messages HOperatorSet.GetBarCodeResult(hv_BarCodeHandle, hv_IndexCandidate, "status", out hv_Status); } // //Show decoded bar code data if ((int)(new HTuple((new HTuple(hv_DecodedDataStrings.TupleLength())).TupleGreater( 0))) != 0) { if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_ScanlinesValid, HDevWindowStack.GetActive() ); } hv_String = "Decoded data strings: " + hv_DecodedDataStrings; // //Combinations of status IDs can be combined to //give further hints for the image acquisition, // e.g. about a possible overexposure HOperatorSet.TupleNumber(hv_StatusID.TupleSplit(";"), out hv_StatusIDSplit); if ((int)((new HTuple(((hv_StatusIDSplit.TupleFind(1000))).TupleNotEqual( -1))).TupleAnd(new HTuple(((hv_StatusIDSplit.TupleFind(1004))).TupleNotEqual( -1)))) != 0) { hv_String = hv_String.TupleConcat("Possible overexposure detected."); } disp_message(hv_WindowHandle, hv_String, "image", 12, 12, "forest green", "true"); } else { disp_message(hv_WindowHandle, "Could not decode bar code", "image", 12, 12, "red", "true"); } // //Display the index number of each scanline at its center scanline_centers(ho_ScanlinesAll, out hv_CenterRows, out hv_CenterColumns); // //If the scanlines lie to close to each other, omit the //background boxes if ((int)(new HTuple(((((((hv_CenterRows.TupleSelect((new HTuple(hv_CenterRows.TupleLength() )) - 2)) - (hv_CenterRows.TupleSelect((new HTuple(hv_CenterRows.TupleLength() )) - 1)))).TuplePow(2)) + ((((hv_CenterColumns.TupleSelect((new HTuple(hv_CenterColumns.TupleLength() )) - 2)) - (hv_CenterColumns.TupleSelect((new HTuple(hv_CenterColumns.TupleLength() )) - 1)))).TuplePow(2)))).TupleGreater(200 * 200))) != 0) { hv_Box = "true"; } else { hv_Box = "false"; } for (hv_IndexStatus = 0; (int)hv_IndexStatus <= (int)((new HTuple(hv_Status.TupleLength() )) - 1); hv_IndexStatus = (int)hv_IndexStatus + 1) { disp_message(hv_WindowHandle, "" + hv_IndexStatus, "image", hv_CenterRows.TupleSelect( hv_IndexStatus), hv_CenterColumns.TupleSelect(hv_IndexStatus), "green", hv_Box); } // dev_inspect_ctrl(...); only in hdevelop if ((int)((new HTuple(hv_IndexCandidate.TupleLess(hv_NumInterestingCand - 1))).TupleOr( new HTuple(hv_IndexExposure.TupleLess(hv_Count)))) != 0) { disp_continue_message(hv_WindowHandle, "black", "true"); HDevelopStop(); } } } // //Free all memory of the bar code model HOperatorSet.ClearBarCodeModel(hv_BarCodeHandle); } catch (HalconException HDevExpDefaultException) { ho_Image.Dispose(); ho_SymbolRegions.Dispose(); ho_CandidateRegions.Dispose(); ho_ScanlinesAll.Dispose(); ho_ScanlinesValid.Dispose(); throw HDevExpDefaultException; } ho_Image.Dispose(); ho_SymbolRegions.Dispose(); ho_CandidateRegions.Dispose(); ho_ScanlinesAll.Dispose(); ho_ScanlinesValid.Dispose(); }
public void BarRecognition(HObject ho_Image) { HTuple hv_BarCodeHandle = new HTuple(); HTuple hv_MeasThreshAbsValue = new HTuple(); HObject ho_SymbolRegions = null, ho_BarCodeObjects = null, ho_ObjectSelected, ho_ImageMirror2; HTuple hv_CodeTypes = new HTuple(); HTuple hv_DecodedDataStrings = new HTuple(), hv_DecodedDataTypes = new HTuple(); HTuple hv_Start = new HTuple(), hv_Stop = new HTuple(), hv_Duration = new HTuple(); HTuple hv_J = new HTuple(); string Codetype; int i; try { // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_ImageMirror2); HOperatorSet.GenEmptyObj(out ho_SymbolRegions); HOperatorSet.GenEmptyObj(out ho_BarCodeObjects); HOperatorSet.GenEmptyObj(out ho_ObjectSelected); HTuple Pointer, type, Width, Height; ho_ImageMirror2.Dispose(); HOperatorSet.MirrorImage(ho_Image, out ho_ImageMirror2, "row"); HOperatorSet.GetImagePointer1(ho_ImageMirror2, out Pointer, out type, out Width, out Height); HOperatorSet.SetPart(hv_ExpDefaultWinHandle, 0, 0, Height, Width); HOperatorSet.DispObj(ho_ImageMirror2, hv_ExpDefaultWinHandle); //hv_MeasThreshAbsValue = 10.0; //hv_CodeTypes = "auto"; hv_CodeTypes[0] = "Code 128"; hv_CodeTypes[1] = "Code 39"; hv_CodeTypes[2] = "Code 93"; hv_CodeTypes[3] = "EAN-13"; HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, "margin"); HOperatorSet.SetLineWidth(hv_ExpDefaultWinHandle, 4); HOperatorSet.CreateBarCodeModel(new HTuple(), new HTuple(), out hv_BarCodeHandle); //HOperatorSet.SetBarCodeParam(hv_BarCodeHandle, "meas_thresh_abs", hv_MeasThreshAbsValue); HOperatorSet.SetBarCodeParam(hv_BarCodeHandle, "element_size_min", 1.0); HOperatorSet.SetBarCodeParam(hv_BarCodeHandle, "element_size_max", 8); HOperatorSet.SetBarCodeParam(hv_BarCodeHandle, "check_char", "present"); HOperatorSet.SetBarCodeParam(hv_BarCodeHandle, "meas_param_estimation", "true"); HOperatorSet.SetBarCodeParam(hv_BarCodeHandle, "majority_voting", "true"); HOperatorSet.SetBarCodeParam(hv_BarCodeHandle, "element_size_variable", "true"); HOperatorSet.SetBarCodeParam(hv_BarCodeHandle, "start_stop_tolerance", "low"); HOperatorSet.SetBarCodeParam(hv_BarCodeHandle, "quiet_zone", "true"); // HOperatorSet.SetBarCodeParam(hv_BarCodeHandle, "stop_after_result_num", 2); ho_SymbolRegions.Dispose(); HOperatorSet.CountSeconds(out hv_Start); HOperatorSet.FindBarCode(ho_ImageMirror2, out ho_SymbolRegions, hv_BarCodeHandle, hv_CodeTypes, out hv_DecodedDataStrings); HOperatorSet.CountSeconds(out hv_Stop); hv_Duration = (hv_Stop - hv_Start) * 1000; ho_BarCodeObjects.Dispose(); HOperatorSet.GetBarCodeObject(out ho_BarCodeObjects, hv_BarCodeHandle, "all", "symbol_regions"); HOperatorSet.GetBarCodeResult(hv_BarCodeHandle, "all", "decoded_types", out hv_DecodedDataTypes); i = new HTuple(hv_DecodedDataStrings.TupleLength()); if (i == 1) { Codetype = hv_DecodedDataTypes.S; HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "forest green"); HOperatorSet.SelectObj(ho_BarCodeObjects, out ho_ObjectSelected, 1); HOperatorSet.DispObj(ho_ObjectSelected, hv_ExpDefaultWinHandle); if (Complete != null) { this.Complete(this, new OnCompleteEventArgs(hv_DecodedDataStrings.S, Codetype, hv_Duration)); } } else { for (hv_J = 0; (int)hv_J <= (int)((new HTuple(hv_DecodedDataStrings.TupleLength())) - 1); hv_J = (int)hv_J + 1) { HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "red"); ho_ObjectSelected.Dispose(); HOperatorSet.SelectObj(ho_BarCodeObjects, out ho_ObjectSelected, hv_J + 1); HOperatorSet.DispObj(ho_ObjectSelected, hv_ExpDefaultWinHandle); } if (Error != null) { this.Error(this, new OnErrorEventArgs(i)); } } HOperatorSet.ClearBarCodeModel(hv_BarCodeHandle); ho_Image.Dispose(); ho_ImageMirror2.Dispose(); ho_SymbolRegions.Dispose(); ho_BarCodeObjects.Dispose(); } catch (Exception ex) { ex.ToString(); } finally { } }
/// <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(""); } }