public Result decodeRow(int rowNumber, BitArray row, int[] startGuardRange) { StringBuilder result = decodeRowStringBuffer; result.Length = 0; int endStart = decodeMiddle(row, startGuardRange, result); int[] endRange = decodeEnd(row, endStart); // Make sure there is a quiet zone at least as big as the end pattern after the barcode. The // spec might want more whitespace, but in practice this is the maximum we can count on. int end = endRange[1]; int quietEnd = end + (end - endRange[0]); if (quietEnd >= row.getSize() || !row.isRange(end, quietEnd, false)) { throw new ReaderException(); } String resultString = result.ToString(); if (!checkChecksum(resultString)) { throw new ReaderException(); } float left = (float) (startGuardRange[1] + startGuardRange[0]) / 2.0f; float right = (float) (endRange[1] + endRange[0]) / 2.0f; return new Result(resultString, null, // no natural byte representation for these barcodes new ResultPoint[]{ new GenericResultPoint(left, (float) rowNumber), new GenericResultPoint(right, (float) rowNumber)}, getBarcodeFormat()); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: private static int[] findAsteriskPattern(com.google.zxing.common.BitArray row, int[] counters) throws com.google.zxing.NotFoundException private static int[] findAsteriskPattern(BitArray row, int[] counters) { int width = row.Size; int rowOffset = row.getNextSet(0); int counterPosition = 0; int patternStart = rowOffset; bool isWhite = false; int patternLength = counters.Length; for (int i = rowOffset; i < width; i++) { if (row.get(i) ^ isWhite) { counters[counterPosition]++; } else { if (counterPosition == patternLength - 1) { // Look for whitespace before start pattern, >= 50% of width of start pattern if (toNarrowWidePattern(counters) == ASTERISK_ENCODING && row.isRange(Math.Max(0, patternStart - ((i - patternStart) >> 1)), patternStart, false)) { return new int[]{patternStart, i}; } patternStart += counters[0] + counters[1]; Array.Copy(counters, 2, counters, 0, patternLength - 2); counters[patternLength - 2] = 0; counters[patternLength - 1] = 0; counterPosition--; } else { counterPosition++; } counters[counterPosition] = 1; isWhite = !isWhite; } } throw NotFoundException.NotFoundInstance; }
public static int[] findStartGuardPattern(BitArray row) { bool foundStart = false; int[] startRange = null; int nextStart = 0; while (!foundStart) { startRange = findGuardPattern(row, nextStart, false, START_END_PATTERN); int start = startRange[0]; nextStart = startRange[1]; // Make sure there is a quiet zone at least as big as the start pattern before the barcode. If // this check would run off the left edge of the image, do not accept this barcode, as it is // very likely to be a false positive. int quietStart = start - (nextStart - start); if (quietStart >= 0) { foundStart = row.isRange(quietStart, start, false); } } return startRange; }
public override Result decodeRow(int rowNumber, BitArray row, System.Collections.Hashtable hints) { int[] startPatternInfo = findStartPattern(row); int startCode = startPatternInfo[2]; int codeSet; switch (startCode) { case CODE_START_A: codeSet = CODE_CODE_A; break; case CODE_START_B: codeSet = CODE_CODE_B; break; case CODE_START_C: codeSet = CODE_CODE_C; break; default: throw new ReaderException(); } bool done = false; bool isNextShifted = false; StringBuilder result = new StringBuilder(); int lastStart = startPatternInfo[0]; int nextStart = startPatternInfo[1]; int[] counters = new int[6]; int lastCode = 0; int code = 0; int checksumTotal = startCode; int multiplier = 0; bool lastCharacterWasPrintable = true; while (!done) { bool unshift = isNextShifted; isNextShifted = false; // Save off last code lastCode = code; // Decode another code from image code = decodeCode(row, counters, nextStart); // Remember whether the last code was printable or not (excluding CODE_STOP) if (code != CODE_STOP) { lastCharacterWasPrintable = true; } // Add to checksum computation (if not CODE_STOP of course) if (code != CODE_STOP) { multiplier++; checksumTotal += multiplier * code; } // Advance to where the next code will to start lastStart = nextStart; for (int i = 0; i < counters.Length; i++) { nextStart += counters[i]; } // Take care of illegal start codes switch (code) { case CODE_START_A: case CODE_START_B: case CODE_START_C: throw new ReaderException(); } switch (codeSet) { case CODE_CODE_A: if (code < 64) { result.Append((char) (' ' + code)); } else if (code < 96) { result.Append((char) (code - 64)); } else { // Don't let CODE_STOP, which always appears, affect whether whether we think the last code // was printable or not if (code != CODE_STOP) { lastCharacterWasPrintable = false; } switch (code) { case CODE_FNC_1: case CODE_FNC_2: case CODE_FNC_3: case CODE_FNC_4_A: // do nothing? break; case CODE_SHIFT: isNextShifted = true; codeSet = CODE_CODE_B; break; case CODE_CODE_B: codeSet = CODE_CODE_B; break; case CODE_CODE_C: codeSet = CODE_CODE_C; break; case CODE_STOP: done = true; break; } } break; case CODE_CODE_B: if (code < 96) { result.Append((char) (' ' + code)); } else { if (code != CODE_STOP) { lastCharacterWasPrintable = false; } switch (code) { case CODE_FNC_1: case CODE_FNC_2: case CODE_FNC_3: case CODE_FNC_4_B: // do nothing? break; case CODE_SHIFT: isNextShifted = true; codeSet = CODE_CODE_C; break; case CODE_CODE_A: codeSet = CODE_CODE_A; break; case CODE_CODE_C: codeSet = CODE_CODE_C; break; case CODE_STOP: done = true; break; } } break; case CODE_CODE_C: if (code < 100) { if (code < 10) { result.Append('0'); } result.Append(code); } else { if (code != CODE_STOP) { lastCharacterWasPrintable = false; } switch (code) { case CODE_FNC_1: // do nothing? break; case CODE_CODE_A: codeSet = CODE_CODE_A; break; case CODE_CODE_B: codeSet = CODE_CODE_B; break; case CODE_STOP: done = true; break; } } break; } // Unshift back to another code set if we were shifted if (unshift) { switch (codeSet) { case CODE_CODE_A: codeSet = CODE_CODE_C; break; case CODE_CODE_B: codeSet = CODE_CODE_A; break; case CODE_CODE_C: codeSet = CODE_CODE_B; break; } } } // Check for ample whitespice following pattern, but, to do this we first need to remember that we // fudged decoding CODE_STOP since it actually has 7 bars, not 6. There is a black bar left to read off. // Would be slightly better to properly read. Here we just skip it: while (row.get(nextStart)) { nextStart++; } if (!row.isRange(nextStart, Math.Min(row.getSize(), nextStart + (nextStart - lastStart) / 2), false)) { throw new ReaderException(); } // Pull out from sum the value of the penultimate check code checksumTotal -= multiplier * lastCode; // lastCode is the checksum then: if (checksumTotal % 103 != lastCode) { throw new ReaderException(); } // Need to pull out the check digits from string int resultLength = result.Length; // Only bother if, well, the result had at least one character, and if the checksum digit happened // to be a printable character. If it was just interpreted as a control code, nothing to remove if (resultLength > 0 && lastCharacterWasPrintable) { if (codeSet == CODE_CODE_C) { result.Remove(resultLength - 2, 2); } else { result.Remove(resultLength - 1, 1); } } String resultString = result.ToString(); if (resultString.Length == 0) { // Almost surely a false positive throw new ReaderException(); } float left = (float) (startPatternInfo[1] + startPatternInfo[0]) / 2.0f; float right = (float) (nextStart + lastStart) / 2.0f; return new Result( resultString, null, new ResultPoint[]{ new GenericResultPoint(left, (float) rowNumber), new GenericResultPoint(right, (float) rowNumber)}, BarcodeFormat.CODE_128); }
private static int[] findStartPattern(BitArray row) { int width = row.getSize(); int rowOffset = 0; while (rowOffset < width) { if (row.get(rowOffset)) { break; } rowOffset++; } int counterPosition = 0; int[] counters = new int[6]; int patternStart = rowOffset; bool isWhite = false; int patternLength = counters.Length; for (int i = rowOffset; i < width; i++) { bool pixel = row.get(i); if ((!pixel && isWhite) || (pixel && !isWhite)) { counters[counterPosition]++; } else { if (counterPosition == patternLength - 1) { int bestVariance = MAX_AVG_VARIANCE; int bestMatch = -1; for (int startCode = CODE_START_A; startCode <= CODE_START_C; startCode++) { int variance = patternMatchVariance(counters, CODE_PATTERNS[startCode], MAX_INDIVIDUAL_VARIANCE); if (variance < bestVariance) { bestVariance = variance; bestMatch = startCode; } } if (bestMatch >= 0) { // Look for whitespace before start pattern, >= 50% of width of start pattern if (row.isRange(Math.Max(0, patternStart - (i - patternStart) / 2), patternStart, false)) { return new int[]{patternStart, i, bestMatch}; } } patternStart += counters[0] + counters[1]; for (int y = 2; y < patternLength; y++) { counters[y - 2] = counters[y]; } counters[patternLength - 2] = 0; counters[patternLength - 1] = 0; counterPosition--; } else { counterPosition++; } counters[counterPosition] = 1; isWhite = !isWhite; } } throw new ReaderException(); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: static int[] findStartGuardPattern(com.google.zxing.common.BitArray row) throws com.google.zxing.NotFoundException internal static int[] findStartGuardPattern(BitArray row) { bool foundStart = false; int[] startRange = null; int nextStart = 0; int[] counters = new int[START_END_PATTERN.Length]; while (!foundStart) { //Arrays.fill(counters, 0, START_END_PATTERN.Length, 0); counters.Fill(0); startRange = findGuardPattern(row, nextStart, false, START_END_PATTERN, counters); int start = startRange[0]; nextStart = startRange[1]; // Make sure there is a quiet zone at least as big as the start pattern before the barcode. // If this check would run off the left edge of the image, do not accept this barcode, // as it is very likely to be a false positive. int quietStart = start - (nextStart - start); if (quietStart >= 0) { foundStart = row.isRange(quietStart, start, false); } } return startRange; }
/// <summary> /// <p>Like <seealso cref="#decodeRow(int, BitArray, java.util.Map)"/>, but /// allows caller to inform method about where the UPC/EAN start pattern is /// found. This allows this to be computed once and reused across many implementations.</p> /// </summary> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public com.google.zxing.Result decodeRow(int rowNumber, com.google.zxing.common.BitArray row, int[] startGuardRange, java.util.Map<com.google.zxing.DecodeHintType,?> hints) throws com.google.zxing.NotFoundException, com.google.zxing.ChecksumException, com.google.zxing.FormatException public virtual Result decodeRow(int rowNumber, BitArray row, int[] startGuardRange, IDictionary<DecodeHintType, object> hints) { //ResultPointCallback resultPointCallback = hints == null ? null : (ResultPointCallback) hints[DecodeHintType.NEED_RESULT_POINT_CALLBACK]; ResultPointCallback resultPointCallback = null; if (hints != null && hints.ContainsKey(DecodeHintType.NEED_RESULT_POINT_CALLBACK)) { resultPointCallback = (ResultPointCallback)hints[DecodeHintType.NEED_RESULT_POINT_CALLBACK]; } if (resultPointCallback != null) { resultPointCallback.foundPossibleResultPoint(new ResultPoint((startGuardRange[0] + startGuardRange[1]) / 2.0f, rowNumber)); } StringBuilder result = decodeRowStringBuffer; result.Length = 0; int endStart = decodeMiddle(row, startGuardRange, result); if (resultPointCallback != null) { resultPointCallback.foundPossibleResultPoint(new ResultPoint(endStart, rowNumber)); } int[] endRange = decodeEnd(row, endStart); if (resultPointCallback != null) { resultPointCallback.foundPossibleResultPoint(new ResultPoint((endRange[0] + endRange[1]) / 2.0f, rowNumber)); } // Make sure there is a quiet zone at least as big as the end pattern after the barcode. The // spec might want more whitespace, but in practice this is the maximum we can count on. int end = endRange[1]; int quietEnd = end + (end - endRange[0]); if (quietEnd >= row.Size || !row.isRange(end, quietEnd, false)) { throw NotFoundException.NotFoundInstance; } string resultString = result.ToString(); if (!checkChecksum(resultString)) { throw ChecksumException.ChecksumInstance; } float left = (float)(startGuardRange[1] + startGuardRange[0]) / 2.0f; float right = (float)(endRange[1] + endRange[0]) / 2.0f; BarcodeFormat format = BarcodeFormat; Result decodeResult = new Result(resultString, null, new ResultPoint[]{new ResultPoint(left, (float) rowNumber), new ResultPoint(right, (float) rowNumber)}, format); // no natural byte representation for these barcodes try { Result extensionResult = extensionReader.decodeRow(rowNumber, row, endRange[1]); decodeResult.putMetadata(ResultMetadataType.UPC_EAN_EXTENSION, extensionResult.Text); decodeResult.putAllMetadata(extensionResult.ResultMetadata); decodeResult.addResultPoints(extensionResult.ResultPoints); } catch (ReaderException re) { // continue } if (format == BarcodeFormat.EAN_13 || format == BarcodeFormat.UPC_A) { string countryID = eanManSupport.lookupCountryIdentifier(resultString); if (countryID != null) { decodeResult.putMetadata(ResultMetadataType.POSSIBLE_COUNTRY, countryID); } } return decodeResult; }
private static int[] findAsteriskPattern(BitArray row) { int width = row.getSize(); int rowOffset = 0; while (rowOffset < width) { if (row.get(rowOffset)) { break; } rowOffset++; } int counterPosition = 0; int[] counters = new int[9]; int patternStart = rowOffset; bool isWhite = false; int patternLength = counters.Length; for (int i = rowOffset; i < width; i++) { bool pixel = row.get(i); if ((!pixel && isWhite) || (pixel && !isWhite)) { counters[counterPosition]++; } else { if (counterPosition == patternLength - 1) { try { if (toNarrowWidePattern(counters) == ASTERISK_ENCODING) { // Look for whitespace before start pattern, >= 50% of width of start pattern if (row.isRange(Math.Max(0, patternStart - (i - patternStart) / 2), patternStart, false)) { return new int[]{patternStart, i}; } } } catch (ReaderException re) { // no match, continue } patternStart += counters[0] + counters[1]; for (int y = 2; y < patternLength; y++) { counters[y - 2] = counters[y]; } counters[patternLength - 2] = 0; counters[patternLength - 1] = 0; counterPosition--; } else { counterPosition++; } counters[counterPosition] = 1; isWhite = !isWhite; } } throw new ReaderException(); }