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); }