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

The following examples show how to use com.google.zxing.common.BitArray#appendBits() . 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 barcodescanner-lib-aar with MIT License 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 2
Source File: MatrixUtil.java    From QrCodeScanner with GNU General Public License v3.0 6 votes vote down vote up
static void makeTypeInfoBits(ErrorCorrectionLevel ecLevel, int maskPattern, BitArray bits)
    throws WriterException {
  if (!QRCode.isValidMaskPattern(maskPattern)) {
    throw new WriterException("Invalid mask pattern");
  }
  int typeInfo = (ecLevel.getBits() << 3) | maskPattern;
  bits.appendBits(typeInfo, 5);

  int bchCode = calculateBCHCode(typeInfo, TYPE_INFO_POLY);
  bits.appendBits(bchCode, 10);

  BitArray maskBits = new BitArray();
  maskBits.appendBits(TYPE_INFO_MASK_PATTERN, 15);
  bits.xor(maskBits);

  if (bits.getSize() != 15) {  // Just in case.
    throw new WriterException("should not happen but we got: " + bits.getSize());
  }
}
 
Example 3
Source File: Encoder.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 6 votes vote down vote up
static BitArray stuffBits(BitArray bits, int wordSize) {
  BitArray out = new BitArray();

  int n = bits.getSize();
  int mask = (1 << wordSize) - 2;
  for (int i = 0; i < n; i += wordSize) {
    int word = 0;
    for (int j = 0; j < wordSize; j++) {
      if (i + j >= n || bits.get(i + j)) {
        word |= 1 << (wordSize - 1 - j);
      }
    }
    if ((word & mask) == mask) {
      out.appendBits(word & mask, wordSize);
      i--;
    } else if ((word & mask) == 0) {
      out.appendBits(word | 1, wordSize);
      i--;
    } else {
      out.appendBits(word, wordSize);
    }
  }
  return out;
}
 
Example 4
Source File: MatrixUtil.java    From ScreenCapture with MIT License 6 votes vote down vote up
static void makeTypeInfoBits(ErrorCorrectionLevel ecLevel, int maskPattern, BitArray bits)
    throws WriterException {
  if (!QRCode.isValidMaskPattern(maskPattern)) {
    throw new WriterException("Invalid mask pattern");
  }
  int typeInfo = (ecLevel.getBits() << 3) | maskPattern;
  bits.appendBits(typeInfo, 5);

  int bchCode = calculateBCHCode(typeInfo, TYPE_INFO_POLY);
  bits.appendBits(bchCode, 10);

  BitArray maskBits = new BitArray();
  maskBits.appendBits(TYPE_INFO_MASK_PATTERN, 15);
  bits.xor(maskBits);

  if (bits.getSize() != 15) {  // Just in case.
    throw new WriterException("should not happen but we got: " + bits.getSize());
  }
}
 
Example 5
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 6
Source File: MatrixUtil.java    From barterli_android with Apache License 2.0 6 votes vote down vote up
static void makeTypeInfoBits(ErrorCorrectionLevel ecLevel, int maskPattern, BitArray bits) throws WriterException {
    if (!QRCode.isValidMaskPattern(maskPattern)) {
        throw new WriterException("Invalid mask pattern");
    }
    int typeInfo = (ecLevel.getBits() << 3) | maskPattern;
    bits.appendBits(typeInfo, 5);

    int bchCode = calculateBCHCode(typeInfo, TYPE_INFO_POLY);
    bits.appendBits(bchCode, 10);

    BitArray maskBits = new BitArray();
    maskBits.appendBits(TYPE_INFO_MASK_PATTERN, 15);
    bits.xor(maskBits);

    if (bits.getSize() != 15) { // Just in case.
        throw new WriterException("should not happen but we got: " + bits.getSize());
    }
}
 
Example 7
Source File: Encoder.java    From QrCodeScanner with GNU General Public License v3.0 5 votes vote down vote up
static void append8BitBytes(String content, BitArray bits, String encoding)
    throws WriterException {
  byte[] bytes;
  try {
    bytes = content.getBytes(encoding);
  } catch (UnsupportedEncodingException uee) {
    throw new WriterException(uee);
  }
  for (byte b : bytes) {
    bits.appendBits(b, 8);
  }
}
 
Example 8
Source File: MatrixUtil.java    From reacteu-app with MIT License 5 votes vote down vote up
static void makeVersionInfoBits(Version version, BitArray bits) throws WriterException {
  bits.appendBits(version.getVersionNumber(), 6);
  int bchCode = calculateBCHCode(version.getVersionNumber(), VERSION_INFO_POLY);
  bits.appendBits(bchCode, 12);

  if (bits.getSize() != 18) {  // Just in case.
    throw new WriterException("should not happen but we got: " + bits.getSize());
  }
}
 
Example 9
Source File: Encoder.java    From weex with Apache License 2.0 5 votes vote down vote up
private static BitArray generateCheckWords(BitArray bitArray, int totalBits, int wordSize) {
  // bitArray is guaranteed to be a multiple of the wordSize, so no padding needed
  int messageSizeInWords = bitArray.getSize() / wordSize;
  ReedSolomonEncoder rs = new ReedSolomonEncoder(getGF(wordSize));
  int totalWords = totalBits / wordSize;
  int[] messageWords = bitsToWords(bitArray, wordSize, totalWords);
  rs.encode(messageWords, totalWords - messageSizeInWords);
  int startPad = totalBits % wordSize;
  BitArray messageBits = new BitArray();
  messageBits.appendBits(0, startPad);
  for (int messageWord : messageWords) {
    messageBits.appendBits(messageWord, wordSize);
  }
  return messageBits;
}
 
Example 10
Source File: Encoder.java    From MiBandDecompiled with Apache License 2.0 5 votes vote down vote up
static void a(int i, int j, Mode mode, BitArray bitarray)
{
    int k = mode.getCharacterCountBits(Version.getVersionForNumber(j));
    if (i > -1 + (1 << k))
    {
        throw new WriterException((new StringBuilder()).append(i).append("is bigger than").append(-1 + (1 << k)).toString());
    } else
    {
        bitarray.appendBits(i, k);
        return;
    }
}
 
Example 11
Source File: Encoder.java    From weex with Apache License 2.0 5 votes vote down vote up
static BitArray generateModeMessage(boolean compact, int layers, int messageSizeInWords) {
  BitArray modeMessage = new BitArray();
  if (compact) {
    modeMessage.appendBits(layers - 1, 2);
    modeMessage.appendBits(messageSizeInWords - 1, 6);
    modeMessage = generateCheckWords(modeMessage, 28, 4);
  } else {
    modeMessage.appendBits(layers - 1, 5);
    modeMessage.appendBits(messageSizeInWords - 1, 11);
    modeMessage = generateCheckWords(modeMessage, 40, 4);
  }
  return modeMessage;
}
 
Example 12
Source File: Encoder.java    From reacteu-app with MIT License 5 votes vote down vote up
/**
 * Append length info. On success, store the result in "bits".
 */
static void appendLengthInfo(int numLetters, Version version, Mode mode, BitArray bits) throws WriterException {
  int numBits = mode.getCharacterCountBits(version);
  if (numLetters >= (1 << numBits)) {
    throw new WriterException(numLetters + " is bigger than " + ((1 << numBits) - 1));
  }
  bits.appendBits(numLetters, numBits);
}
 
Example 13
Source File: Encoder.java    From Tesseract-OCR-Scanner with Apache License 2.0 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 14
Source File: Encoder.java    From barcodescanner-lib-aar with MIT License 5 votes vote down vote up
/**
 * Append length info. On success, store the result in "bits".
 */
static void appendLengthInfo(int numLetters, Version version, Mode mode, BitArray bits) throws WriterException {
  int numBits = mode.getCharacterCountBits(version);
  if (numLetters >= (1 << numBits)) {
    throw new WriterException(numLetters + " is bigger than " + ((1 << numBits) - 1));
  }
  bits.appendBits(numLetters, numBits);
}
 
Example 15
Source File: d.java    From MiBandDecompiled with Apache License 2.0 5 votes vote down vote up
static void a(int j, BitArray bitarray)
{
    bitarray.appendBits(j, 6);
    bitarray.appendBits(a(j, 7973), 12);
    if (bitarray.getSize() != 18)
    {
        throw new WriterException((new StringBuilder()).append("should not happen but we got: ").append(bitarray.getSize()).toString());
    } else
    {
        return;
    }
}
 
Example 16
Source File: MatrixUtil.java    From ZXing-Orient with Apache License 2.0 5 votes vote down vote up
static void makeVersionInfoBits(Version version, BitArray bits) throws WriterException {
  bits.appendBits(version.getVersionNumber(), 6);
  int bchCode = calculateBCHCode(version.getVersionNumber(), VERSION_INFO_POLY);
  bits.appendBits(bchCode, 12);

  if (bits.getSize() != 18) {  // Just in case.
    throw new WriterException("should not happen but we got: " + bits.getSize());
  }
}
 
Example 17
Source File: Encoder.java    From Tesseract-OCR-Scanner with Apache License 2.0 4 votes vote down vote up
/**
 * Append mode info. On success, store the result in "bits".
 */
static void appendModeInfo(Mode mode, BitArray bits) {
  bits.appendBits(mode.getBits(), 4);
}
 
Example 18
Source File: Encoder.java    From RipplePower with Apache License 2.0 4 votes vote down vote up
private static void appendECI(CharacterSetECI eci, BitArray bits) {
	bits.appendBits(Mode.ECI.getBits(), 4);
	// This is correct for values up to 127, which is all we need now.
	bits.appendBits(eci.getValue(), 8);
}
 
Example 19
Source File: Encoder.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 4 votes vote down vote up
private static void appendECI(CharacterSetECI eci, BitArray bits) {
  bits.appendBits(Mode.ECI.getBits(), 4);
  // This is correct for values up to 127, which is all we need now.
  bits.appendBits(eci.getValue(), 8);
}
 
Example 20
Source File: Encoder.java    From barcodescanner-lib-aar with MIT License 4 votes vote down vote up
private static void appendECI(CharacterSetECI eci, BitArray bits) {
  bits.appendBits(Mode.ECI.getBits(), 4);
  // This is correct for values up to 127, which is all we need now.
  bits.appendBits(eci.getValue(), 8);
}