////////////////////////////////////////////////////////////////////////////// // IPRun() - The thread function IPRun performs the image processing. // It waits for the grabbing thread to indicate a new image in the // image list. After calling the operator FindDataCode2D, the // result values are stored in the ResultContainer instance // resultData, which can be entered only after the previous result // values were displayed (containerIsFree-event). ////////////////////////////////////////////////////////////////////////////// public void IPRun() { // ------------------- INIT ---------------- HDataCode2D reader = new HDataCode2D("Data Matrix ECC 200", new HTuple(), new HTuple()); reader.SetDataCode2dParam("default_parameters", "enhanced_recognition"); // ----------- WAIT FOR EVENTS --------------- while (newImgEvent.WaitOne()) { newImgMutex.WaitOne(); // CriticalSect HImage image = (HImage)imgList[0]; // CriticalSect imgList.Remove(image); // CriticalSect newImgMutex.ReleaseMutex(); // CriticalSect HTuple t1 = HSystem.CountSeconds(); HTuple decodedDataStrings, resultHandle; HXLD symbolXLDs = reader.FindDataCode2d(image, new HTuple(), new HTuple(), out resultHandle, out decodedDataStrings); HTuple t2 = HSystem.CountSeconds(); containerIsFreeEvent.WaitOne(); resultDataMutex.WaitOne(); // CriticalSect resultData.timeNeeded = (1000 * (t2 - t1)); // CriticalSect resultData.decodedData = decodedDataStrings; // CriticalSect resultData.resultImg = image; // CriticalSect resultData.resultHandle = resultHandle; // CriticalSect resultData.symbolData = symbolXLDs; // CriticalSect containerIsFreeEvent.Reset(); // CriticalSect resultDataMutex.ReleaseMutex(); // CriticalSect newResultEvent.Set(); mainForm.Invoke(delegateDisplay); if (delegatedStopEvent.WaitOne(0, true)) { break; } } // -------- RESET/CLOSE ALL HANDLES --------- mainForm.threadAcq.Join(); mainForm.Invoke(delegateControlReset); reader.Dispose(); newResultEvent.Reset(); return; }
private void OnDeserializedMethod(StreamingContext context) { _CodeReaderHand = new HDataCode2D("Data Matrix ECC 200", new HTuple(), new HTuple()); //_CodeReaderHand = new HDataCode2D("QR Code", new HTuple(), new HTuple()); _CodeReaderHand.SetDataCode2dParam("default_parameters", "standard_recognition"); _serachRegion = new HRegion(); DataCodeString = string.Empty; DataCodeContour = new HXLDCont(); DisplayControl = new List <HWindowControl>(); }
public CodeReader() { _CodeReaderHand = new HDataCode2D("Data Matrix ECC 200", new HTuple(), new HTuple()); //_CodeReaderHand = new HDataCode2D("QR Code", new HTuple(), new HTuple()); _CodeReaderHand.SetDataCode2dParam("default_parameters", "standard_recognition"); DateCodeZoomImg = 1f; DateCodeTimeOut = true; UseBrightenValue = 1.5f; }
public static string[] GetBarcode2() { string[] res = new string[2]; DataCodeHandle2 = new HDataCode2D("Data Matrix ECC 200", new HTuple(), new HTuple()); HObject image1, image2; HObject Roi1, Roi2; HObject symbolXLD1, symbolXLD2; HOperatorSet.ReadObject(out Roi1, System.Environment.CurrentDirectory + "\\rectangle3.hobj"); HOperatorSet.ReadObject(out Roi2, System.Environment.CurrentDirectory + "\\rectangle4.hobj"); HOperatorSet.ReduceDomain(Camera2.CurrentImage, Roi1, out image1); HOperatorSet.ReduceDomain(Camera2.CurrentImage, Roi2, out image2); HOperatorSet.FindDataCode2d(new HImage(image1), out symbolXLD1, DataCodeHandle2, new HTuple(), new HTuple(), out ResultHandles2, out DecodedDataStrings2); res[0] = new HTuple((new HTuple(DecodedDataStrings2.TupleLength())).TupleEqual(1)) == 1 ? DecodedDataStrings2.TupleSelect(0).ToString().Replace("\"", "") : "error"; HOperatorSet.FindDataCode2d(new HImage(image2), out symbolXLD2, DataCodeHandle2, new HTuple(), new HTuple(), out ResultHandles2, out DecodedDataStrings2); res[1] = new HTuple((new HTuple(DecodedDataStrings2.TupleLength())).TupleEqual(1)) == 1 ? DecodedDataStrings2.TupleSelect(0).ToString().Replace("\"", "") : "error"; viewController2.addIconicVar(symbolXLD1); viewController2.addIconicVar(symbolXLD2); viewController2.addIconicVar(Roi1); viewController2.addIconicVar(Roi2); viewController2.viewPort.HalconWindow.SetColor("green"); viewController2.repaint(); HTuple area, rows, columns; HOperatorSet.AreaCenter(Roi1, out area, out rows, out columns); int[] co = GetCorinWindow(viewController2.viewPort.HalconWindow, Camera2.CurrentImage, (int)rows.D, (int)columns.D); HOperatorSet.DispText(viewController2.viewPort.HalconWindow, "① " + res[0], "window", co[0], co[1], "black", "box", "true"); HOperatorSet.AreaCenter(Roi2, out area, out rows, out columns); co = GetCorinWindow(viewController2.viewPort.HalconWindow, Camera2.CurrentImage, (int)rows.D, (int)columns.D); HOperatorSet.DispText(viewController2.viewPort.HalconWindow, "② " + res[1], "window", co[0], co[1], "black", "box", "true"); image1.Dispose(); image2.Dispose(); DataCodeHandle2.Dispose(); return(res); }
public Vision() { //------------------------------------------------------------ // Khởi tạo Camera //------------------------------------------------------------ GenCamera.Init_Acquisition(out HFramegrabber); //------------------------------------------------------------ // Khởi tạo đối tượng đọc code //------------------------------------------------------------ HDataCode2D_QRCode = new HDataCode2D("QR Code", new HTuple(), new HTuple()); HDataCode2D_DataMatrix = new HDataCode2D("Data Matrix ECC 200", new HTuple("default_parameters"), new HTuple("enhanced_recognition")); HBarcode = new HBarCode(new HTuple(), new HTuple()); //------------------------------------------------------------ // Load ROI đọc code của 3 loại code //------------------------------------------------------------ HOperatorSet.SetSystem(new HTuple("clip_region"), new HTuple("false")); HOperatorSet.ReadRegion(out HRegion_QRCode, new HTuple("D:/Halcon/ROI_QR.hobj")); HOperatorSet.ReadRegion(out HRegion_DataMatrix, new HTuple("D:/Halcon/ROI_DM.hobj")); HOperatorSet.ReadRegion(out HRegion_Barcode, new HTuple("D:/Halcon/ROI_B39.hobj")); }
//------------------------------------------------------------ // Đọc code từ ảnh gửi vào, trả về kết quả dạng string //------------------------------------------------------------ public string Run(HObject imageIn) { HObject imgCrop; HObject symbolQRCode, symbolDMCode, symbolBarcode; HTuple decodedDataBarcode, resultDMCode, resultQRCode, decodedDataQRCode, decodedDataDMCode; HTuple resultTuple; string returnStr = ""; resultTuple = new HTuple(); // Lấy ảnh theo vùng ROI HOperatorSet.ReduceDomain(imageIn, ROI, out imgCrop); // Sử dụng Tool đọc code tương ứng, xử lý ảnh đã Crop // Kết quả trả về resultTuple switch (Type) { case 0: // Barcode type ---------------------------------------------------------- if (BarcodeTool == null) { BarcodeTool = new HBarCode(new HTuple(), new HTuple()); } // Find Code HOperatorSet.FindBarCode(imgCrop, out symbolBarcode, BarcodeTool, new HTuple("auto"), out decodedDataBarcode); // Get Result resultTuple = decodedDataBarcode; break; case 1: // DMCode type ------------------------------------------------------------ if (DMCodeTool == null) { DMCodeTool = new HDataCode2D("Data Matrix ECC 200", new HTuple("default_parameters"), new HTuple("enhanced_recognition")); } // Find Code HOperatorSet.FindDataCode2d(imgCrop, out symbolDMCode, DMCodeTool, new HTuple(), new HTuple(), out resultDMCode, out decodedDataDMCode); // Get Result resultTuple = decodedDataDMCode; break; case 2: // QRCode type ------------------------------------------------------------- if (QRCodeTool == null) { QRCodeTool = new HDataCode2D("QR Code", new HTuple(), new HTuple()); } // Find Code HOperatorSet.FindDataCode2d(imgCrop, out symbolQRCode, QRCodeTool, new HTuple(), new HTuple(), out resultQRCode, out decodedDataQRCode); // Get Result resultTuple = decodedDataQRCode; break; default: break; } // Kiểm tra kết quả tool đọc code, lọc lấy code cần tìm if (resultTuple.Length == 0) { returnStr = "ERR"; } else { // Filter theo độ dài + chuỗi ký tự bắt buộc (filterString) foreach (string item in resultTuple.SArr) { // Filter if ((item.Length > MaxLength) || (item.Length < MinLength)) { continue; } if (!item.Contains(FilterString)) { continue; } // Lấy code dài nhất đã filter if (item.Length > returnStr.Length) { returnStr = item; } } // Nếu đã lọc hết => trả về ERR if (returnStr.Length < MinLength) { returnStr = "ERR"; } } return(returnStr); }
public override VisionResult Detected(VisionImage image, Dictionary <string, VisionResult> Result = null, VisionFlow parent = null, Shape newRoi = null) { VisionResult rtn = new VisionResult(); rtn.State = VisionResultState.WaitCal; if (ROI == null || image == null) { rtn.State = VisionResultState.NG; return(rtn); } try { using (VisionImage temp = new VisionImage()) { var roi = this.ROI; if (newRoi != null) { roi = newRoi; } using (HImage hImage = VisionHelper.Image(image)) { RectangleContour rect = roi as RectangleContour; using (HImage reduceImage = hImage.ReduceDomain( new HRegion(rect.Top, rect.Left, rect.Top + rect.Height, rect.Left + rect.Width))) { string code = string.Empty; switch (CodeType) { case CodeType.Code_2D_Mat: code = "Data Matrix ECC 200"; break; case CodeType.Code_QR: code = "QR Code"; break; } using (HDataCode2D code2D = new HDataCode2D(code, "default_parameters", "maximum_recognition")) { HTuple result = new HTuple(); HTuple resultString = new HTuple(); code2D.FindDataCode2d(reduceImage, "stop_after_result_num", 1, out result, out resultString); if (resultString.Length > 0) { rtn.BarCode = resultString.SArr[0]; this.AddVisionResc(rtn, $"条码寻找成功:{rtn.BarCode}"); rtn.State = VisionResultState.OK; image.Overlays.Default.AddText(rtn.BarCode, new PointContour(rect.Left, rect.Top), Rgb32Value.BlueColor, new OverlayTextOptions("Consolas", 128)); } else { rtn.State = VisionResultState.NG; this.AddVisionResc(rtn, $"没有找到条码"); } } } } } } catch (HalconException ex) { this.AddVisionResc(rtn, $"没有找到条码 {ex.GetErrorMessage()}"); } return(rtn); }