public static string ReadCode(VisionImage image, Roi roi, CodeType type) { string code = string.Empty; try { if (type == CodeType.Code_1D39) { } else if (type == CodeType.Code_1D129) { } else if (type == CodeType.Code_1D93) { } else if (type == CodeType.Code_2D_Mat) { DataMatrixReport datamatrixreport = Algorithms.ReadDataMatrixBarcode(image, roi); if (datamatrixreport.StringData != null) { code = datamatrixreport.StringData; } } else if (type == CodeType.Code_QR) { QRReport qrreport = new QRReport(); qrreport = Algorithms.ReadQRCode(image, roi); System.Text.ASCIIEncoding vaASCIIEncoding = new System.Text.ASCIIEncoding(); if (qrreport != null) { code = vaASCIIEncoding.GetString(qrreport.GetData()); } } } catch { code = string.Empty; } return(code); }
public string ProcessDatamatrix(VisionImage image, TemplateConfig templateConfig, List <DataMatrixConfig> dataMatrixConfigs) { string dataMatrixInfo = string.Empty; // Initialize the IVA_Data structure to pass results and coordinate systems. IVA_Data ivaData = new IVA_Data(3, 1); // Creates a new, empty region of interest. Roi roi = new Roi(); //// Creates a new RotatedRectangleContour using the given values. //RotatedRectangleContour vaRotatedRect = new RotatedRectangleContour(vaCenter, 1268, 1220, 0); // Creates a new RectangleContour using the given values. RectangleContour vaRotatedRect = new RectangleContour(templateConfig.Rectangle.Left, templateConfig.Rectangle.Top, templateConfig.Rectangle.Width, templateConfig.Rectangle.Height); roi.Add(vaRotatedRect); image.Overlays.Default.AddRoi(roi); // MatchPattern Grayscale MatchMode vaMode = MatchMode.RotationInvariant; bool vaSubpixelVal = false; int[] minAngleVals = { -30, 0 }; int[] maxAngleVals = { 30, 0 }; int vaNumMatchesRequested = 1; double vaMinMatchScore = 800; double vaOffsetX = 0; double vaOffsetY = 0; pmResults = IVA_MatchPattern(image, ivaData, templateConfig.TemplatePath, vaMode, vaSubpixelVal, minAngleVals, maxAngleVals, vaNumMatchesRequested, vaMinMatchScore, roi, vaOffsetX, vaOffsetY, 0); foreach (PatternMatch match in pmResults) { image.Overlays.Default.AddPolygon(new PolygonContour(match.Corners), Rgb32Value.RedColor); } roi.Dispose(); // Set Coordinate System int vaCoordSystemIndex = 0; int stepIndexOrigin = 0; int resultIndexOrigin = 1; int stepIndexAngle = 0; int resultIndexAngle = 3; double refSysOriginX = templateConfig.Position.X; double refSysOriginY = templateConfig.Position.Y; double refSysAngle = 0; AxisOrientation refSysAxisOrientation = AxisOrientation.Direct; int vaCoordSystemType = 3; IVA_CoordSys(vaCoordSystemIndex, stepIndexOrigin, resultIndexOrigin, stepIndexAngle, resultIndexAngle, refSysOriginX, refSysOriginY, refSysAngle, refSysAxisOrientation, vaCoordSystemType, ivaData); for (int i = 0; i < dataMatrixConfigs.Count; i++) { // Creates a new, empty region of interest. Roi roiDM = new Roi(); // Creates a new RectangleContour using the given values. RectangleContour vaRect = new RectangleContour(dataMatrixConfigs[i].Rectangle.Left, dataMatrixConfigs[i].Rectangle.Top, dataMatrixConfigs[i].Rectangle.Width, dataMatrixConfigs[i].Rectangle.Height); roiDM.Add(vaRect); // Reposition the region of interest based on the coordinate system. int coordSystemIndex = 0; Algorithms.TransformRoi(roiDM, new CoordinateTransform(ivaData.baseCoordinateSystems[coordSystemIndex], ivaData.MeasurementSystems[coordSystemIndex])); image.Overlays.Default.AddRoi(roiDM); // Read DataMatrix Barcode DataMatrixDescriptionOptions vaDescriptionOptions = new DataMatrixDescriptionOptions(); vaDescriptionOptions.AspectRatio = 0; vaDescriptionOptions.CellFill = DataMatrixCellFillMode.AutoDetect; uint matrixSizeColumns = 25; uint.TryParse(dataMatrixConfigs[i].MatrixSize.Split('X')[1], out matrixSizeColumns); vaDescriptionOptions.Columns = matrixSizeColumns; vaDescriptionOptions.MinimumBorderIntegrity = 90; vaDescriptionOptions.MirrorMode = DataMatrixMirrorMode.AutoDetect; vaDescriptionOptions.Polarity = dataMatrixConfigs[0].Polarity; vaDescriptionOptions.Rectangle = false; uint matrixSizeRows = 25; uint.TryParse(dataMatrixConfigs[i].MatrixSize.Split('X')[0], out matrixSizeRows); vaDescriptionOptions.Rows = matrixSizeRows; DataMatrixSizeOptions vaSizeOptions = new DataMatrixSizeOptions(); vaSizeOptions.MaximumSize = 250; vaSizeOptions.MinimumSize = 50; vaSizeOptions.QuietZoneWidth = 0; DataMatrixSearchOptions vaSearchOptions = new DataMatrixSearchOptions(); vaSearchOptions.CellFilterMode = DataMatrixCellFilterMode.AutoDetect; vaSearchOptions.CellSampleSize = dataMatrixConfigs[0].CellSize; vaSearchOptions.DemodulationMode = DataMatrixDemodulationMode.AutoDetect; vaSearchOptions.EdgeThreshold = 30; vaSearchOptions.InitialSearchVectorWidth = 5; vaSearchOptions.MaximumIterations = 150; vaSearchOptions.RotationMode = DataMatrixRotationMode.Unlimited; vaSearchOptions.SkewDegreesAllowed = 5; vaSearchOptions.SkipLocation = false; // Reads the data matrix from the image. vaDataMatrixReport = Algorithms.ReadDataMatrixBarcode(image, roiDM, DataMatrixGradingMode.None, vaDescriptionOptions, vaSizeOptions, vaSearchOptions); if (vaDataMatrixReport.Found) { image.Overlays.Default.AddPolygon(new PolygonContour(vaDataMatrixReport.Corners), Rgb32Value.RedColor, DrawingMode.DrawValue); } dataMatrixInfo += string.Format("{0},", vaDataMatrixReport.StringData); roiDM.Dispose(); } dataMatrixInfo = dataMatrixInfo.Substring(0, dataMatrixInfo.Length - 1); // Dispose the IVA_Data structure. ivaData.Dispose(); // Return the palette type of the final image. return(dataMatrixInfo); }
// timer1_Tick is called when the timer interval has passed. The next image // is read in, barcode settings are initialized based on the type, // and the barcode is read and processed. private void timer1_Tick(object sender, EventArgs e) { // Stop the timer so that we don't count the time spent reading the barcode timer1.Enabled = false; using (VisionImage tempImage = new VisionImage()) { // Load the barcode image. tempImage.ReadVisionFile(curImages[imageIndex]); Collection <Pdf417Report> pdf417Report = new Collection <Pdf417Report>(); DataMatrixReport dmReport = new DataMatrixReport(); QRReport qrReport = new QRReport(); // Read barcode and time how long it takes. int startTime = System.Environment.TickCount; if (barcodeType.Text == "PDF417") { // Decode a PDF417 code. pdf417Report = Algorithms.ReadPdf417Barcode(tempImage); } else if (barcodeType.Text == "Data Matrix") { // Decode a Data Matrix DataMatrixDescriptionOptions descOptions = new DataMatrixDescriptionOptions(); DataMatrixSizeOptions sizeOptions = new DataMatrixSizeOptions(); DataMatrixSearchOptions searchOptions = new DataMatrixSearchOptions(); if (useOptionsBox.Checked) { // Read the options from the image. GetDataMatrixCodeSettings(tempImage, ref descOptions, ref sizeOptions, ref searchOptions); } DataMatrixGradingMode gradingMode = DataMatrixGradingMode.None; if (gradeDMBox.Checked) { gradingMode = DataMatrixGradingMode.PrepareForAim; } dmReport = Algorithms.ReadDataMatrixBarcode(tempImage, null, gradingMode, descOptions, sizeOptions, searchOptions); } else { // Decode a QR Code QRDescriptionOptions descOptions = new QRDescriptionOptions(); QRSizeOptions sizeOptions = new QRSizeOptions(); QRSearchOptions searchOptions = new QRSearchOptions(); if (useOptionsBox.Checked) { // Read the options from the image. } qrReport = Algorithms.ReadQRCode(tempImage, null, descOptions, sizeOptions, searchOptions); } int elapsedTime = System.Environment.TickCount - startTime; bool found; if (barcodeType.Text == "PDF417") { found = pdf417Report.Count > 0; } else if (barcodeType.Text == "Data Matrix") { found = dmReport.Found; } else { found = qrReport.Found; } // Process info if (found) { PointContour centerPoint = new PointContour(); if (barcodeType.Text == "PDF417") { dataFound.Text = pdf417Report[0].StringData; typeFound.Text = "Pdf417"; tempImage.Overlays.Default.AddPolygon(new PolygonContour(pdf417Report[0].Corners), Rgb32Value.GreenColor, DrawingMode.DrawValue); // Center the viewer on the barcode. centerPoint.Initialize((pdf417Report[0].Corners[0].X + pdf417Report[0].Corners[2].X) / 2, (pdf417Report[0].Corners[0].Y + pdf417Report[0].Corners[2].Y) / 2); } else if (barcodeType.Text == "Data Matrix") { if (dmReport.Binary) { dataFound.Text = System.Text.Encoding.Default.GetString(dmReport.GetBinaryData()); } else { dataFound.Text = dmReport.StringData; } DisplayDataMatrixType(dmReport); tempImage.Overlays.Default.AddPolygon(new PolygonContour(dmReport.Corners), Rgb32Value.GreenColor, DrawingMode.DrawValue); // Center the viewer on the barcode. centerPoint.Initialize((dmReport.Corners[0].X + dmReport.Corners[2].X) / 2, (dmReport.Corners[0].Y + dmReport.Corners[2].Y) / 2); // Grade the barcode if requested. if (gradeDMBox.Checked) { AimGradeReport gradeReport = Algorithms.GradeDataMatrixBarcodeAim(tempImage); gradeOverall.Text = gradeReport.OverallGrade.ToString(); gradeDecoding.Text = gradeReport.DecodingGrade.ToString(); gradeSymbolContrast.Text = gradeReport.SymbolContrastGrade.ToString(); gradePrintGrowth.Text = gradeReport.PrintGrowthGrade.ToString(); gradeAxialNonuniformity.Text = gradeReport.AxialNonuniformityGrade.ToString(); gradeUnusedErrorCorrection.Text = gradeReport.UnusedErrorCorrectionGrade.ToString(); } else { gradeOverall.Text = ""; gradeDecoding.Text = ""; gradeSymbolContrast.Text = ""; gradePrintGrowth.Text = ""; gradeAxialNonuniformity.Text = ""; gradeUnusedErrorCorrection.Text = ""; } } else { dataFound.Text = System.Text.Encoding.Default.GetString(qrReport.GetData()); DisplayQRType(qrReport); tempImage.Overlays.Default.AddPolygon(new PolygonContour(qrReport.Corners), Rgb32Value.GreenColor, DrawingMode.DrawValue); // Center the viewer on the barcode. centerPoint.Initialize((qrReport.Corners[0].X + qrReport.Corners[2].X) / 2, (qrReport.Corners[0].Y + qrReport.Corners[2].Y) / 2); } readTime.Text = elapsedTime.ToString(); Algorithms.Copy(tempImage, imageViewer1.Image); imageViewer1.RefreshImage(); imageViewer1.Center.Initialize(centerPoint.X, centerPoint.Y); } } // Set up for next image imageIndex = (imageIndex + 1) % curImages.Count; timer1.Enabled = true; }