Java Code Examples for com.google.zxing.common.BitArray#getNextUnset()

The following examples show how to use com.google.zxing.common.BitArray#getNextUnset() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CodaBarReader.java    From ZXing-Orient with Apache License 2.0 6 votes vote down vote up
/**
 * Records the size of all runs of white and black pixels, starting with white.
 * This is just like recordPattern, except it records all the counters, and
 * uses our builtin "counters" member for storage.
 * @param row row to count from
 */
private void setCounters(BitArray row) throws NotFoundException {
  counterLength = 0;
  // Start from the first white bit.
  int i = row.getNextUnset(0);
  int end = row.getSize();
  if (i >= end) {
    throw NotFoundException.getNotFoundInstance();
  }
  boolean isWhite = true;
  int count = 0;
  while (i < end) {
    if (row.get(i) ^ isWhite) { // that is, exactly one is true
      count++;
    } else {
      counterAppend(count);
      count = 1;
      isWhite = !isWhite;
    }
    i++;
  }
  counterAppend(count);
}
 
Example 2
Source File: CodaBarReader.java    From Tesseract-OCR-Scanner with Apache License 2.0 6 votes vote down vote up
/**
 * Records the size of all runs of white and black pixels, starting with white.
 * This is just like recordPattern, except it records all the counters, and
 * uses our builtin "counters" member for storage.
 * @param row row to count from
 */
private void setCounters(BitArray row) throws NotFoundException {
  counterLength = 0;
  // Start from the first white bit.
  int i = row.getNextUnset(0);
  int end = row.getSize();
  if (i >= end) {
    throw NotFoundException.getNotFoundInstance();
  }
  boolean isWhite = true;
  int count = 0;
  while (i < end) {
    if (row.get(i) != isWhite) {
      count++;
    } else {
      counterAppend(count);
      count = 1;
      isWhite = !isWhite;
    }
    i++;
  }
  counterAppend(count);
}
 
Example 3
Source File: UPCEANExtension2Support.java    From Tesseract-OCR-Scanner with Apache License 2.0 5 votes vote down vote up
private int decodeMiddle(BitArray row, int[] startRange, StringBuilder resultString) throws NotFoundException {
  int[] counters = decodeMiddleCounters;
  counters[0] = 0;
  counters[1] = 0;
  counters[2] = 0;
  counters[3] = 0;
  int end = row.getSize();
  int rowOffset = startRange[1];

  int checkParity = 0;

  for (int x = 0; x < 2 && rowOffset < end; x++) {
    int bestMatch = UPCEANReader.decodeDigit(row, counters, rowOffset, UPCEANReader.L_AND_G_PATTERNS);
    resultString.append((char) ('0' + bestMatch % 10));
    for (int counter : counters) {
      rowOffset += counter;
    }
    if (bestMatch >= 10) {
      checkParity |= 1 << (1 - x);
    }
    if (x != 1) {
      // Read off separator if not last
      rowOffset = row.getNextSet(rowOffset);
      rowOffset = row.getNextUnset(rowOffset);
    }
  }

  if (resultString.length() != 2) {
    throw NotFoundException.getNotFoundInstance();
  }

  if (Integer.parseInt(resultString.toString()) % 4 != checkParity) {
    throw NotFoundException.getNotFoundInstance();
  }

  return rowOffset;
}
 
Example 4
Source File: UPCEANExtension5Support.java    From ZXing-Orient with Apache License 2.0 5 votes vote down vote up
int decodeMiddle(BitArray row, int[] startRange, StringBuilder resultString) throws NotFoundException {
  int[] counters = decodeMiddleCounters;
  counters[0] = 0;
  counters[1] = 0;
  counters[2] = 0;
  counters[3] = 0;
  int end = row.getSize();
  int rowOffset = startRange[1];

  int lgPatternFound = 0;

  for (int x = 0; x < 5 && rowOffset < end; x++) {
    int bestMatch = UPCEANReader.decodeDigit(row, counters, rowOffset, UPCEANReader.L_AND_G_PATTERNS);
    resultString.append((char) ('0' + bestMatch % 10));
    for (int counter : counters) {
      rowOffset += counter;
    }
    if (bestMatch >= 10) {
      lgPatternFound |= 1 << (4 - x);
    }
    if (x != 4) {
      // Read off separator if not last
      rowOffset = row.getNextSet(rowOffset);
      rowOffset = row.getNextUnset(rowOffset);
    }
  }

  if (resultString.length() != 5) {
    throw NotFoundException.getNotFoundInstance();
  }

  int checkDigit = determineCheckDigit(lgPatternFound);
  if (extensionChecksum(resultString.toString()) != checkDigit) {
    throw NotFoundException.getNotFoundInstance();
  }
  
  return rowOffset;
}
 
Example 5
Source File: UPCEANReader.java    From barcodescanner-lib-aar with MIT License 5 votes vote down vote up
/**
 * @param row row of black/white values to search
 * @param rowOffset position to start search
 * @param whiteFirst if true, indicates that the pattern specifies white/black/white/...
 * pixel counts, otherwise, it is interpreted as black/white/black/...
 * @param pattern pattern of counts of number of black and white pixels that are being
 * searched for as a pattern
 * @param counters array of counters, as long as pattern, to re-use
 * @return start/end horizontal offset of guard pattern, as an array of two ints
 * @throws NotFoundException if pattern is not found
 */
private static int[] findGuardPattern(BitArray row,
                                      int rowOffset,
                                      boolean whiteFirst,
                                      int[] pattern,
                                      int[] counters) throws NotFoundException {
  int width = row.getSize();
  rowOffset = whiteFirst ? row.getNextUnset(rowOffset) : row.getNextSet(rowOffset);
  int counterPosition = 0;
  int patternStart = rowOffset;
  int patternLength = pattern.length;
  boolean isWhite = whiteFirst;
  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];
        System.arraycopy(counters, 2, counters, 0, patternLength - 2);
        counters[patternLength - 2] = 0;
        counters[patternLength - 1] = 0;
        counterPosition--;
      } else {
        counterPosition++;
      }
      counters[counterPosition] = 1;
      isWhite = !isWhite;
    }
  }
  throw NotFoundException.getNotFoundInstance();
}
 
Example 6
Source File: UPCEANExtension2Support.java    From QrCodeScanner with GNU General Public License v3.0 5 votes vote down vote up
private int decodeMiddle(BitArray row, int[] startRange, StringBuilder resultString) throws NotFoundException {
  int[] counters = decodeMiddleCounters;
  counters[0] = 0;
  counters[1] = 0;
  counters[2] = 0;
  counters[3] = 0;
  int end = row.getSize();
  int rowOffset = startRange[1];

  int checkParity = 0;

  for (int x = 0; x < 2 && rowOffset < end; x++) {
    int bestMatch = UPCEANReader.decodeDigit(row, counters, rowOffset, UPCEANReader.L_AND_G_PATTERNS);
    resultString.append((char) ('0' + bestMatch % 10));
    for (int counter : counters) {
      rowOffset += counter;
    }
    if (bestMatch >= 10) {
      checkParity |= 1 << (1 - x);
    }
    if (x != 1) {
      // Read off separator if not last
      rowOffset = row.getNextSet(rowOffset);
      rowOffset = row.getNextUnset(rowOffset);
    }
  }

  if (resultString.length() != 2) {
    throw NotFoundException.getNotFoundInstance();
  }

  if (Integer.parseInt(resultString.toString()) % 4 != checkParity) {
    throw NotFoundException.getNotFoundInstance();
  }

  return rowOffset;
}
 
Example 7
Source File: UPCEANExtension2Support.java    From reacteu-app with MIT License 5 votes vote down vote up
int decodeMiddle(BitArray row, int[] startRange, StringBuilder resultString) throws NotFoundException {
  int[] counters = decodeMiddleCounters;
  counters[0] = 0;
  counters[1] = 0;
  counters[2] = 0;
  counters[3] = 0;
  int end = row.getSize();
  int rowOffset = startRange[1];

  int checkParity = 0;

  for (int x = 0; x < 2 && rowOffset < end; x++) {
    int bestMatch = UPCEANReader.decodeDigit(row, counters, rowOffset, UPCEANReader.L_AND_G_PATTERNS);
    resultString.append((char) ('0' + bestMatch % 10));
    for (int counter : counters) {
      rowOffset += counter;
    }
    if (bestMatch >= 10) {
      checkParity |= 1 << (1 - x);
    }
    if (x != 1) {
      // Read off separator if not last
      rowOffset = row.getNextSet(rowOffset);
      rowOffset = row.getNextUnset(rowOffset);
    }
  }

  if (resultString.length() != 2) {
    throw NotFoundException.getNotFoundInstance();
  }

  if (Integer.parseInt(resultString.toString()) % 4 != checkParity) {
    throw NotFoundException.getNotFoundInstance();
  }
  
  return rowOffset;
}
 
Example 8
Source File: UPCEANExtension5Support.java    From weex with Apache License 2.0 5 votes vote down vote up
int decodeMiddle(BitArray row, int[] startRange, StringBuilder resultString) throws NotFoundException {
  int[] counters = decodeMiddleCounters;
  counters[0] = 0;
  counters[1] = 0;
  counters[2] = 0;
  counters[3] = 0;
  int end = row.getSize();
  int rowOffset = startRange[1];

  int lgPatternFound = 0;

  for (int x = 0; x < 5 && rowOffset < end; x++) {
    int bestMatch = UPCEANReader.decodeDigit(row, counters, rowOffset, UPCEANReader.L_AND_G_PATTERNS);
    resultString.append((char) ('0' + bestMatch % 10));
    for (int counter : counters) {
      rowOffset += counter;
    }
    if (bestMatch >= 10) {
      lgPatternFound |= 1 << (4 - x);
    }
    if (x != 4) {
      // Read off separator if not last
      rowOffset = row.getNextSet(rowOffset);
      rowOffset = row.getNextUnset(rowOffset);
    }
  }

  if (resultString.length() != 5) {
    throw NotFoundException.getNotFoundInstance();
  }

  int checkDigit = determineCheckDigit(lgPatternFound);
  if (extensionChecksum(resultString.toString()) != checkDigit) {
    throw NotFoundException.getNotFoundInstance();
  }
  
  return rowOffset;
}
 
Example 9
Source File: UPCEANExtension5Support.java    From QrCodeScanner with GNU General Public License v3.0 5 votes vote down vote up
private int decodeMiddle(BitArray row, int[] startRange, StringBuilder resultString) throws NotFoundException {
  int[] counters = decodeMiddleCounters;
  counters[0] = 0;
  counters[1] = 0;
  counters[2] = 0;
  counters[3] = 0;
  int end = row.getSize();
  int rowOffset = startRange[1];

  int lgPatternFound = 0;

  for (int x = 0; x < 5 && rowOffset < end; x++) {
    int bestMatch = UPCEANReader.decodeDigit(row, counters, rowOffset, UPCEANReader.L_AND_G_PATTERNS);
    resultString.append((char) ('0' + bestMatch % 10));
    for (int counter : counters) {
      rowOffset += counter;
    }
    if (bestMatch >= 10) {
      lgPatternFound |= 1 << (4 - x);
    }
    if (x != 4) {
      // Read off separator if not last
      rowOffset = row.getNextSet(rowOffset);
      rowOffset = row.getNextUnset(rowOffset);
    }
  }

  if (resultString.length() != 5) {
    throw NotFoundException.getNotFoundInstance();
  }

  int checkDigit = determineCheckDigit(lgPatternFound);
  if (extensionChecksum(resultString.toString()) != checkDigit) {
    throw NotFoundException.getNotFoundInstance();
  }

  return rowOffset;
}
 
Example 10
Source File: RSSExpandedReader.java    From reacteu-app with MIT License 5 votes vote down vote up
private static int getNextSecondBar(BitArray row, int initialPos){
  int currentPos;
  if (row.get(initialPos)) {
    currentPos = row.getNextUnset(initialPos);
    currentPos = row.getNextSet(currentPos);
  } else {
    currentPos = row.getNextSet(initialPos);
    currentPos = row.getNextUnset(currentPos);
  }
  return currentPos;
}
 
Example 11
Source File: UPCEANExtension2Support.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
int decodeMiddle(BitArray row, int[] startRange, StringBuilder resultString) throws NotFoundException {
	int[] counters = decodeMiddleCounters;
	counters[0] = 0;
	counters[1] = 0;
	counters[2] = 0;
	counters[3] = 0;
	int end = row.getSize();
	int rowOffset = startRange[1];

	int checkParity = 0;

	for (int x = 0; x < 2 && rowOffset < end; x++) {
		int bestMatch = UPCEANReader.decodeDigit(row, counters, rowOffset, UPCEANReader.L_AND_G_PATTERNS);
		resultString.append((char) ('0' + bestMatch % 10));
		for (int counter : counters) {
			rowOffset += counter;
		}
		if (bestMatch >= 10) {
			checkParity |= 1 << (1 - x);
		}
		if (x != 1) {
			// Read off separator if not last
			rowOffset = row.getNextSet(rowOffset);
			rowOffset = row.getNextUnset(rowOffset);
		}
	}

	if (resultString.length() != 2) {
		throw NotFoundException.getNotFoundInstance();
	}

	if (Integer.parseInt(resultString.toString()) % 4 != checkParity) {
		throw NotFoundException.getNotFoundInstance();
	}

	return rowOffset;
}
 
Example 12
Source File: UPCEANExtension2Support.java    From barcodescanner-lib-aar with MIT License 5 votes vote down vote up
private int decodeMiddle(BitArray row, int[] startRange, StringBuilder resultString) throws NotFoundException {
  int[] counters = decodeMiddleCounters;
  counters[0] = 0;
  counters[1] = 0;
  counters[2] = 0;
  counters[3] = 0;
  int end = row.getSize();
  int rowOffset = startRange[1];

  int checkParity = 0;

  for (int x = 0; x < 2 && rowOffset < end; x++) {
    int bestMatch = UPCEANReader.decodeDigit(row, counters, rowOffset, UPCEANReader.L_AND_G_PATTERNS);
    resultString.append((char) ('0' + bestMatch % 10));
    for (int counter : counters) {
      rowOffset += counter;
    }
    if (bestMatch >= 10) {
      checkParity |= 1 << (1 - x);
    }
    if (x != 1) {
      // Read off separator if not last
      rowOffset = row.getNextSet(rowOffset);
      rowOffset = row.getNextUnset(rowOffset);
    }
  }

  if (resultString.length() != 2) {
    throw NotFoundException.getNotFoundInstance();
  }

  if (Integer.parseInt(resultString.toString()) % 4 != checkParity) {
    throw NotFoundException.getNotFoundInstance();
  }
  
  return rowOffset;
}
 
Example 13
Source File: RSSExpandedReader.java    From Tesseract-OCR-Scanner with Apache License 2.0 5 votes vote down vote up
private static int getNextSecondBar(BitArray row, int initialPos) {
  int currentPos;
  if (row.get(initialPos)) {
    currentPos = row.getNextUnset(initialPos);
    currentPos = row.getNextSet(currentPos);
  } else {
    currentPos = row.getNextSet(initialPos);
    currentPos = row.getNextUnset(currentPos);
  }
  return currentPos;
}
 
Example 14
Source File: UPCEANExtension5Support.java    From Tesseract-OCR-Scanner with Apache License 2.0 5 votes vote down vote up
private int decodeMiddle(BitArray row, int[] startRange, StringBuilder resultString) throws NotFoundException {
  int[] counters = decodeMiddleCounters;
  counters[0] = 0;
  counters[1] = 0;
  counters[2] = 0;
  counters[3] = 0;
  int end = row.getSize();
  int rowOffset = startRange[1];

  int lgPatternFound = 0;

  for (int x = 0; x < 5 && rowOffset < end; x++) {
    int bestMatch = UPCEANReader.decodeDigit(row, counters, rowOffset, UPCEANReader.L_AND_G_PATTERNS);
    resultString.append((char) ('0' + bestMatch % 10));
    for (int counter : counters) {
      rowOffset += counter;
    }
    if (bestMatch >= 10) {
      lgPatternFound |= 1 << (4 - x);
    }
    if (x != 4) {
      // Read off separator if not last
      rowOffset = row.getNextSet(rowOffset);
      rowOffset = row.getNextUnset(rowOffset);
    }
  }

  if (resultString.length() != 5) {
    throw NotFoundException.getNotFoundInstance();
  }

  int checkDigit = determineCheckDigit(lgPatternFound);
  if (extensionChecksum(resultString.toString()) != checkDigit) {
    throw NotFoundException.getNotFoundInstance();
  }

  return rowOffset;
}
 
Example 15
Source File: UPCEANReader.java    From ScreenCapture with MIT License 5 votes vote down vote up
/**
 * @param row row of black/white values to search
 * @param rowOffset position to start search
 * @param whiteFirst if true, indicates that the pattern specifies white/black/white/...
 * pixel counts, otherwise, it is interpreted as black/white/black/...
 * @param pattern pattern of counts of number of black and white pixels that are being
 * searched for as a pattern
 * @param counters array of counters, as long as pattern, to re-use
 * @return start/end horizontal offset of guard pattern, as an array of two ints
 * @throws NotFoundException if pattern is not found
 */
private static int[] findGuardPattern(BitArray row,
                                      int rowOffset,
                                      boolean whiteFirst,
                                      int[] pattern,
                                      int[] counters) throws NotFoundException {
  int width = row.getSize();
  rowOffset = whiteFirst ? row.getNextUnset(rowOffset) : row.getNextSet(rowOffset);
  int counterPosition = 0;
  int patternStart = rowOffset;
  int patternLength = pattern.length;
  boolean isWhite = whiteFirst;
  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];
        System.arraycopy(counters, 2, counters, 0, counterPosition - 1);
        counters[counterPosition - 1] = 0;
        counters[counterPosition] = 0;
        counterPosition--;
      } else {
        counterPosition++;
      }
      counters[counterPosition] = 1;
      isWhite = !isWhite;
    }
  }
  throw NotFoundException.getNotFoundInstance();
}
 
Example 16
Source File: UPCEANReader.java    From ZXing-Orient with Apache License 2.0 5 votes vote down vote up
/**
 * @param row row of black/white values to search
 * @param rowOffset position to start search
 * @param whiteFirst if true, indicates that the pattern specifies white/black/white/...
 * pixel counts, otherwise, it is interpreted as black/white/black/...
 * @param pattern pattern of counts of number of black and white pixels that are being
 * searched for as a pattern
 * @param counters array of counters, as long as pattern, to re-use
 * @return start/end horizontal offset of guard pattern, as an array of two ints
 * @throws NotFoundException if pattern is not found
 */
private static int[] findGuardPattern(BitArray row,
                                      int rowOffset,
                                      boolean whiteFirst,
                                      int[] pattern,
                                      int[] counters) throws NotFoundException {
  int patternLength = pattern.length;
  int width = row.getSize();
  boolean isWhite = whiteFirst;
  rowOffset = whiteFirst ? row.getNextUnset(rowOffset) : row.getNextSet(rowOffset);
  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];
        System.arraycopy(counters, 2, counters, 0, patternLength - 2);
        counters[patternLength - 2] = 0;
        counters[patternLength - 1] = 0;
        counterPosition--;
      } else {
        counterPosition++;
      }
      counters[counterPosition] = 1;
      isWhite = !isWhite;
    }
  }
  throw NotFoundException.getNotFoundInstance();
}
 
Example 17
Source File: UPCEANExtension5Support.java    From barcodescanner-lib-aar with MIT License 5 votes vote down vote up
private int decodeMiddle(BitArray row, int[] startRange, StringBuilder resultString) throws NotFoundException {
  int[] counters = decodeMiddleCounters;
  counters[0] = 0;
  counters[1] = 0;
  counters[2] = 0;
  counters[3] = 0;
  int end = row.getSize();
  int rowOffset = startRange[1];

  int lgPatternFound = 0;

  for (int x = 0; x < 5 && rowOffset < end; x++) {
    int bestMatch = UPCEANReader.decodeDigit(row, counters, rowOffset, UPCEANReader.L_AND_G_PATTERNS);
    resultString.append((char) ('0' + bestMatch % 10));
    for (int counter : counters) {
      rowOffset += counter;
    }
    if (bestMatch >= 10) {
      lgPatternFound |= 1 << (4 - x);
    }
    if (x != 4) {
      // Read off separator if not last
      rowOffset = row.getNextSet(rowOffset);
      rowOffset = row.getNextUnset(rowOffset);
    }
  }

  if (resultString.length() != 5) {
    throw NotFoundException.getNotFoundInstance();
  }

  int checkDigit = determineCheckDigit(lgPatternFound);
  if (extensionChecksum(resultString.toString()) != checkDigit) {
    throw NotFoundException.getNotFoundInstance();
  }
  
  return rowOffset;
}
 
Example 18
Source File: RSSExpandedReader.java    From ScreenCapture with MIT License 5 votes vote down vote up
private static int getNextSecondBar(BitArray row, int initialPos) {
  int currentPos;
  if (row.get(initialPos)) {
    currentPos = row.getNextUnset(initialPos);
    currentPos = row.getNextSet(currentPos);
  } else {
    currentPos = row.getNextSet(initialPos);
    currentPos = row.getNextUnset(currentPos);
  }
  return currentPos;
}
 
Example 19
Source File: RSSExpandedReader.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 5 votes vote down vote up
private static int getNextSecondBar(BitArray row, int initialPos){
  int currentPos;
  if (row.get(initialPos)) {
    currentPos = row.getNextUnset(initialPos);
    currentPos = row.getNextSet(currentPos);
  } else {
    currentPos = row.getNextSet(initialPos);
    currentPos = row.getNextUnset(currentPos);
  }
  return currentPos;
}
 
Example 20
Source File: RSSExpandedReader.java    From Tesseract-OCR-Scanner with Apache License 2.0 4 votes vote down vote up
private FinderPattern parseFoundFinderPattern(BitArray row, int rowNumber, boolean oddPattern) {
  // Actually we found elements 2-5.
  int firstCounter;
  int start;
  int end;

  if (oddPattern) {
    // If pattern number is odd, we need to locate element 1 *before* the current block.

    int firstElementStart = this.startEnd[0] - 1;
    // Locate element 1
    while (firstElementStart >= 0 && !row.get(firstElementStart)) {
      firstElementStart--;
    }

    firstElementStart++;
    firstCounter = this.startEnd[0] - firstElementStart;
    start = firstElementStart;
    end = this.startEnd[1];

  } else {
    // If pattern number is even, the pattern is reversed, so we need to locate element 1 *after* the current block.

    start = this.startEnd[0];

    end = row.getNextUnset(this.startEnd[1] + 1);
    firstCounter = end - this.startEnd[1];
  }

  // Make 'counters' hold 1-4
  int [] counters = this.getDecodeFinderCounters();
  System.arraycopy(counters, 0, counters, 1, counters.length - 1);

  counters[0] = firstCounter;
  int value;
  try {
    value = parseFinderValue(counters, FINDER_PATTERNS);
  } catch (NotFoundException ignored) {
    return null;
  }
  return new FinderPattern(value, new int[] {start, end}, start, end, rowNumber);
}