Java Code Examples for com.google.zxing.qrcode.decoder.Version#getTotalCodewords()

The following examples show how to use com.google.zxing.qrcode.decoder.Version#getTotalCodewords() . 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 RipplePower with Apache License 2.0 6 votes vote down vote up
private static Version chooseVersion(int numInputBits, ErrorCorrectionLevel ecLevel) throws WriterException {
	// In the following comments, we use numbers of Version 7-H.
	for (int versionNum = 1; versionNum <= 40; versionNum++) {
		Version version = Version.getVersionForNumber(versionNum);
		// numBytes = 196
		int numBytes = version.getTotalCodewords();
		// getNumECBytes = 130
		Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel);
		int numEcBytes = ecBlocks.getTotalECCodewords();
		// getNumDataBytes = 196 - 130 = 66
		int numDataBytes = numBytes - numEcBytes;
		int totalInputBytes = (numInputBits + 7) / 8;
		if (numDataBytes >= totalInputBytes) {
			return version;
		}
	}
	throw new WriterException("Data too big");
}
 
Example 2
Source File: Qr.java    From bither-android with Apache License 2.0 6 votes vote down vote up
public static void printQrContentSize() {
    for (int versionNum = 1;
         versionNum <= 40;
         versionNum++) {
        Version version = Version.getVersionForNumber(versionNum);
        // numBytes = 196
        int numBytes = version.getTotalCodewords();
        // getNumECBytes = 130
        Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ErrorCorrectionLevel.L);
        int numEcBytes = ecBlocks.getTotalECCodewords();
        // getNumDataBytes = 196 - 130 = 66
        int numDataBytes = numBytes - numEcBytes;
        int numInputBytes = numDataBytes * 8 - 7;
        int length = (numInputBytes - 10) / 11 * 2;
        LogUtil.d("Qr", "Version: " + versionNum + " numData bytes: " + numDataBytes + "  " +
                "input: " + numInputBytes + "  string length: " + length);
    }
}
 
Example 3
Source File: Encoder.java    From MiBandDecompiled with Apache License 2.0 6 votes vote down vote up
private static void a(int i, ErrorCorrectionLevel errorcorrectionlevel, Mode mode, QRCode qrcode)
{
    qrcode.setECLevel(errorcorrectionlevel);
    qrcode.setMode(mode);
    for (int j = 1; j <= 40; j++)
    {
        Version version = Version.getVersionForNumber(j);
        int k = version.getTotalCodewords();
        com.google.zxing.qrcode.decoder.Version.ECBlocks ecblocks = version.getECBlocksForLevel(errorcorrectionlevel);
        int l = ecblocks.getTotalECCodewords();
        int i1 = ecblocks.getNumBlocks();
        int j1 = k - l;
        if (j1 >= a(i, version, mode))
        {
            qrcode.setVersion(j);
            qrcode.setNumTotalBytes(k);
            qrcode.setNumDataBytes(j1);
            qrcode.setNumRSBlocks(i1);
            qrcode.setNumECBytes(l);
            qrcode.setMatrixWidth(version.getDimensionForVersion());
            return;
        }
    }

    throw new WriterException("Cannot find proper rs block info (input data too big?)");
}
 
Example 4
Source File: Encoder.java    From reacteu-app with MIT License 6 votes vote down vote up
private static Version chooseVersion(int numInputBits, ErrorCorrectionLevel ecLevel) throws WriterException {
  // In the following comments, we use numbers of Version 7-H.
  for (int versionNum = 1; versionNum <= 40; versionNum++) {
    Version version = Version.getVersionForNumber(versionNum);
    // numBytes = 196
    int numBytes = version.getTotalCodewords();
    // getNumECBytes = 130
    Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel);
    int numEcBytes = ecBlocks.getTotalECCodewords();
    // getNumDataBytes = 196 - 130 = 66
    int numDataBytes = numBytes - numEcBytes;
    int totalInputBytes = (numInputBits + 7) / 8;
    if (numDataBytes >= totalInputBytes) {
      return version;
    }
  }
  throw new WriterException("Data too big");
}
 
Example 5
Source File: Encoder.java    From weex with Apache License 2.0 6 votes vote down vote up
private static Version chooseVersion(int numInputBits, ErrorCorrectionLevel ecLevel) throws WriterException {
  // In the following comments, we use numbers of Version 7-H.
  for (int versionNum = 1; versionNum <= 40; versionNum++) {
    Version version = Version.getVersionForNumber(versionNum);
    // numBytes = 196
    int numBytes = version.getTotalCodewords();
    // getNumECBytes = 130
    Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel);
    int numEcBytes = ecBlocks.getTotalECCodewords();
    // getNumDataBytes = 196 - 130 = 66
    int numDataBytes = numBytes - numEcBytes;
    int totalInputBytes = (numInputBits + 7) / 8;
    if (numDataBytes >= totalInputBytes) {
      return version;
    }
  }
  throw new WriterException("Data too big");
}
 
Example 6
Source File: Encoder.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 6 votes vote down vote up
private static Version chooseVersion(int numInputBits, ErrorCorrectionLevel ecLevel) throws WriterException {
  // In the following comments, we use numbers of Version 7-H.
  for (int versionNum = 1; versionNum <= 40; versionNum++) {
    Version version = Version.getVersionForNumber(versionNum);
    // numBytes = 196
    int numBytes = version.getTotalCodewords();
    // getNumECBytes = 130
    Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel);
    int numEcBytes = ecBlocks.getTotalECCodewords();
    // getNumDataBytes = 196 - 130 = 66
    int numDataBytes = numBytes - numEcBytes;
    int totalInputBytes = (numInputBits + 7) / 8;
    if (numDataBytes >= totalInputBytes) {
      return version;
    }
  }
  throw new WriterException("Data too big");
}
 
Example 7
Source File: Encoder.java    From ScreenCapture with MIT License 5 votes vote down vote up
/**
 * @return true if the number of input bits will fit in a code with the specified version and
 * error correction level.
 */
private static boolean willFit(int numInputBits, Version version, ErrorCorrectionLevel ecLevel) {
    // In the following comments, we use numbers of Version 7-H.
    // numBytes = 196
    int numBytes = version.getTotalCodewords();
    // getNumECBytes = 130
    Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel);
    int numEcBytes = ecBlocks.getTotalECCodewords();
    // getNumDataBytes = 196 - 130 = 66
    int numDataBytes = numBytes - numEcBytes;
    int totalInputBytes = (numInputBits + 7) / 8;
    return numDataBytes >= totalInputBytes;
}
 
Example 8
Source File: Encoder.java    From barcodescanner-lib-aar with MIT License 5 votes vote down vote up
/**
 * @return true if the number of input bits will fit in a code with the specified version and
 * error correction level.
 */
private static boolean willFit(int numInputBits, Version version, ErrorCorrectionLevel ecLevel) {
    // In the following comments, we use numbers of Version 7-H.
    // numBytes = 196
    int numBytes = version.getTotalCodewords();
    // getNumECBytes = 130
    Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel);
    int numEcBytes = ecBlocks.getTotalECCodewords();
    // getNumDataBytes = 196 - 130 = 66
    int numDataBytes = numBytes - numEcBytes;
    int totalInputBytes = (numInputBits + 7) / 8;
    return numDataBytes >= totalInputBytes;
}
 
Example 9
Source File: Encoder.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @return true if the number of input bits will fit in a code with the specified version and
 * error correction level.
 */
private static boolean willFit(int numInputBits, Version version, ErrorCorrectionLevel ecLevel) {
    // In the following comments, we use numbers of Version 7-H.
    // numBytes = 196
    int numBytes = version.getTotalCodewords();
    // getNumECBytes = 130
    Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel);
    int numEcBytes = ecBlocks.getTotalECCodewords();
    // getNumDataBytes = 196 - 130 = 66
    int numDataBytes = numBytes - numEcBytes;
    int totalInputBytes = (numInputBits + 7) / 8;
    return numDataBytes >= totalInputBytes;
}
 
Example 10
Source File: Encoder.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @return true if the number of input bits will fit in a code with the specified version and
 * error correction level.
 */
private static boolean willFit(int numInputBits, Version version, ErrorCorrectionLevel ecLevel) {
    // In the following comments, we use numbers of Version 7-H.
    // numBytes = 196
    int numBytes = version.getTotalCodewords();
    // getNumECBytes = 130
    Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel);
    int numEcBytes = ecBlocks.getTotalECCodewords();
    // getNumDataBytes = 196 - 130 = 66
    int numDataBytes = numBytes - numEcBytes;
    int totalInputBytes = (numInputBits + 7) / 8;
    return numDataBytes >= totalInputBytes;
}
 
Example 11
Source File: Encoder.java    From Tesseract-OCR-Scanner with Apache License 2.0 5 votes vote down vote up
/**
 * @return true if the number of input bits will fit in a code with the specified version and
 * error correction level.
 */
private static boolean willFit(int numInputBits, Version version, ErrorCorrectionLevel ecLevel) {
    // In the following comments, we use numbers of Version 7-H.
    // numBytes = 196
    int numBytes = version.getTotalCodewords();
    // getNumECBytes = 130
    Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel);
    int numEcBytes = ecBlocks.getTotalECCodewords();
    // getNumDataBytes = 196 - 130 = 66
    int numDataBytes = numBytes - numEcBytes;
    int totalInputBytes = (numInputBits + 7) / 8;
    return numDataBytes >= totalInputBytes;
}
 
Example 12
Source File: Encoder.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 4 votes vote down vote up
public static QRCode encode(String content,
                            ErrorCorrectionLevel ecLevel,
                            Map<EncodeHintType,?> hints) throws WriterException {

  // Determine what character encoding has been specified by the caller, if any
  String encoding = DEFAULT_BYTE_MODE_ENCODING;
  if (hints != null && hints.containsKey(EncodeHintType.CHARACTER_SET)) {
    encoding = hints.get(EncodeHintType.CHARACTER_SET).toString();
  }

  // Pick an encoding mode appropriate for the content. Note that this will not attempt to use
  // multiple modes / segments even if that were more efficient. Twould be nice.
  Mode mode = chooseMode(content, encoding);

  // This will store the header information, like mode and
  // length, as well as "header" segments like an ECI segment.
  BitArray headerBits = new BitArray();

  // Append ECI segment if applicable
  if (mode == Mode.BYTE && !DEFAULT_BYTE_MODE_ENCODING.equals(encoding)) {
    CharacterSetECI eci = CharacterSetECI.getCharacterSetECIByName(encoding);
    if (eci != null) {
      appendECI(eci, headerBits);
    }
  }

  // (With ECI in place,) Write the mode marker
  appendModeInfo(mode, headerBits);

  // Collect data within the main segment, separately, to count its size if needed. Don't add it to
  // main payload yet.
  BitArray dataBits = new BitArray();
  appendBytes(content, mode, dataBits, encoding);

  // Hard part: need to know version to know how many bits length takes. But need to know how many
  // bits it takes to know version. First we take a guess at version by assuming version will be
  // the minimum, 1:

  int provisionalBitsNeeded = headerBits.getSize()
      + mode.getCharacterCountBits(Version.getVersionForNumber(1))
      + dataBits.getSize();
  Version provisionalVersion = chooseVersion(provisionalBitsNeeded, ecLevel);

  // Use that guess to calculate the right version. I am still not sure this works in 100% of cases.

  int bitsNeeded = headerBits.getSize()
      + mode.getCharacterCountBits(provisionalVersion)
      + dataBits.getSize();
  Version version = chooseVersion(bitsNeeded, ecLevel);

  BitArray headerAndDataBits = new BitArray();
  headerAndDataBits.appendBitArray(headerBits);
  // Find "length" of main segment and write it
  int numLetters = mode == Mode.BYTE ? dataBits.getSizeInBytes() : content.length();
  appendLengthInfo(numLetters, version, mode, headerAndDataBits);
  // Put data together into the overall payload
  headerAndDataBits.appendBitArray(dataBits);

  Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel);
  int numDataBytes = version.getTotalCodewords() - ecBlocks.getTotalECCodewords();

  // Terminate the bits properly.
  terminateBits(numDataBytes, headerAndDataBits);

  // Interleave data bits with error correction code.
  BitArray finalBits = interleaveWithECBytes(headerAndDataBits,
                                             version.getTotalCodewords(),
                                             numDataBytes,
                                             ecBlocks.getNumBlocks());

  QRCode qrCode = new QRCode();

  qrCode.setECLevel(ecLevel);
  qrCode.setMode(mode);
  qrCode.setVersion(version);

  //  Choose the mask pattern and set to "qrCode".
  int dimension = version.getDimensionForVersion();
  ByteMatrix matrix = new ByteMatrix(dimension, dimension);
  int maskPattern = chooseMaskPattern(finalBits, ecLevel, version, matrix);
  qrCode.setMaskPattern(maskPattern);

  // Build the matrix and set it to "qrCode".
  MatrixUtil.buildMatrix(finalBits, ecLevel, version, maskPattern, matrix);
  qrCode.setMatrix(matrix);

  return qrCode;
}
 
Example 13
Source File: Encoder.java    From weex with Apache License 2.0 4 votes vote down vote up
public static QRCode encode(String content,
                            ErrorCorrectionLevel ecLevel,
                            Map<EncodeHintType,?> hints) throws WriterException {

  // Determine what character encoding has been specified by the caller, if any
  String encoding = DEFAULT_BYTE_MODE_ENCODING;
  if (hints != null && hints.containsKey(EncodeHintType.CHARACTER_SET)) {
    encoding = hints.get(EncodeHintType.CHARACTER_SET).toString();
  }

  // Pick an encoding mode appropriate for the content. Note that this will not attempt to use
  // multiple modes / segments even if that were more efficient. Twould be nice.
  Mode mode = chooseMode(content, encoding);

  // This will store the header information, like mode and
  // length, as well as "header" segments like an ECI segment.
  BitArray headerBits = new BitArray();

  // Append ECI segment if applicable
  if (mode == Mode.BYTE && !DEFAULT_BYTE_MODE_ENCODING.equals(encoding)) {
    CharacterSetECI eci = CharacterSetECI.getCharacterSetECIByName(encoding);
    if (eci != null) {
      appendECI(eci, headerBits);
    }
  }

  // (With ECI in place,) Write the mode marker
  appendModeInfo(mode, headerBits);

  // Collect data within the main segment, separately, to count its size if needed. Don't add it to
  // main payload yet.
  BitArray dataBits = new BitArray();
  appendBytes(content, mode, dataBits, encoding);

  // Hard part: need to know version to know how many bits length takes. But need to know how many
  // bits it takes to know version. First we take a guess at version by assuming version will be
  // the minimum, 1:

  int provisionalBitsNeeded = headerBits.getSize()
      + mode.getCharacterCountBits(Version.getVersionForNumber(1))
      + dataBits.getSize();
  Version provisionalVersion = chooseVersion(provisionalBitsNeeded, ecLevel);

  // Use that guess to calculate the right version. I am still not sure this works in 100% of cases.

  int bitsNeeded = headerBits.getSize()
      + mode.getCharacterCountBits(provisionalVersion)
      + dataBits.getSize();
  Version version = chooseVersion(bitsNeeded, ecLevel);

  BitArray headerAndDataBits = new BitArray();
  headerAndDataBits.appendBitArray(headerBits);
  // Find "length" of main segment and write it
  int numLetters = mode == Mode.BYTE ? dataBits.getSizeInBytes() : content.length();
  appendLengthInfo(numLetters, version, mode, headerAndDataBits);
  // Put data together into the overall payload
  headerAndDataBits.appendBitArray(dataBits);

  Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel);
  int numDataBytes = version.getTotalCodewords() - ecBlocks.getTotalECCodewords();

  // Terminate the bits properly.
  terminateBits(numDataBytes, headerAndDataBits);

  // Interleave data bits with error correction code.
  BitArray finalBits = interleaveWithECBytes(headerAndDataBits,
                                             version.getTotalCodewords(),
                                             numDataBytes,
                                             ecBlocks.getNumBlocks());

  QRCode qrCode = new QRCode();

  qrCode.setECLevel(ecLevel);
  qrCode.setMode(mode);
  qrCode.setVersion(version);

  //  Choose the mask pattern and set to "qrCode".
  int dimension = version.getDimensionForVersion();
  ByteMatrix matrix = new ByteMatrix(dimension, dimension);
  int maskPattern = chooseMaskPattern(finalBits, ecLevel, version, matrix);
  qrCode.setMaskPattern(maskPattern);

  // Build the matrix and set it to "qrCode".
  MatrixUtil.buildMatrix(finalBits, ecLevel, version, maskPattern, matrix);
  qrCode.setMatrix(matrix);

  return qrCode;
}
 
Example 14
Source File: Encoder.java    From ZXing-Orient with Apache License 2.0 4 votes vote down vote up
public static QRCode encode(String content,
                            ErrorCorrectionLevel ecLevel,
                            Map<EncodeHintType,?> hints) throws WriterException {

  // Determine what character encoding has been specified by the caller, if any
  String encoding = hints == null ? null : (String) hints.get(EncodeHintType.CHARACTER_SET);
  if (encoding == null) {
    encoding = DEFAULT_BYTE_MODE_ENCODING;
  }

  // Pick an encoding mode appropriate for the content. Note that this will not attempt to use
  // multiple modes / segments even if that were more efficient. Twould be nice.
  Mode mode = chooseMode(content, encoding);

  // This will store the header information, like mode and
  // length, as well as "header" segments like an ECI segment.
  BitArray headerBits = new BitArray();

  // Append ECI segment if applicable
  if (mode == Mode.BYTE && !DEFAULT_BYTE_MODE_ENCODING.equals(encoding)) {
    CharacterSetECI eci = CharacterSetECI.getCharacterSetECIByName(encoding);
    if (eci != null) {
      appendECI(eci, headerBits);
    }
  }

  // (With ECI in place,) Write the mode marker
  appendModeInfo(mode, headerBits);

  // Collect data within the main segment, separately, to count its size if needed. Don't add it to
  // main payload yet.
  BitArray dataBits = new BitArray();
  appendBytes(content, mode, dataBits, encoding);

  // Hard part: need to know version to know how many bits length takes. But need to know how many
  // bits it takes to know version. First we take a guess at version by assuming version will be
  // the minimum, 1:

  int provisionalBitsNeeded = headerBits.getSize()
      + mode.getCharacterCountBits(Version.getVersionForNumber(1))
      + dataBits.getSize();
  Version provisionalVersion = chooseVersion(provisionalBitsNeeded, ecLevel);

  // Use that guess to calculate the right version. I am still not sure this works in 100% of cases.

  int bitsNeeded = headerBits.getSize()
      + mode.getCharacterCountBits(provisionalVersion)
      + dataBits.getSize();
  Version version = chooseVersion(bitsNeeded, ecLevel);

  BitArray headerAndDataBits = new BitArray();
  headerAndDataBits.appendBitArray(headerBits);
  // Find "length" of main segment and write it
  int numLetters = mode == Mode.BYTE ? dataBits.getSizeInBytes() : content.length();
  appendLengthInfo(numLetters, version, mode, headerAndDataBits);
  // Put data together into the overall payload
  headerAndDataBits.appendBitArray(dataBits);

  Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel);
  int numDataBytes = version.getTotalCodewords() - ecBlocks.getTotalECCodewords();

  // Terminate the bits properly.
  terminateBits(numDataBytes, headerAndDataBits);

  // Interleave data bits with error correction code.
  BitArray finalBits = interleaveWithECBytes(headerAndDataBits,
                                             version.getTotalCodewords(),
                                             numDataBytes,
                                             ecBlocks.getNumBlocks());

  QRCode qrCode = new QRCode();

  qrCode.setECLevel(ecLevel);
  qrCode.setMode(mode);
  qrCode.setVersion(version);

  //  Choose the mask pattern and set to "qrCode".
  int dimension = version.getDimensionForVersion();
  ByteMatrix matrix = new ByteMatrix(dimension, dimension);
  int maskPattern = chooseMaskPattern(finalBits, ecLevel, version, matrix);
  qrCode.setMaskPattern(maskPattern);

  // Build the matrix and set it to "qrCode".
  MatrixUtil.buildMatrix(finalBits, ecLevel, version, maskPattern, matrix);
  qrCode.setMatrix(matrix);

  return qrCode;
}
 
Example 15
Source File: Encoder.java    From barcodescanner-lib-aar with MIT License 4 votes vote down vote up
public static QRCode encode(String content,
                            ErrorCorrectionLevel ecLevel,
                            Map<EncodeHintType,?> hints) throws WriterException {

  // Determine what character encoding has been specified by the caller, if any
  String encoding = DEFAULT_BYTE_MODE_ENCODING;
  if (hints != null && hints.containsKey(EncodeHintType.CHARACTER_SET)) {
    encoding = hints.get(EncodeHintType.CHARACTER_SET).toString();
  }

  // Pick an encoding mode appropriate for the content. Note that this will not attempt to use
  // multiple modes / segments even if that were more efficient. Twould be nice.
  Mode mode = chooseMode(content, encoding);

  // This will store the header information, like mode and
  // length, as well as "header" segments like an ECI segment.
  BitArray headerBits = new BitArray();

  // Append ECI segment if applicable
  if (mode == Mode.BYTE && !DEFAULT_BYTE_MODE_ENCODING.equals(encoding)) {
    CharacterSetECI eci = CharacterSetECI.getCharacterSetECIByName(encoding);
    if (eci != null) {
      appendECI(eci, headerBits);
    }
  }

  // (With ECI in place,) Write the mode marker
  appendModeInfo(mode, headerBits);

  // Collect data within the main segment, separately, to count its size if needed. Don't add it to
  // main payload yet.
  BitArray dataBits = new BitArray();
  appendBytes(content, mode, dataBits, encoding);

  Version version;
  if (hints != null && hints.containsKey(EncodeHintType.QR_VERSION)) {
    int versionNumber = Integer.parseInt(hints.get(EncodeHintType.QR_VERSION).toString());
    version = Version.getVersionForNumber(versionNumber);
    int bitsNeeded = calculateBitsNeeded(mode, headerBits, dataBits, version);
    if (!willFit(bitsNeeded, version, ecLevel)) {
      throw new WriterException("Data too big for requested version");
    }
  } else {
    version = recommendVersion(ecLevel, mode, headerBits, dataBits);
  }

  BitArray headerAndDataBits = new BitArray();
  headerAndDataBits.appendBitArray(headerBits);
  // Find "length" of main segment and write it
  int numLetters = mode == Mode.BYTE ? dataBits.getSizeInBytes() : content.length();
  appendLengthInfo(numLetters, version, mode, headerAndDataBits);
  // Put data together into the overall payload
  headerAndDataBits.appendBitArray(dataBits);

  Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel);
  int numDataBytes = version.getTotalCodewords() - ecBlocks.getTotalECCodewords();

  // Terminate the bits properly.
  terminateBits(numDataBytes, headerAndDataBits);

  // Interleave data bits with error correction code.
  BitArray finalBits = interleaveWithECBytes(headerAndDataBits,
                                             version.getTotalCodewords(),
                                             numDataBytes,
                                             ecBlocks.getNumBlocks());

  QRCode qrCode = new QRCode();

  qrCode.setECLevel(ecLevel);
  qrCode.setMode(mode);
  qrCode.setVersion(version);

  //  Choose the mask pattern and set to "qrCode".
  int dimension = version.getDimensionForVersion();
  ByteMatrix matrix = new ByteMatrix(dimension, dimension);
  int maskPattern = chooseMaskPattern(finalBits, ecLevel, version, matrix);
  qrCode.setMaskPattern(maskPattern);

  // Build the matrix and set it to "qrCode".
  MatrixUtil.buildMatrix(finalBits, ecLevel, version, maskPattern, matrix);
  qrCode.setMatrix(matrix);

  return qrCode;
}
 
Example 16
Source File: Encoder.java    From reacteu-app with MIT License 4 votes vote down vote up
public static QRCode encode(String content,
                            ErrorCorrectionLevel ecLevel,
                            Map<EncodeHintType,?> hints) throws WriterException {

  // Determine what character encoding has been specified by the caller, if any
  String encoding = hints == null ? null : (String) hints.get(EncodeHintType.CHARACTER_SET);
  if (encoding == null) {
    encoding = DEFAULT_BYTE_MODE_ENCODING;
  }

  // Pick an encoding mode appropriate for the content. Note that this will not attempt to use
  // multiple modes / segments even if that were more efficient. Twould be nice.
  Mode mode = chooseMode(content, encoding);

  // This will store the header information, like mode and
  // length, as well as "header" segments like an ECI segment.
  BitArray headerBits = new BitArray();

  // Append ECI segment if applicable
  if (mode == Mode.BYTE && !DEFAULT_BYTE_MODE_ENCODING.equals(encoding)) {
    CharacterSetECI eci = CharacterSetECI.getCharacterSetECIByName(encoding);
    if (eci != null) {
      appendECI(eci, headerBits);
    }
  }

  // (With ECI in place,) Write the mode marker
  appendModeInfo(mode, headerBits);

  // Collect data within the main segment, separately, to count its size if needed. Don't add it to
  // main payload yet.
  BitArray dataBits = new BitArray();
  appendBytes(content, mode, dataBits, encoding);

  // Hard part: need to know version to know how many bits length takes. But need to know how many
  // bits it takes to know version. First we take a guess at version by assuming version will be
  // the minimum, 1:

  int provisionalBitsNeeded = headerBits.getSize()
      + mode.getCharacterCountBits(Version.getVersionForNumber(1))
      + dataBits.getSize();
  Version provisionalVersion = chooseVersion(provisionalBitsNeeded, ecLevel);

  // Use that guess to calculate the right version. I am still not sure this works in 100% of cases.

  int bitsNeeded = headerBits.getSize()
      + mode.getCharacterCountBits(provisionalVersion)
      + dataBits.getSize();
  Version version = chooseVersion(bitsNeeded, ecLevel);

  BitArray headerAndDataBits = new BitArray();
  headerAndDataBits.appendBitArray(headerBits);
  // Find "length" of main segment and write it
  int numLetters = mode == Mode.BYTE ? dataBits.getSizeInBytes() : content.length();
  appendLengthInfo(numLetters, version, mode, headerAndDataBits);
  // Put data together into the overall payload
  headerAndDataBits.appendBitArray(dataBits);

  Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel);
  int numDataBytes = version.getTotalCodewords() - ecBlocks.getTotalECCodewords();

  // Terminate the bits properly.
  terminateBits(numDataBytes, headerAndDataBits);

  // Interleave data bits with error correction code.
  BitArray finalBits = interleaveWithECBytes(headerAndDataBits,
                                             version.getTotalCodewords(),
                                             numDataBytes,
                                             ecBlocks.getNumBlocks());

  QRCode qrCode = new QRCode();

  qrCode.setECLevel(ecLevel);
  qrCode.setMode(mode);
  qrCode.setVersion(version);

  //  Choose the mask pattern and set to "qrCode".
  int dimension = version.getDimensionForVersion();
  ByteMatrix matrix = new ByteMatrix(dimension, dimension);
  int maskPattern = chooseMaskPattern(finalBits, ecLevel, version, matrix);
  qrCode.setMaskPattern(maskPattern);

  // Build the matrix and set it to "qrCode".
  MatrixUtil.buildMatrix(finalBits, ecLevel, version, maskPattern, matrix);
  qrCode.setMatrix(matrix);

  return qrCode;
}
 
Example 17
Source File: Encoder.java    From QrCodeScanner with GNU General Public License v3.0 4 votes vote down vote up
public static QRCode encode(String content,
                            ErrorCorrectionLevel ecLevel,
                            Map<EncodeHintType,?> hints) throws WriterException {

  // Determine what character encoding has been specified by the caller, if any
  String encoding = DEFAULT_BYTE_MODE_ENCODING;
  boolean hasEncodingHint = hints != null && hints.containsKey(EncodeHintType.CHARACTER_SET);
  if (hasEncodingHint) {
    encoding = hints.get(EncodeHintType.CHARACTER_SET).toString();
  }

  // Pick an encoding mode appropriate for the content. Note that this will not attempt to use
  // multiple modes / segments even if that were more efficient. Twould be nice.
  Mode mode = chooseMode(content, encoding);

  // This will store the header information, like mode and
  // length, as well as "header" segments like an ECI segment.
  BitArray headerBits = new BitArray();

  // Append ECI segment if applicable
  if (mode == Mode.BYTE && (hasEncodingHint || !DEFAULT_BYTE_MODE_ENCODING.equals(encoding))) {
    CharacterSetECI eci = CharacterSetECI.getCharacterSetECIByName(encoding);
    if (eci != null) {
      appendECI(eci, headerBits);
    }
  }

  // (With ECI in place,) Write the mode marker
  appendModeInfo(mode, headerBits);

  // Collect data within the main segment, separately, to count its size if needed. Don't add it to
  // main payload yet.
  BitArray dataBits = new BitArray();
  appendBytes(content, mode, dataBits, encoding);

  Version version;
  if (hints != null && hints.containsKey(EncodeHintType.QR_VERSION)) {
    int versionNumber = Integer.parseInt(hints.get(EncodeHintType.QR_VERSION).toString());
    version = Version.getVersionForNumber(versionNumber);
    int bitsNeeded = calculateBitsNeeded(mode, headerBits, dataBits, version);
    if (!willFit(bitsNeeded, version, ecLevel)) {
      throw new WriterException("Data too big for requested version");
    }
  } else {
    version = recommendVersion(ecLevel, mode, headerBits, dataBits);
  }

  BitArray headerAndDataBits = new BitArray();
  headerAndDataBits.appendBitArray(headerBits);
  // Find "length" of main segment and write it
  int numLetters = mode == Mode.BYTE ? dataBits.getSizeInBytes() : content.length();
  appendLengthInfo(numLetters, version, mode, headerAndDataBits);
  // Put data together into the overall payload
  headerAndDataBits.appendBitArray(dataBits);

  Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel);
  int numDataBytes = version.getTotalCodewords() - ecBlocks.getTotalECCodewords();

  // Terminate the bits properly.
  terminateBits(numDataBytes, headerAndDataBits);

  // Interleave data bits with error correction code.
  BitArray finalBits = interleaveWithECBytes(headerAndDataBits,
                                             version.getTotalCodewords(),
                                             numDataBytes,
                                             ecBlocks.getNumBlocks());

  QRCode qrCode = new QRCode();

  qrCode.setECLevel(ecLevel);
  qrCode.setMode(mode);
  qrCode.setVersion(version);

  //  Choose the mask pattern and set to "qrCode".
  int dimension = version.getDimensionForVersion();
  ByteMatrix matrix = new ByteMatrix(dimension, dimension);
  int maskPattern = chooseMaskPattern(finalBits, ecLevel, version, matrix);
  qrCode.setMaskPattern(maskPattern);

  // Build the matrix and set it to "qrCode".
  MatrixUtil.buildMatrix(finalBits, ecLevel, version, maskPattern, matrix);
  qrCode.setMatrix(matrix);

  return qrCode;
}
 
Example 18
Source File: Encoder.java    From Tesseract-OCR-Scanner with Apache License 2.0 4 votes vote down vote up
public static QRCode encode(String content,
                            ErrorCorrectionLevel ecLevel,
                            Map<EncodeHintType,?> hints) throws WriterException {

  // Determine what character encoding has been specified by the caller, if any
  String encoding = DEFAULT_BYTE_MODE_ENCODING;
  boolean hasEncodingHint = hints != null && hints.containsKey(EncodeHintType.CHARACTER_SET);
  if (hasEncodingHint) {
    encoding = hints.get(EncodeHintType.CHARACTER_SET).toString();
  }

  // Pick an encoding mode appropriate for the content. Note that this will not attempt to use
  // multiple modes / segments even if that were more efficient. Twould be nice.
  Mode mode = chooseMode(content, encoding);

  // This will store the header information, like mode and
  // length, as well as "header" segments like an ECI segment.
  BitArray headerBits = new BitArray();

  // Append ECI segment if applicable
  if (mode == Mode.BYTE && (hasEncodingHint || !DEFAULT_BYTE_MODE_ENCODING.equals(encoding))) {
    CharacterSetECI eci = CharacterSetECI.getCharacterSetECIByName(encoding);
    if (eci != null) {
      appendECI(eci, headerBits);
    }
  }

  // (With ECI in place,) Write the mode marker
  appendModeInfo(mode, headerBits);

  // Collect data within the main segment, separately, to count its size if needed. Don't add it to
  // main payload yet.
  BitArray dataBits = new BitArray();
  appendBytes(content, mode, dataBits, encoding);

  Version version;
  if (hints != null && hints.containsKey(EncodeHintType.QR_VERSION)) {
    int versionNumber = Integer.parseInt(hints.get(EncodeHintType.QR_VERSION).toString());
    version = Version.getVersionForNumber(versionNumber);
    int bitsNeeded = calculateBitsNeeded(mode, headerBits, dataBits, version);
    if (!willFit(bitsNeeded, version, ecLevel)) {
      throw new WriterException("Data too big for requested version");
    }
  } else {
    version = recommendVersion(ecLevel, mode, headerBits, dataBits);
  }

  BitArray headerAndDataBits = new BitArray();
  headerAndDataBits.appendBitArray(headerBits);
  // Find "length" of main segment and write it
  int numLetters = mode == Mode.BYTE ? dataBits.getSizeInBytes() : content.length();
  appendLengthInfo(numLetters, version, mode, headerAndDataBits);
  // Put data together into the overall payload
  headerAndDataBits.appendBitArray(dataBits);

  Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel);
  int numDataBytes = version.getTotalCodewords() - ecBlocks.getTotalECCodewords();

  // Terminate the bits properly.
  terminateBits(numDataBytes, headerAndDataBits);

  // Interleave data bits with error correction code.
  BitArray finalBits = interleaveWithECBytes(headerAndDataBits,
                                             version.getTotalCodewords(),
                                             numDataBytes,
                                             ecBlocks.getNumBlocks());

  QRCode qrCode = new QRCode();

  qrCode.setECLevel(ecLevel);
  qrCode.setMode(mode);
  qrCode.setVersion(version);

  //  Choose the mask pattern and set to "qrCode".
  int dimension = version.getDimensionForVersion();
  ByteMatrix matrix = new ByteMatrix(dimension, dimension);
  int maskPattern = chooseMaskPattern(finalBits, ecLevel, version, matrix);
  qrCode.setMaskPattern(maskPattern);

  // Build the matrix and set it to "qrCode".
  MatrixUtil.buildMatrix(finalBits, ecLevel, version, maskPattern, matrix);
  qrCode.setMatrix(matrix);

  return qrCode;
}
 
Example 19
Source File: Encoder.java    From RipplePower with Apache License 2.0 4 votes vote down vote up
public static QRCode encode(String content, ErrorCorrectionLevel ecLevel, Map<EncodeHintType, ?> hints)
		throws WriterException {

	// Determine what character encoding has been specified by the caller,
	// if any
	String encoding = hints == null ? null : (String) hints.get(EncodeHintType.CHARACTER_SET);
	if (encoding == null) {
		encoding = DEFAULT_BYTE_MODE_ENCODING;
	}

	// Pick an encoding mode appropriate for the content. Note that this
	// will not attempt to use
	// multiple modes / segments even if that were more efficient. Twould be
	// nice.
	Mode mode = chooseMode(content, encoding);

	// This will store the header information, like mode and
	// length, as well as "header" segments like an ECI segment.
	BitArray headerBits = new BitArray();

	// Append ECI segment if applicable
	if (mode == Mode.BYTE && !DEFAULT_BYTE_MODE_ENCODING.equals(encoding)) {
		CharacterSetECI eci = CharacterSetECI.getCharacterSetECIByName(encoding);
		if (eci != null) {
			appendECI(eci, headerBits);
		}
	}

	// (With ECI in place,) Write the mode marker
	appendModeInfo(mode, headerBits);

	// Collect data within the main segment, separately, to count its size
	// if needed. Don't add it to
	// main payload yet.
	BitArray dataBits = new BitArray();
	appendBytes(content, mode, dataBits, encoding);

	// Hard part: need to know version to know how many bits length takes.
	// But need to know how many
	// bits it takes to know version. First we take a guess at version by
	// assuming version will be
	// the minimum, 1:

	int provisionalBitsNeeded = headerBits.getSize() + mode.getCharacterCountBits(Version.getVersionForNumber(1))
			+ dataBits.getSize();
	Version provisionalVersion = chooseVersion(provisionalBitsNeeded, ecLevel);

	// Use that guess to calculate the right version. I am still not sure
	// this works in 100% of cases.

	int bitsNeeded = headerBits.getSize() + mode.getCharacterCountBits(provisionalVersion) + dataBits.getSize();
	Version version = chooseVersion(bitsNeeded, ecLevel);

	BitArray headerAndDataBits = new BitArray();
	headerAndDataBits.appendBitArray(headerBits);
	// Find "length" of main segment and write it
	int numLetters = mode == Mode.BYTE ? dataBits.getSizeInBytes() : content.length();
	appendLengthInfo(numLetters, version, mode, headerAndDataBits);
	// Put data together into the overall payload
	headerAndDataBits.appendBitArray(dataBits);

	Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel);
	int numDataBytes = version.getTotalCodewords() - ecBlocks.getTotalECCodewords();

	// Terminate the bits properly.
	terminateBits(numDataBytes, headerAndDataBits);

	// Interleave data bits with error correction code.
	BitArray finalBits = interleaveWithECBytes(headerAndDataBits, version.getTotalCodewords(), numDataBytes,
			ecBlocks.getNumBlocks());

	QRCode qrCode = new QRCode();

	qrCode.setECLevel(ecLevel);
	qrCode.setMode(mode);
	qrCode.setVersion(version);

	// Choose the mask pattern and set to "qrCode".
	int dimension = version.getDimensionForVersion();
	ByteMatrix matrix = new ByteMatrix(dimension, dimension);
	int maskPattern = chooseMaskPattern(finalBits, ecLevel, version, matrix);
	qrCode.setMaskPattern(maskPattern);

	// Build the matrix and set it to "qrCode".
	MatrixUtil.buildMatrix(finalBits, ecLevel, version, maskPattern, matrix);
	qrCode.setMatrix(matrix);

	return qrCode;
}
 
Example 20
Source File: Encoder.java    From ScreenCapture with MIT License 4 votes vote down vote up
public static QRCode encode(String content,
                            ErrorCorrectionLevel ecLevel,
                            Map<EncodeHintType,?> hints) throws WriterException {

  // Determine what character encoding has been specified by the caller, if any
  String encoding = DEFAULT_BYTE_MODE_ENCODING;
  boolean hasEncodingHint = hints != null && hints.containsKey(EncodeHintType.CHARACTER_SET);
  if (hasEncodingHint) {
    encoding = hints.get(EncodeHintType.CHARACTER_SET).toString();
  }

  // Pick an encoding mode appropriate for the content. Note that this will not attempt to use
  // multiple modes / segments even if that were more efficient. Twould be nice.
  Mode mode = chooseMode(content, encoding);

  // This will store the header information, like mode and
  // length, as well as "header" segments like an ECI segment.
  BitArray headerBits = new BitArray();

  // Append ECI segment if applicable
  if (mode == Mode.BYTE && (hasEncodingHint || !DEFAULT_BYTE_MODE_ENCODING.equals(encoding))) {
    CharacterSetECI eci = CharacterSetECI.getCharacterSetECIByName(encoding);
    if (eci != null) {
      appendECI(eci, headerBits);
    }
  }

  // (With ECI in place,) Write the mode marker
  appendModeInfo(mode, headerBits);

  // Collect data within the main segment, separately, to count its size if needed. Don't add it to
  // main payload yet.
  BitArray dataBits = new BitArray();
  appendBytes(content, mode, dataBits, encoding);

  Version version;
  if (hints != null && hints.containsKey(EncodeHintType.QR_VERSION)) {
    int versionNumber = Integer.parseInt(hints.get(EncodeHintType.QR_VERSION).toString());
    version = Version.getVersionForNumber(versionNumber);
    int bitsNeeded = calculateBitsNeeded(mode, headerBits, dataBits, version);
    if (!willFit(bitsNeeded, version, ecLevel)) {
      throw new WriterException("Data too big for requested version");
    }
  } else {
    version = recommendVersion(ecLevel, mode, headerBits, dataBits);
  }

  BitArray headerAndDataBits = new BitArray();
  headerAndDataBits.appendBitArray(headerBits);
  // Find "length" of main segment and write it
  int numLetters = mode == Mode.BYTE ? dataBits.getSizeInBytes() : content.length();
  appendLengthInfo(numLetters, version, mode, headerAndDataBits);
  // Put data together into the overall payload
  headerAndDataBits.appendBitArray(dataBits);

  Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel);
  int numDataBytes = version.getTotalCodewords() - ecBlocks.getTotalECCodewords();

  // Terminate the bits properly.
  terminateBits(numDataBytes, headerAndDataBits);

  // Interleave data bits with error correction code.
  BitArray finalBits = interleaveWithECBytes(headerAndDataBits,
                                             version.getTotalCodewords(),
                                             numDataBytes,
                                             ecBlocks.getNumBlocks());

  QRCode qrCode = new QRCode();

  qrCode.setECLevel(ecLevel);
  qrCode.setMode(mode);
  qrCode.setVersion(version);

  //  Choose the mask pattern and set to "qrCode".
  int dimension = version.getDimensionForVersion();
  ByteMatrix matrix = new ByteMatrix(dimension, dimension);
  int maskPattern = chooseMaskPattern(finalBits, ecLevel, version, matrix);
  qrCode.setMaskPattern(maskPattern);

  // Build the matrix and set it to "qrCode".
  MatrixUtil.buildMatrix(finalBits, ecLevel, version, maskPattern, matrix);
  qrCode.setMatrix(matrix);

  return qrCode;
}