com.google.zxing.common.BitArray Java Examples

The following examples show how to use com.google.zxing.common.BitArray. 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: Encoder.java    From ZXing-Orient with Apache License 2.0 6 votes vote down vote up
static void appendKanjiBytes(String content, BitArray bits) throws WriterException {
  byte[] bytes;
  try {
    bytes = content.getBytes("Shift_JIS");
  } catch (UnsupportedEncodingException uee) {
    throw new WriterException(uee);
  }
  int length = bytes.length;
  for (int i = 0; i < length; i += 2) {
    int byte1 = bytes[i] & 0xFF;
    int byte2 = bytes[i + 1] & 0xFF;
    int code = (byte1 << 8) | byte2;
    int subtracted = -1;
    if (code >= 0x8140 && code <= 0x9ffc) {
      subtracted = code - 0x8140;
    } else if (code >= 0xe040 && code <= 0xebbf) {
      subtracted = code - 0xc140;
    }
    if (subtracted == -1) {
      throw new WriterException("Invalid byte sequence");
    }
    int encoded = ((subtracted >> 8) * 0xc0) + (subtracted & 0xff);
    bits.appendBits(encoded, 13);
  }
}
 
Example #2
Source File: UPCEANExtension5Support.java    From barcodescanner-lib-aar with MIT License 6 votes vote down vote up
Result decodeRow(int rowNumber, BitArray row, int[] extensionStartRange) throws NotFoundException {

    StringBuilder result = decodeRowStringBuffer;
    result.setLength(0);
    int end = decodeMiddle(row, extensionStartRange, result);

    String resultString = result.toString();
    Map<ResultMetadataType,Object> extensionData = parseExtensionString(resultString);

    Result extensionResult =
        new Result(resultString,
                   null,
                   new ResultPoint[] {
                       new ResultPoint((extensionStartRange[0] + extensionStartRange[1]) / 2.0f, rowNumber),
                       new ResultPoint(end, rowNumber),
                   },
                   BarcodeFormat.UPC_EAN_EXTENSION);
    if (extensionData != null) {
      extensionResult.putAllMetadata(extensionData);
    }
    return extensionResult;
  }
 
Example #3
Source File: UPCEANReader.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
static int[] findStartGuardPattern(BitArray row) throws NotFoundException {
	boolean 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);
		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;
}
 
Example #4
Source File: Encoder.java    From MiBandDecompiled with Apache License 2.0 6 votes vote down vote up
static void a(CharSequence charsequence, BitArray bitarray)
{
    int i = charsequence.length();
    for (int j = 0; j < i;)
    {
        int k = -48 + charsequence.charAt(j);
        if (j + 2 < i)
        {
            int l = -48 + charsequence.charAt(j + 1);
            bitarray.appendBits(-48 + charsequence.charAt(j + 2) + (k * 100 + l * 10), 10);
            j += 3;
        } else
        if (j + 1 < i)
        {
            bitarray.appendBits(-48 + charsequence.charAt(j + 1) + k * 10, 7);
            j += 2;
        } else
        {
            bitarray.appendBits(k, 4);
            j++;
        }
    }

}
 
Example #5
Source File: ITFReader.java    From reacteu-app with MIT License 6 votes vote down vote up
/**
 * 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
 *
 * @param row bit array representing the scanned barcode.
 * @param startPattern index into row of the start or end pattern.
 * @throws NotFoundException if the quiet zone cannot be found, a ReaderException is thrown.
 */
private void validateQuietZone(BitArray row, int startPattern) throws NotFoundException {

  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.getNotFoundInstance();
  }
}
 
Example #6
Source File: RSS14Reader.java    From weex with Apache License 2.0 6 votes vote down vote up
private FinderPattern parseFoundFinderPattern(BitArray row, int rowNumber, boolean right, int[] startEnd)
    throws NotFoundException {
  // Actually we found elements 2-5
  boolean firstIsBlack = row.get(startEnd[0]);
  int firstElementStart = startEnd[0] - 1;
  // Locate element 1
  while (firstElementStart >= 0 && firstIsBlack ^ row.get(firstElementStart)) {
    firstElementStart--;
  }
  firstElementStart++;
  int firstCounter = startEnd[0] - firstElementStart;
  // Make 'counters' hold 1-4
  int[] counters = getDecodeFinderCounters();
  System.arraycopy(counters, 0, counters, 1, counters.length - 1);
  counters[0] = firstCounter;
  int value = parseFinderValue(counters, FINDER_PATTERNS);
  int start = firstElementStart;
  int end = startEnd[1];
  if (right) {
    // row is actually reversed
    start = row.getSize() - 1 - start;
    end = row.getSize() - 1 - end;
  }
  return new FinderPattern(value, new int[] {firstElementStart, startEnd[1]}, start, end, rowNumber);
}
 
Example #7
Source File: UPCEANExtension2Support.java    From ZXing-Orient with Apache License 2.0 6 votes vote down vote up
Result decodeRow(int rowNumber, BitArray row, int[] extensionStartRange) throws NotFoundException {

    StringBuilder result = decodeRowStringBuffer;
    result.setLength(0);
    int end = decodeMiddle(row, extensionStartRange, result);

    String resultString = result.toString();
    Map<ResultMetadataType,Object> extensionData = parseExtensionString(resultString);

    Result extensionResult =
        new Result(resultString,
                   null,
                   new ResultPoint[] {
                       new ResultPoint((extensionStartRange[0] + extensionStartRange[1]) / 2.0f, (float) rowNumber),
                       new ResultPoint((float) end, (float) rowNumber),
                   },
                   BarcodeFormat.UPC_EAN_EXTENSION);
    if (extensionData != null) {
      extensionResult.putAllMetadata(extensionData);
    }
    return extensionResult;
  }
 
Example #8
Source File: CodaBarReader.java    From RipplePower 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 #9
Source File: UPCEANReader.java    From weex with Apache License 2.0 6 votes vote down vote up
/**
 * Attempts to decode a single UPC/EAN-encoded digit.
 *
 * @param row row of black/white values to decode
 * @param counters the counts of runs of observed black/white/black/... values
 * @param rowOffset horizontal offset to start decoding from
 * @param patterns the set of patterns to use to decode -- sometimes different encodings
 * for the digits 0-9 are used, and this indicates the encodings for 0 to 9 that should
 * be used
 * @return horizontal offset of first pixel beyond the decoded digit
 * @throws NotFoundException if digit cannot be decoded
 */
static int decodeDigit(BitArray row, int[] counters, int rowOffset, int[][] patterns)
    throws NotFoundException {
  recordPattern(row, rowOffset, counters);
  float bestVariance = MAX_AVG_VARIANCE; // worst variance we'll accept
  int bestMatch = -1;
  int max = patterns.length;
  for (int i = 0; i < max; i++) {
    int[] pattern = patterns[i];
    float variance = patternMatchVariance(counters, pattern, MAX_INDIVIDUAL_VARIANCE);
    if (variance < bestVariance) {
      bestVariance = variance;
      bestMatch = i;
    }
  }
  if (bestMatch >= 0) {
    return bestMatch;
  } else {
    throw NotFoundException.getNotFoundInstance();
  }
}
 
Example #10
Source File: Encoder.java    From Tesseract-OCR-Scanner with Apache License 2.0 6 votes vote down vote up
static void appendKanjiBytes(String content, BitArray bits) throws WriterException {
  byte[] bytes;
  try {
    bytes = content.getBytes("Shift_JIS");
  } catch (UnsupportedEncodingException uee) {
    throw new WriterException(uee);
  }
  int length = bytes.length;
  for (int i = 0; i < length; i += 2) {
    int byte1 = bytes[i] & 0xFF;
    int byte2 = bytes[i + 1] & 0xFF;
    int code = (byte1 << 8) | byte2;
    int subtracted = -1;
    if (code >= 0x8140 && code <= 0x9ffc) {
      subtracted = code - 0x8140;
    } else if (code >= 0xe040 && code <= 0xebbf) {
      subtracted = code - 0xc140;
    }
    if (subtracted == -1) {
      throw new WriterException("Invalid byte sequence");
    }
    int encoded = ((subtracted >> 8) * 0xc0) + (subtracted & 0xff);
    bits.appendBits(encoded, 13);
  }
}
 
Example #11
Source File: UPCEANExtension2Support.java    From reacteu-app with MIT License 6 votes vote down vote up
Result decodeRow(int rowNumber, BitArray row, int[] extensionStartRange) throws NotFoundException {

    StringBuilder result = decodeRowStringBuffer;
    result.setLength(0);
    int end = decodeMiddle(row, extensionStartRange, result);

    String resultString = result.toString();
    Map<ResultMetadataType,Object> extensionData = parseExtensionString(resultString);

    Result extensionResult =
        new Result(resultString,
                   null,
                   new ResultPoint[] {
                       new ResultPoint((extensionStartRange[0] + extensionStartRange[1]) / 2.0f, (float) rowNumber),
                       new ResultPoint((float) end, (float) rowNumber),
                   },
                   BarcodeFormat.UPC_EAN_EXTENSION);
    if (extensionData != null) {
      extensionResult.putAllMetadata(extensionData);
    }
    return extensionResult;
  }
 
Example #12
Source File: MatrixUtil.java    From Tesseract-OCR-Scanner with Apache License 2.0 6 votes vote down vote up
static void maybeEmbedVersionInfo(Version version, ByteMatrix matrix) throws WriterException {
  if (version.getVersionNumber() < 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.
      boolean bit = versionInfoBits.get(bitIndex);
      bitIndex--;
      // Left bottom corner.
      matrix.set(i, matrix.getHeight() - 11 + j, bit);
      // Right bottom corner.
      matrix.set(matrix.getHeight() - 11 + j, i, bit);
    }
  }
}
 
Example #13
Source File: UPCEANReader.java    From weex with Apache License 2.0 6 votes vote down vote up
static int[] findStartGuardPattern(BitArray row) throws NotFoundException {
  boolean 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);
    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;
}
 
Example #14
Source File: Encoder.java    From Tesseract-OCR-Scanner with Apache License 2.0 6 votes vote down vote up
/**
 * Append "bytes" in "mode" mode (encoding) into "bits". On success, store the result in "bits".
 */
static void appendBytes(String content,
                        Mode mode,
                        BitArray bits,
                        String encoding) throws WriterException {
  switch (mode) {
    case NUMERIC:
      appendNumericBytes(content, bits);
      break;
    case ALPHANUMERIC:
      appendAlphanumericBytes(content, bits);
      break;
    case BYTE:
      append8BitBytes(content, bits, encoding);
      break;
    case KANJI:
      appendKanjiBytes(content, bits);
      break;
    default:
      throw new WriterException("Invalid mode: " + mode);
  }
}
 
Example #15
Source File: Encoder.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
static void appendAlphanumericBytes(CharSequence content, BitArray bits) throws WriterException {
  int length = content.length();
  int i = 0;
  while (i < length) {
    int code1 = getAlphanumericCode(content.charAt(i));
    if (code1 == -1) {
      throw new WriterException();
    }
    if (i + 1 < length) {
      int code2 = getAlphanumericCode(content.charAt(i + 1));
      if (code2 == -1) {
        throw new WriterException();
      }
      // Encode two alphanumeric letters in 11 bits.
      bits.appendBits(code1 * 45 + code2, 11);
      i += 2;
    } else {
      // Encode one alphanumeric letter in six bits.
      bits.appendBits(code1, 6);
      i++;
    }
  }
}
 
Example #16
Source File: OneDReader.java    From QrCodeScanner with GNU General Public License v3.0 6 votes vote down vote up
protected static void recordPatternInReverse(BitArray row, int start, int[] counters)
    throws NotFoundException {
  // This could be more efficient I guess
  int numTransitionsLeft = counters.length;
  boolean last = row.get(start);
  while (start > 0 && numTransitionsLeft >= 0) {
    if (row.get(--start) != last) {
      numTransitionsLeft--;
      last = !last;
    }
  }
  if (numTransitionsLeft >= 0) {
    throw NotFoundException.getNotFoundInstance();
  }
  recordPattern(row, start + 1, counters);
}
 
Example #17
Source File: UPCEANExtension5Support.java    From reacteu-app with MIT License 6 votes vote down vote up
Result decodeRow(int rowNumber, BitArray row, int[] extensionStartRange) throws NotFoundException {

    StringBuilder result = decodeRowStringBuffer;
    result.setLength(0);
    int end = decodeMiddle(row, extensionStartRange, result);

    String resultString = result.toString();
    Map<ResultMetadataType,Object> extensionData = parseExtensionString(resultString);

    Result extensionResult =
        new Result(resultString,
                   null,
                   new ResultPoint[] {
                       new ResultPoint((extensionStartRange[0] + extensionStartRange[1]) / 2.0f, (float) rowNumber),
                       new ResultPoint((float) end, (float) rowNumber),
                   },
                   BarcodeFormat.UPC_EAN_EXTENSION);
    if (extensionData != null) {
      extensionResult.putAllMetadata(extensionData);
    }
    return extensionResult;
  }
 
Example #18
Source File: MatrixUtil.java    From QrCodeScanner with GNU General Public License v3.0 6 votes vote down vote up
static void maybeEmbedVersionInfo(Version version, ByteMatrix matrix) throws WriterException {
  if (version.getVersionNumber() < 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.
      boolean bit = versionInfoBits.get(bitIndex);
      bitIndex--;
      // Left bottom corner.
      matrix.set(i, matrix.getHeight() - 11 + j, bit);
      // Right bottom corner.
      matrix.set(matrix.getHeight() - 11 + j, i, bit);
    }
  }
}
 
Example #19
Source File: ITFReader.java    From Tesseract-OCR-Scanner with Apache License 2.0 5 votes vote down vote up
/**
 * @param row          row of black/white values to search
 * @param payloadStart offset of start pattern
 * @param resultString {@link StringBuilder} to append decoded chars to
 * @throws NotFoundException if decoding could not complete successfully
 */
private static void decodeMiddle(BitArray row,
                                 int payloadStart,
                                 int payloadEnd,
                                 StringBuilder resultString) throws NotFoundException {

  // Digits are interleaved in pairs - 5 black lines for one digit, and the
  // 5
  // interleaved white lines for the second digit.
  // Therefore, need to scan 10 lines and then
  // split these into two arrays
  int[] counterDigitPair = new int[10];
  int[] counterBlack = new int[5];
  int[] counterWhite = new int[5];

  while (payloadStart < payloadEnd) {

    // Get 10 runs of black/white.
    recordPattern(row, payloadStart, counterDigitPair);
    // Split them into each array
    for (int k = 0; k < 5; k++) {
      int twoK = 2 * k;
      counterBlack[k] = counterDigitPair[twoK];
      counterWhite[k] = counterDigitPair[twoK + 1];
    }

    int bestMatch = decodeDigit(counterBlack);
    resultString.append((char) ('0' + bestMatch));
    bestMatch = decodeDigit(counterWhite);
    resultString.append((char) ('0' + bestMatch));

    for (int counterDigit : counterDigitPair) {
      payloadStart += counterDigit;
    }
  }
}
 
Example #20
Source File: State.java    From ZXing-Orient with Apache License 2.0 5 votes vote down vote up
BitArray toBitArray(byte[] text) {
  // Reverse the tokens, so that they are in the order that they should
  // be output
  Deque<Token> symbols = new LinkedList<>();
  for (Token token = endBinaryShift(text.length).token; token != null; token = token.getPrevious()) {
    symbols.addFirst(token);
  }
  BitArray bitArray = new BitArray();
  // Add each token to the result.
  for (Token symbol : symbols) {
    symbol.appendTo(bitArray, text);
  }
  //assert bitArray.getSize() == this.bitCount;
  return bitArray;
}
 
Example #21
Source File: UPCEANExtensionSupport.java    From Tesseract-OCR-Scanner with Apache License 2.0 5 votes vote down vote up
Result decodeRow(int rowNumber, BitArray row, int rowOffset) throws NotFoundException {
  int[] extensionStartRange = UPCEANReader.findGuardPattern(row, rowOffset, false, EXTENSION_START_PATTERN);
  try {
    return fiveSupport.decodeRow(rowNumber, row, extensionStartRange);
  } catch (ReaderException ignored) {
    return twoSupport.decodeRow(rowNumber, row, extensionStartRange);
  }
}
 
Example #22
Source File: ITFReader.java    From Tesseract-OCR-Scanner with Apache License 2.0 5 votes vote down vote up
/**
 * Identify where the start of the middle / payload section starts.
 *
 * @param row row of black/white values to search
 * @return Array, containing index of start of 'start block' and end of
 *         'start block'
 */
private int[] decodeStart(BitArray row) throws NotFoundException {
  int endStart = skipWhiteSpace(row);
  int[] startPattern = findGuardPattern(row, endStart, START_PATTERN);

  // Determine the width of a narrow line in pixels. We can do this by
  // getting the width of the start pattern and dividing by 4 because its
  // made up of 4 narrow lines.
  this.narrowLineWidth = (startPattern[1] - startPattern[0]) / 4;

  validateQuietZone(row, startPattern[0]);

  return startPattern;
}
 
Example #23
Source File: RSSExpandedReader.java    From MiBandDecompiled with Apache License 2.0 5 votes vote down vote up
private static int a(BitArray bitarray, int i1)
{
    if (bitarray.get(i1))
    {
        return bitarray.getNextSet(bitarray.getNextUnset(i1));
    } else
    {
        return bitarray.getNextUnset(bitarray.getNextSet(i1));
    }
}
 
Example #24
Source File: Code39Reader.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
private static int[] findAsteriskPattern(BitArray row, int[] counters) throws NotFoundException {
	int width = row.getSize();
	int rowOffset = row.getNextSet(0);

	int counterPosition = 0;
	int patternStart = rowOffset;
	boolean 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) / 2)), patternStart, false)) {
					return new int[] { patternStart, i };
				}
				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 #25
Source File: Encoder.java    From barcodescanner-lib-aar with MIT License 5 votes vote down vote up
/**
 * Terminate bits as described in 8.4.8 and 8.4.9 of JISX0510:2004 (p.24).
 */
static void terminateBits(int numDataBytes, BitArray bits) throws WriterException {
  int capacity = numDataBytes * 8;
  if (bits.getSize() > capacity) {
    throw new WriterException("data bits cannot fit in the QR Code" + bits.getSize() + " > " +
        capacity);
  }
  for (int i = 0; i < 4 && bits.getSize() < capacity; ++i) {
    bits.appendBit(false);
  }
  // Append termination bits. See 8.4.8 of JISX0510:2004 (p.24) for details.
  // If the last byte isn't 8-bit aligned, we'll add padding bits.
  int numBitsInLastByte = bits.getSize() & 0x07;    
  if (numBitsInLastByte > 0) {
    for (int i = numBitsInLastByte; i < 8; i++) {
      bits.appendBit(false);
    }
  }
  // If we have more space, we'll fill the space with padding patterns defined in 8.4.9 (p.24).
  int numPaddingBytes = numDataBytes - bits.getSizeInBytes();
  for (int i = 0; i < numPaddingBytes; ++i) {
    bits.appendBits((i & 0x01) == 0 ? 0xEC : 0x11, 8);
  }
  if (bits.getSize() != capacity) {
    throw new WriterException("Bits size does not equal capacity");
  }
}
 
Example #26
Source File: RSSExpandedReader.java    From QrCodeScanner with GNU General Public License v3.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 #27
Source File: Encoder.java    From barterli_android with Apache License 2.0 5 votes vote down vote up
/**
 * Append length info. On success, store the result in "bits".
 */
static void appendLengthInfo(int numLetters, int version, Mode mode, BitArray bits) throws WriterException {
    int numBits = mode.getCharacterCountBits(Version.getVersionForNumber(version));
    if (numLetters > ((1 << numBits) - 1)) {
        throw new WriterException(numLetters + "is bigger than" + ((1 << numBits) - 1));
    }
    bits.appendBits(numLetters, numBits);
}
 
Example #28
Source File: Code39Reader.java    From ZXing-Orient with Apache License 2.0 5 votes vote down vote up
private static int[] findAsteriskPattern(BitArray row, int[] counters) throws NotFoundException {
  int width = row.getSize();
  int rowOffset = row.getNextSet(0);

  int counterPosition = 0;
  int patternStart = rowOffset;
  boolean 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) / 2)), patternStart, false)) {
          return new int[]{patternStart, i};
        }
        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 #29
Source File: Code39Reader.java    From barcodescanner-lib-aar with MIT License 5 votes vote down vote up
private static int[] findAsteriskPattern(BitArray row, int[] counters) throws NotFoundException {
  int width = row.getSize();
  int rowOffset = row.getNextSet(0);

  int counterPosition = 0;
  int patternStart = rowOffset;
  boolean 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) / 2)), patternStart, false)) {
          return new int[]{patternStart, i};
        }
        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 #30
Source File: MultiFormatOneDReader.java    From Tesseract-OCR-Scanner with Apache License 2.0 5 votes vote down vote up
@Override
public Result decodeRow(int rowNumber,
                        BitArray row,
                        Map<DecodeHintType,?> hints) throws NotFoundException {
  for (OneDReader reader : readers) {
    try {
      return reader.decodeRow(rowNumber, row, hints);
    } catch (ReaderException re) {
      // continue
    }
  }

  throw NotFoundException.getNotFoundInstance();
}