//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: private static int[] findStartPattern(com.google.zxing.common.BitArray row) throws com.google.zxing.NotFoundException private static int[] findStartPattern(BitArray row) { int width = row.Size; int rowOffset = row.getNextSet(0); 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++) { if (row.get(i) ^ 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; } } // Look for whitespace before start pattern, >= 50% of width of start pattern if (bestMatch >= 0 && row.isRange(Math.Max(0, patternStart - (i - patternStart) / 2), patternStart, false)) { return new int[]{patternStart, i, bestMatch}; } 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; }
/// <summary> /// Records the size of successive runs of white and black pixels in a row, starting at a given point. /// The values are recorded in the given array, and the number of runs recorded is equal to the size /// of the array. If the row starts on a white pixel at the given start point, then the first count /// recorded is the run of white pixels starting from that point; likewise it is the count of a run /// of black pixels if the row begin on a black pixels at that point. /// </summary> /// <param name="row"> row to count from </param> /// <param name="start"> offset into row to start at </param> /// <param name="counters"> array into which to record counts </param> /// <exception cref="NotFoundException"> if counters cannot be filled entirely from row before running out /// of pixels </exception> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: protected static void recordPattern(com.google.zxing.common.BitArray row, int start, int[] counters) throws com.google.zxing.NotFoundException protected internal static void recordPattern(BitArray row, int start, int[] counters) { int numCounters = counters.Length; //Arrays.fill(counters, 0, numCounters, 0); counters.Fill(0); int end = row.Size; if (start >= end) { throw NotFoundException.NotFoundInstance; } bool isWhite = !row.get(start); int counterPosition = 0; int i = start; while (i < end) { if (row.get(i) ^ isWhite) // that is, exactly one is true { counters[counterPosition]++; } else { counterPosition++; if (counterPosition == numCounters) { break; } else { counters[counterPosition] = 1; isWhite = !isWhite; } } i++; } // If we read fully the last section of pixels and filled up our counters -- or filled // the last counter but ran off the side of the image, OK. Otherwise, a problem. if (!(counterPosition == numCounters || (counterPosition == numCounters - 1 && i == end))) { throw NotFoundException.NotFoundInstance; } }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: protected static void recordPatternInReverse(com.google.zxing.common.BitArray row, int start, int[] counters) throws com.google.zxing.NotFoundException protected internal static void recordPatternInReverse(BitArray row, int start, int[] counters) { // This could be more efficient I guess int numTransitionsLeft = counters.Length; bool last = row.get(start); while (start > 0 && numTransitionsLeft >= 0) { if (row.get(--start) != last) { numTransitionsLeft--; last = !last; } } if (numTransitionsLeft >= 0) { throw NotFoundException.NotFoundInstance; } recordPattern(row, start + 1, counters); }
// Embed version information if need be. On success, modify the matrix and return true. // See 8.10 of JISX0510:2004 (p.47) for how to embed version information. //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: static void maybeEmbedVersionInfo(com.google.zxing.qrcode.decoder.Version version, ByteMatrix matrix) throws com.google.zxing.WriterException internal static void maybeEmbedVersionInfo(Version version, ByteMatrix matrix) { if (version.VersionNumber < 7) // Version info is necessary if version >= 7. { return; // Don't need version info. } BitArray versionInfoBits = new BitArray(); makeVersionInfoBits(version, versionInfoBits); int bitIndex = 6 * 3 - 1; // It will decrease from 17 to 0. for (int i = 0; i < 6; ++i) { for (int j = 0; j < 3; ++j) { // Place bits in LSB (least significant bit) to MSB order. bool bit = versionInfoBits.get(bitIndex); bitIndex--; // Left bottom corner. matrix.set(i, matrix.Height - 11 + j, bit); // Right bottom corner. matrix.set(matrix.Height - 11 + j, i, bit); } } }
// Embed type information. On success, modify the matrix. //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: static void embedTypeInfo(com.google.zxing.qrcode.decoder.ErrorCorrectionLevel ecLevel, int maskPattern, ByteMatrix matrix) throws com.google.zxing.WriterException internal static void embedTypeInfo(ErrorCorrectionLevel ecLevel, int maskPattern, ByteMatrix matrix) { BitArray typeInfoBits = new BitArray(); makeTypeInfoBits(ecLevel, maskPattern, typeInfoBits); for (int i = 0; i < typeInfoBits.Size; ++i) { // Place bits in LSB to MSB order. LSB (least significant bit) is the last value in // "typeInfoBits". bool bit = typeInfoBits.get(typeInfoBits.Size - 1 - i); // Type info bits at the left top corner. See 8.9 of JISX0510:2004 (p.46). int x1 = TYPE_INFO_COORDINATES[i][0]; int y1 = TYPE_INFO_COORDINATES[i][1]; matrix.set(x1, y1, bit); if (i < 8) { // Right top corner. int x2 = matrix.Width - i - 1; int y2 = 8; matrix.set(x2, y2, bit); } else { // Left bottom corner. int x2 = 8; int y2 = matrix.Height - 7 + (i - 8); matrix.set(x2, y2, bit); } } }
// Embed "dataBits" using "getMaskPattern". On success, modify the matrix and return true. // For debugging purposes, it skips masking process if "getMaskPattern" is -1. // See 8.7 of JISX0510:2004 (p.38) for how to embed data bits. //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: static void embedDataBits(com.google.zxing.common.BitArray dataBits, int maskPattern, ByteMatrix matrix) throws com.google.zxing.WriterException internal static void embedDataBits(BitArray dataBits, int maskPattern, ByteMatrix matrix) { int bitIndex = 0; int direction = -1; // Start from the right bottom cell. int x = matrix.Width - 1; int y = matrix.Height - 1; while (x > 0) { // Skip the vertical timing pattern. if (x == 6) { x -= 1; } while (y >= 0 && y < matrix.Height) { for (int i = 0; i < 2; ++i) { int xx = x - i; // Skip the cell if it's not empty. if (!isEmpty(matrix.get(xx, y))) { continue; } bool bit; if (bitIndex < dataBits.Size) { bit = dataBits.get(bitIndex); ++bitIndex; } else { // Padding bit. If there is no bit left, we'll fill the left cells with 0, as described // in 8.4.9 of JISX0510:2004 (p. 24). bit = false; } // Skip masking if mask_pattern is -1. if (maskPattern != -1 && MaskUtil.getDataMaskBit(maskPattern, xx, y)) { bit = !bit; } matrix.set(xx, y, bit); } y += direction; } direction = -direction; // Reverse the direction. y += direction; x -= 2; // Move to the left. } // All bits should be consumed. if (bitIndex != dataBits.Size) { throw new WriterException("Not all bits consumed: " + bitIndex + '/' + dataBits.Size); } }
/// <summary> /// The start & end patterns must be pre/post fixed by a quiet zone. This /// zone must be at least 10 times the width of a narrow line. Scan back until /// we either get to the start of the barcode or match the necessary number of /// quiet zone pixels. /// /// Note: Its assumed the row is reversed when using this method to find /// quiet zone after the end pattern. /// /// ref: http://www.barcode-1.net/i25code.html /// </summary> /// <param name="row"> bit array representing the scanned barcode. </param> /// <param name="startPattern"> index into row of the start or end pattern. </param> /// <exception cref="NotFoundException"> if the quiet zone cannot be found, a ReaderException is thrown. </exception> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: private void validateQuietZone(com.google.zxing.common.BitArray row, int startPattern) throws com.google.zxing.NotFoundException private void validateQuietZone(BitArray row, int startPattern) { int quietCount = this.narrowLineWidth * 10; // expect to find this many pixels of quiet zone for (int i = startPattern - 1; quietCount > 0 && i >= 0; i--) { if (row.get(i)) { break; } quietCount--; } if (quietCount != 0) { // Unable to find the necessary number of quiet zone pixels. throw NotFoundException.NotFoundInstance; } }
/// <param name="row"> row of black/white values to search </param> /// <param name="rowOffset"> position to start search </param> /// <param name="pattern"> pattern of counts of number of black and white pixels that are /// being searched for as a pattern </param> /// <returns> start/end horizontal offset of guard pattern, as an array of two /// ints </returns> /// <exception cref="NotFoundException"> if pattern is not found </exception> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: private static int[] findGuardPattern(com.google.zxing.common.BitArray row, int rowOffset, int[] pattern) throws com.google.zxing.NotFoundException private static int[] findGuardPattern(BitArray row, int rowOffset, int[] pattern) { // TODO: This is very similar to implementation in UPCEANReader. Consider if they can be // merged to a single method. int patternLength = pattern.Length; int[] counters = new int[patternLength]; int width = row.Size; bool isWhite = false; int counterPosition = 0; int patternStart = rowOffset; for (int x = rowOffset; x < width; x++) { if (row.get(x) ^ isWhite) { counters[counterPosition]++; } else { if (counterPosition == patternLength - 1) { if (patternMatchVariance(counters, pattern, MAX_INDIVIDUAL_VARIANCE) < MAX_AVG_VARIANCE) { return new int[]{patternStart, x}; } 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; }