public DetectionResult(BarcodeMetadata metadata, BoundingBox box) { Metadata = metadata; Box = box; ColumnCount = metadata.ColumnCount; DetectionResultColumns = new DetectionResultColumn[ColumnCount + 2]; }
public DetectionResult(BarcodeMetadata metadata, BoundingBox box) { this.Metadata = metadata; this.Box = box; this.ColumnCount = metadata.ColumnCount; this.DetectionResultColumns = new DetectionResultColumn[ColumnCount + 2]; }
/// <summary> /// Gets the row heights. /// </summary> /// <returns>The row heights.</returns> public int[] getRowHeights() { BarcodeMetadata barcodeMetadata = getBarcodeMetadata(); if (barcodeMetadata == null) { return(null); } adjustIncompleteIndicatorColumnRowNumbers(barcodeMetadata); int[] result = new int[barcodeMetadata.RowCount]; foreach (var codeword in Codewords) { if (codeword != null) { int rowNumber = codeword.RowNumber; if (rowNumber >= result.Length) { // We have more rows than the barcode metadata allows for, ignore them. continue; } result[rowNumber]++; } // else throw exception? (or return null) } return(result); }
public DetectionResult(BarcodeMetadata metadata, BoundingBox box) { this.Metadata = metadata; this.Box = box; this.ColumnCount = metadata.ColumnCount; this.DetectionResultColumns = new DetectionResultColumn[ColumnCount + 2]; }
public DetectionResult(BarcodeMetadata metadata, BoundingBox box) { Metadata = metadata; Box = box; ColumnCount = metadata.ColumnCount; DetectionResultColumns = new DetectionResultColumn[ColumnCount + 2]; }
/// <summary> /// Adjusts the in omplete indicator column row numbers. /// </summary> /// <param name="metadata">Metadata.</param> public int adjustIncompleteIndicatorColumnRowNumbers(BarcodeMetadata metadata) { // TODO maybe we should add missing codewords to store the correct row number to make // finding row numbers for other columns easier // use row height count to make detection of invalid row numbers more reliable ResultPoint top = IsLeft ? Box.TopLeft : Box.TopRight; ResultPoint bottom = IsLeft ? Box.BottomLeft : Box.BottomRight; int firstRow = imageRowToCodewordIndex((int)top.Y); int lastRow = imageRowToCodewordIndex((int)bottom.Y); // We need to be careful using the average row height. // Barcode could be skewed so that we have smaller and taller rows float averageRowHeight = (lastRow - firstRow) / (float)metadata.RowCount; var codewords = Codewords; // initialize loop int barcodeRow = -1; int maxRowHeight = 1; int currentRowHeight = 0; for (int codewordRow = firstRow; codewordRow < lastRow; codewordRow++) { var codeword = codewords[codewordRow]; if (codeword == null) { continue; } codeword.setRowNumberAsRowIndicatorColumn(); int rowDifference = codeword.RowNumber - barcodeRow; // TODO improve handling with case where first row indicator doesn't start with 0 if (rowDifference == 0) { currentRowHeight++; } else if (rowDifference == 1) { maxRowHeight = Math.Max(maxRowHeight, currentRowHeight); currentRowHeight = 1; barcodeRow = codeword.RowNumber; } else if (codeword.RowNumber > metadata.RowCount) { Codewords[codewordRow] = null; } else { barcodeRow = codeword.RowNumber; currentRowHeight = 1; } } return((int)(averageRowHeight + 0.5)); }
/// <summary> /// Gets the barcode metadata. /// </summary> /// <returns>The barcode metadata.</returns> public BarcodeMetadata GetBarcodeMetadata() { BarcodeValue barcodeColumnCount = new BarcodeValue(); BarcodeValue barcodeRowCountUpperPart = new BarcodeValue(); BarcodeValue barcodeRowCountLowerPart = new BarcodeValue(); BarcodeValue barcodeECLevel = new BarcodeValue(); foreach (Codeword codeword in Codewords) { if (codeword == null) { continue; } codeword.SetRowNumberAsRowIndicatorColumn(); int rowIndicatorValue = codeword.Value % 30; int codewordRowNumber = codeword.RowNumber; if (!IsLeft) { codewordRowNumber += 2; } switch (codewordRowNumber % 3) { case 0: barcodeRowCountUpperPart.SetValue(rowIndicatorValue * 3 + 1); break; case 1: barcodeECLevel.SetValue(rowIndicatorValue / 3); barcodeRowCountLowerPart.SetValue(rowIndicatorValue % 3); break; case 2: barcodeColumnCount.SetValue(rowIndicatorValue + 1); break; } } // Maybe we should check if we have ambiguous values? if ((barcodeColumnCount.GetValue().Length == 0) || (barcodeRowCountUpperPart.GetValue().Length == 0) || (barcodeRowCountLowerPart.GetValue().Length == 0) || (barcodeECLevel.GetValue().Length == 0) || barcodeColumnCount.GetValue()[0] < 1 || barcodeRowCountUpperPart.GetValue()[0] + barcodeRowCountLowerPart.GetValue()[0] < PDF417Common.MIN_ROWS_IN_BARCODE || barcodeRowCountUpperPart.GetValue()[0] + barcodeRowCountLowerPart.GetValue()[0] > PDF417Common.MAX_ROWS_IN_BARCODE) { return(null); } BarcodeMetadata barcodeMetadata = new BarcodeMetadata(barcodeColumnCount.GetValue()[0], barcodeRowCountUpperPart.GetValue()[0], barcodeRowCountLowerPart.GetValue()[0], barcodeECLevel.GetValue()[0]); RemoveIncorrectCodewords(barcodeMetadata); return(barcodeMetadata); }
/// <summary> /// Adjusts the in omplete indicator column row numbers. /// </summary> /// <param name="metadata">Metadata.</param> public int AdjustIncompleteIndicatorColumnRowNumbers(BarcodeMetadata metadata) { ResultPoint top = IsLeft ? Box.TopLeft : Box.TopRight; ResultPoint bottom = IsLeft ? Box.BottomLeft : Box.BottomRight; int firstRow = IndexForRow((int)top.Y); int lastRow = IndexForRow((int)bottom.Y); // We need to be careful using the average row height. // Barcode could be skewed so that we have smaller and taller rows float averageRowHeight = (lastRow - firstRow) / (float)metadata.RowCount; // initialize loop int barcodeRow = -1; int maxRowHeight = 1; int currentRowHeight = 0; for (int codewordRow = firstRow; codewordRow < lastRow; codewordRow++) { var codeword = Codewords[codewordRow]; if (codeword == null) { continue; } codeword.SetRowNumberAsRowIndicatorColumn(); int rowDifference = codeword.RowNumber - barcodeRow; // TODO improve handling with case where first row indicator doesn't start with 0 if (rowDifference == 0) { currentRowHeight++; } else if (rowDifference == 1) { maxRowHeight = Math.Max(maxRowHeight, currentRowHeight); currentRowHeight = 1; barcodeRow = codeword.RowNumber; } else if (codeword.RowNumber > metadata.RowCount) { Codewords[codewordRow] = null; } else { barcodeRow = codeword.RowNumber; currentRowHeight = 1; } } return((int)(averageRowHeight + 0.5)); }
/// <summary> /// Prune the codewords which do not match the metadata /// TODO Maybe we should keep the incorrect codewords for the start and end positions? /// </summary> /// <param name="codewords">Codewords.</param> /// <param name="metadata">Metadata.</param> private void removeIncorrectCodewords(Codeword[] codewords, BarcodeMetadata metadata) { for (int row = 0; row < codewords.Length; row++) { var codeword = codewords[row]; if (codeword == null) { continue; } int indicatorValue = codeword.Value % 30; int rowNumber = codeword.RowNumber; // Row does not exist in the metadata if (rowNumber >= metadata.RowCount) // different to java rowNumber > metadata.RowCount { codewords[row] = null; // remove this. continue; } if (!IsLeft) { rowNumber += 2; } switch (rowNumber % 3) { default: case 0: if (indicatorValue * 3 + 1 != metadata.RowCountUpper) { codewords[row] = null; } break; case 1: if (indicatorValue % 3 != metadata.RowCountLower || indicatorValue / 3 != metadata.ErrorCorrectionLevel) { codewords[row] = null; } break; case 2: if (indicatorValue + 1 != metadata.ColumnCount) { codewords[row] = null; } break; } } }
/// <summary> /// Merge the specified leftRowIndicatorColumn and rightRowIndicatorColumn. /// </summary> /// <param name="leftRowIndicatorColumn">Left row indicator column.</param> /// <param name="rightRowIndicatorColumn">Right row indicator column.</param> private static DetectionResult Merge(DetectionResultRowIndicatorColumn leftRowIndicatorColumn, DetectionResultRowIndicatorColumn rightRowIndicatorColumn) { if (leftRowIndicatorColumn == null && rightRowIndicatorColumn == null) { return(null); } BarcodeMetadata barcodeMetadata = GetBarcodeMetadata(leftRowIndicatorColumn, rightRowIndicatorColumn); if (barcodeMetadata == null) { return(null); } BoundingBox boundingBox = GetBoundingBox(leftRowIndicatorColumn, rightRowIndicatorColumn); return(new DetectionResult(barcodeMetadata, boundingBox)); }
/// <summary> /// Gets the row heights. /// </summary> /// <returns>The row heights.</returns> public int[] GetRowHeights() { BarcodeMetadata metadata = GetBarcodeMetadata(); if (metadata == null) { return(null); } AdjustIncompleteIndicatorColumnRowNumbers(metadata); int[] result = Enumerable.Repeat(0, metadata.RowCount).ToArray(); foreach (var word in Codewords) { if (word != null) { result[word.RowNumber]++; } } return(result); }
/// <summary> /// Gets the row heights. /// </summary> /// <returns>The row heights.</returns> public int[] getRowHeights() { BarcodeMetadata barcodeMetadata = getBarcodeMetadata(); if (barcodeMetadata == null) { return(null); } adjustIncompleteIndicatorColumnRowNumbers(barcodeMetadata); int[] result = new int[barcodeMetadata.RowCount]; foreach (var codeword in Codewords) { if (codeword != null) { result[codeword.RowNumber]++; } } return(result); }
/// <summary> /// Gets the barcode metadata. /// </summary> /// <returns>The barcode metadata.</returns> /// <param name="leftRowIndicatorColumn">Left row indicator column.</param> /// <param name="rightRowIndicatorColumn">Right row indicator column.</param> private static BarcodeMetadata getBarcodeMetadata(DetectionResultRowIndicatorColumn leftRowIndicatorColumn, DetectionResultRowIndicatorColumn rightRowIndicatorColumn) { if (leftRowIndicatorColumn == null || leftRowIndicatorColumn.getBarcodeMetadata() == null) { return(rightRowIndicatorColumn == null ? null : rightRowIndicatorColumn.getBarcodeMetadata()); } if (rightRowIndicatorColumn == null || rightRowIndicatorColumn.getBarcodeMetadata() == null) { return(leftRowIndicatorColumn == null ? null : leftRowIndicatorColumn.getBarcodeMetadata()); } BarcodeMetadata leftBarcodeMetadata = leftRowIndicatorColumn.getBarcodeMetadata(); BarcodeMetadata rightBarcodeMetadata = rightRowIndicatorColumn.getBarcodeMetadata(); if (leftBarcodeMetadata.ColumnCount != rightBarcodeMetadata.ColumnCount && leftBarcodeMetadata.ErrorCorrectionLevel != rightBarcodeMetadata.ErrorCorrectionLevel && leftBarcodeMetadata.RowCount != rightBarcodeMetadata.RowCount) { return(null); } return(leftBarcodeMetadata); }
/// <summary> /// TODO implement properly /// TODO maybe we should add missing codewords to store the correct row number to make /// finding row numbers for other columns easier /// use row height count to make detection of invalid row numbers more reliable /// </summary> /// <returns>The indicator column row numbers.</returns> /// <param name="metadata">Metadata.</param> public int adjustCompleteIndicatorColumnRowNumbers(BarcodeMetadata metadata) { var codewords = Codewords; setRowNumbers(); // Assign this as an indicator column removeIncorrectCodewords(codewords, metadata); ResultPoint top = IsLeft ? Box.TopLeft : Box.TopRight; ResultPoint bottom = IsLeft ? Box.BottomLeft : Box.BottomRight; int firstRow = imageRowToCodewordIndex((int)top.Y); int lastRow = imageRowToCodewordIndex((int)bottom.Y); // We need to be careful using the average row height. // Barcode could be skewed so that we have smaller and taller rows float averageRowHeight = (lastRow - firstRow) / (float)metadata.RowCount; // initialize loop int barcodeRow = -1; int maxRowHeight = 1; int currentRowHeight = 0; for (int codewordRow = firstRow; codewordRow < lastRow; codewordRow++) { var codeword = codewords[codewordRow]; if (codeword == null) { continue; } // float expectedRowNumber = (codewordsRow - firstRow) / averageRowHeight; // if (Math.abs(codeword.getRowNumber() - expectedRowNumber) > 2) { // SimpleLog.log(LEVEL.WARNING, // "Removing codeword, rowNumberSkew too high, codeword[" + codewordsRow + "]: Expected Row: " + // expectedRowNumber + ", RealRow: " + codeword.getRowNumber() + ", value: " + codeword.getValue()); // codewords[codewordsRow] = null; // } int rowDifference = codeword.RowNumber - barcodeRow; // TODO improve handling with case where first row indicator doesn't start with 0 if (rowDifference == 0) { currentRowHeight++; } else if (rowDifference == 1) { maxRowHeight = Math.Max(maxRowHeight, currentRowHeight); currentRowHeight = 1; barcodeRow = codeword.RowNumber; } else if (rowDifference < 0 || codeword.RowNumber >= metadata.RowCount || rowDifference > codewordRow) { codewords[codewordRow] = null; } else { int checkedRows; if (maxRowHeight > 2) { checkedRows = (maxRowHeight - 2) * rowDifference; } else { checkedRows = rowDifference; } bool closePreviousCodewordFound = checkedRows > codewordRow; for (int i = 1; i <= checkedRows && !closePreviousCodewordFound; i++) { // there must be (height * rowDifference) number of codewords missing. For now we assume height = 1. // This should hopefully get rid of most problems already. closePreviousCodewordFound = codewords[codewordRow - i] != null; } if (closePreviousCodewordFound) { codewords[codewordRow] = null; } else { barcodeRow = codeword.RowNumber; currentRowHeight = 1; } } } return((int)(averageRowHeight + 0.5)); }
/// <summary> /// Adjusts the in omplete indicator column row numbers. /// </summary> /// <param name="metadata">Metadata.</param> public int AdjustIncompleteIndicatorColumnRowNumbers(BarcodeMetadata metadata) { ResultPoint top = IsLeft ? Box.TopLeft : Box.TopRight; ResultPoint bottom = IsLeft ? Box.BottomLeft : Box.BottomRight; int firstRow = IndexForRow((int)top.Y); int lastRow = IndexForRow((int)bottom.Y); // We need to be careful using the average row height. // Barcode could be skewed so that we have smaller and taller rows float averageRowHeight = (lastRow - firstRow) / (float)metadata.RowCount; // initialize loop int barcodeRow = -1; int maxRowHeight = 1; int currentRowHeight = 0; for (int codewordRow = firstRow; codewordRow < lastRow; codewordRow++) { var codeword = Codewords[codewordRow]; if (codeword == null) { continue; } codeword.SetRowNumberAsRowIndicatorColumn(); int rowDifference = codeword.RowNumber - barcodeRow; // TODO improve handling with case where first row indicator doesn't start with 0 if (rowDifference == 0) { currentRowHeight++; } else if (rowDifference == 1) { maxRowHeight = Math.Max(maxRowHeight, currentRowHeight); currentRowHeight = 1; barcodeRow = codeword.RowNumber; } else if (codeword.RowNumber > metadata.RowCount) { Codewords[codewordRow] = null; } else { barcodeRow = codeword.RowNumber; currentRowHeight = 1; } } return (int)(averageRowHeight + 0.5); }
/// <summary> /// TODO implement properly /// TODO maybe we should add missing codewords to store the correct row number to make /// finding row numbers for other columns easier /// use row height count to make detection of invalid row numbers more reliable /// </summary> /// <returns>The indicator column row numbers.</returns> /// <param name="metadata">Metadata.</param> public int adjustCompleteIndicatorColumnRowNumbers(BarcodeMetadata metadata) { var codewords = Codewords; setRowNumbers(); // Assign this as an indicator column removeIncorrectCodewords(codewords, metadata); ResultPoint top = IsLeft ? Box.TopLeft : Box.TopRight; ResultPoint bottom = IsLeft ? Box.BottomLeft : Box.BottomRight; int firstRow = imageRowToCodewordIndex((int) top.Y); int lastRow = imageRowToCodewordIndex((int) bottom.Y); // We need to be careful using the average row height. // Barcode could be skewed so that we have smaller and taller rows float averageRowHeight = (lastRow - firstRow)/(float) metadata.RowCount; // initialize loop int barcodeRow = -1; int maxRowHeight = 1; int currentRowHeight = 0; for (int codewordRow = firstRow; codewordRow < lastRow; codewordRow++) { var codeword = codewords[codewordRow]; if (codeword == null) { continue; } // float expectedRowNumber = (codewordsRow - firstRow) / averageRowHeight; // if (Math.abs(codeword.getRowNumber() - expectedRowNumber) > 2) { // SimpleLog.log(LEVEL.WARNING, // "Removing codeword, rowNumberSkew too high, codeword[" + codewordsRow + "]: Expected Row: " + // expectedRowNumber + ", RealRow: " + codeword.getRowNumber() + ", value: " + codeword.getValue()); // codewords[codewordsRow] = null; // } int rowDifference = codeword.RowNumber - barcodeRow; // TODO improve handling with case where first row indicator doesn't start with 0 if (rowDifference == 0) { currentRowHeight++; } else if (rowDifference == 1) { maxRowHeight = Math.Max(maxRowHeight, currentRowHeight); currentRowHeight = 1; barcodeRow = codeword.RowNumber; } else if (rowDifference < 0 || codeword.RowNumber >= metadata.RowCount || rowDifference > codewordRow) { codewords[codewordRow] = null; } else { int checkedRows; if (maxRowHeight > 2) { checkedRows = (maxRowHeight - 2)*rowDifference; } else { checkedRows = rowDifference; } bool closePreviousCodewordFound = checkedRows > codewordRow; for (int i = 1; i <= checkedRows && !closePreviousCodewordFound; i++) { // there must be (height * rowDifference) number of codewords missing. For now we assume height = 1. // This should hopefully get rid of most problems already. closePreviousCodewordFound = codewords[codewordRow - i] != null; } if (closePreviousCodewordFound) { codewords[codewordRow] = null; } else { barcodeRow = codeword.RowNumber; currentRowHeight = 1; } } } return (int) (averageRowHeight + 0.5); }
/// <summary> /// Prune the codewords which do not match the metadata /// TODO Maybe we should keep the incorrect codewords for the start and end positions? /// </summary> /// <param name="codewords">Codewords.</param> /// <param name="metadata">Metadata.</param> private void removeIncorrectCodewords(Codeword[] codewords, BarcodeMetadata metadata) { for (int row = 0; row < codewords.Length; row++) { var codeword = codewords[row]; if (codeword == null) continue; int indicatorValue = codeword.Value%30; int rowNumber = codeword.RowNumber; // Row does not exist in the metadata if (rowNumber >= metadata.RowCount) // different to java rowNumber > metadata.RowCount { codewords[row] = null; // remove this. continue; } if (!IsLeft) { rowNumber += 2; } switch (rowNumber%3) { default: case 0: if (indicatorValue*3 + 1 != metadata.RowCountUpper) { codewords[row] = null; } break; case 1: if (indicatorValue%3 != metadata.RowCountLower || indicatorValue/3 != metadata.ErrorCorrectionLevel) { codewords[row] = null; } break; case 2: if (indicatorValue + 1 != metadata.ColumnCount) { codewords[row] = null; } break; } } }
/// <summary> /// Gets the barcode metadata. /// </summary> /// <returns>The barcode metadata.</returns> public BarcodeMetadata getBarcodeMetadata() { var codewords = Codewords; BarcodeValue barcodeColumnCount = new BarcodeValue(); BarcodeValue barcodeRowCountUpperPart = new BarcodeValue(); BarcodeValue barcodeRowCountLowerPart = new BarcodeValue(); BarcodeValue barcodeECLevel = new BarcodeValue(); foreach (Codeword codeword in codewords) { if (codeword == null) { continue; } codeword.setRowNumberAsRowIndicatorColumn(); int rowIndicatorValue = codeword.Value%30; int codewordRowNumber = codeword.RowNumber; if (!IsLeft) { codewordRowNumber += 2; } switch (codewordRowNumber%3) { case 0: barcodeRowCountUpperPart.setValue(rowIndicatorValue*3 + 1); break; case 1: barcodeECLevel.setValue(rowIndicatorValue/3); barcodeRowCountLowerPart.setValue(rowIndicatorValue%3); break; case 2: barcodeColumnCount.setValue(rowIndicatorValue + 1); break; } } // Maybe we should check if we have ambiguous values? var barcodeColumnCountValues = barcodeColumnCount.getValue(); var barcodeRowCountUpperPartValues = barcodeRowCountUpperPart.getValue(); var barcodeRowCountLowerPartValues = barcodeRowCountLowerPart.getValue(); var barcodeECLevelValues = barcodeECLevel.getValue(); if ((barcodeColumnCountValues.Length == 0) || (barcodeRowCountUpperPartValues.Length == 0) || (barcodeRowCountLowerPartValues.Length == 0) || (barcodeECLevelValues.Length == 0) || barcodeColumnCountValues[0] < 1 || barcodeRowCountUpperPartValues[0] + barcodeRowCountLowerPartValues[0] < PDF417Common.MIN_ROWS_IN_BARCODE || barcodeRowCountUpperPartValues[0] + barcodeRowCountLowerPartValues[0] > PDF417Common.MAX_ROWS_IN_BARCODE) { return null; } var barcodeMetadata = new BarcodeMetadata(barcodeColumnCountValues[0], barcodeRowCountUpperPartValues[0], barcodeRowCountLowerPartValues[0], barcodeECLevelValues[0]); removeIncorrectCodewords(codewords, barcodeMetadata); return barcodeMetadata; }
/// <summary> /// Adjusts the in omplete indicator column row numbers. /// </summary> /// <param name="metadata">Metadata.</param> public int adjustIncompleteIndicatorColumnRowNumbers(BarcodeMetadata metadata) { // TODO maybe we should add missing codewords to store the correct row number to make // finding row numbers for other columns easier // use row height count to make detection of invalid row numbers more reliable ResultPoint top = IsLeft ? Box.TopLeft : Box.TopRight; ResultPoint bottom = IsLeft ? Box.BottomLeft : Box.BottomRight; int firstRow = imageRowToCodewordIndex((int) top.Y); int lastRow = imageRowToCodewordIndex((int) bottom.Y); // We need to be careful using the average row height. // Barcode could be skewed so that we have smaller and taller rows float averageRowHeight = (lastRow - firstRow)/(float) metadata.RowCount; var codewords = Codewords; // initialize loop int barcodeRow = -1; int maxRowHeight = 1; int currentRowHeight = 0; for (int codewordRow = firstRow; codewordRow < lastRow; codewordRow++) { var codeword = codewords[codewordRow]; if (codeword == null) { continue; } codeword.setRowNumberAsRowIndicatorColumn(); int rowDifference = codeword.RowNumber - barcodeRow; // TODO improve handling with case where first row indicator doesn't start with 0 if (rowDifference == 0) { currentRowHeight++; } else if (rowDifference == 1) { maxRowHeight = Math.Max(maxRowHeight, currentRowHeight); currentRowHeight = 1; barcodeRow = codeword.RowNumber; } else if (codeword.RowNumber > metadata.RowCount) { Codewords[codewordRow] = null; } else { barcodeRow = codeword.RowNumber; currentRowHeight = 1; } } return (int) (averageRowHeight + 0.5); }