Search in sources :

Example 31 with BitArray

use of com.google.zxing.common.BitArray in project weex-example by KalicyZhou.

the class Encoder method generateModeMessage.

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;
}
Also used : BitArray(com.google.zxing.common.BitArray)

Example 32 with BitArray

use of com.google.zxing.common.BitArray in project weex-example by KalicyZhou.

the class State method toBitArray.

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;
}
Also used : BitArray(com.google.zxing.common.BitArray) LinkedList(java.util.LinkedList)

Example 33 with BitArray

use of com.google.zxing.common.BitArray in project weex-example by KalicyZhou.

the class Encoder method encode.

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;
}
Also used : Version(com.google.zxing.qrcode.decoder.Version) Mode(com.google.zxing.qrcode.decoder.Mode) BitArray(com.google.zxing.common.BitArray) CharacterSetECI(com.google.zxing.common.CharacterSetECI)

Example 34 with BitArray

use of com.google.zxing.common.BitArray in project weex-example by KalicyZhou.

the class Encoder method interleaveWithECBytes.

/**
   * Interleave "bits" with corresponding error correction bytes. On success, store the result in
   * "result". The interleave rule is complicated. See 8.6 of JISX0510:2004 (p.37) for details.
   */
static BitArray interleaveWithECBytes(BitArray bits, int numTotalBytes, int numDataBytes, int numRSBlocks) throws WriterException {
    // "bits" must have "getNumDataBytes" bytes of data.
    if (bits.getSizeInBytes() != numDataBytes) {
        throw new WriterException("Number of bits and data bytes does not match");
    }
    // Step 1.  Divide data bytes into blocks and generate error correction bytes for them. We'll
    // store the divided data bytes blocks and error correction bytes blocks into "blocks".
    int dataBytesOffset = 0;
    int maxNumDataBytes = 0;
    int maxNumEcBytes = 0;
    // Since, we know the number of reedsolmon blocks, we can initialize the vector with the number.
    Collection<BlockPair> blocks = new ArrayList<>(numRSBlocks);
    for (int i = 0; i < numRSBlocks; ++i) {
        int[] numDataBytesInBlock = new int[1];
        int[] numEcBytesInBlock = new int[1];
        getNumDataBytesAndNumECBytesForBlockID(numTotalBytes, numDataBytes, numRSBlocks, i, numDataBytesInBlock, numEcBytesInBlock);
        int size = numDataBytesInBlock[0];
        byte[] dataBytes = new byte[size];
        bits.toBytes(8 * dataBytesOffset, dataBytes, 0, size);
        byte[] ecBytes = generateECBytes(dataBytes, numEcBytesInBlock[0]);
        blocks.add(new BlockPair(dataBytes, ecBytes));
        maxNumDataBytes = Math.max(maxNumDataBytes, size);
        maxNumEcBytes = Math.max(maxNumEcBytes, ecBytes.length);
        dataBytesOffset += numDataBytesInBlock[0];
    }
    if (numDataBytes != dataBytesOffset) {
        throw new WriterException("Data bytes does not match offset");
    }
    BitArray result = new BitArray();
    // First, place data blocks.
    for (int i = 0; i < maxNumDataBytes; ++i) {
        for (BlockPair block : blocks) {
            byte[] dataBytes = block.getDataBytes();
            if (i < dataBytes.length) {
                result.appendBits(dataBytes[i], 8);
            }
        }
    }
    // Then, place error correction blocks.
    for (int i = 0; i < maxNumEcBytes; ++i) {
        for (BlockPair block : blocks) {
            byte[] ecBytes = block.getErrorCorrectionBytes();
            if (i < ecBytes.length) {
                result.appendBits(ecBytes[i], 8);
            }
        }
    }
    if (numTotalBytes != result.getSizeInBytes()) {
        // Should be same.
        throw new WriterException("Interleaving error: " + numTotalBytes + " and " + result.getSizeInBytes() + " differ.");
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) BitArray(com.google.zxing.common.BitArray) WriterException(com.google.zxing.WriterException)

Example 35 with BitArray

use of com.google.zxing.common.BitArray in project weex-example by KalicyZhou.

the class MatrixUtil method embedTypeInfo.

// Embed type information. On success, modify the matrix.
static void embedTypeInfo(ErrorCorrectionLevel ecLevel, int maskPattern, ByteMatrix matrix) throws WriterException {
    BitArray typeInfoBits = new BitArray();
    makeTypeInfoBits(ecLevel, maskPattern, typeInfoBits);
    for (int i = 0; i < typeInfoBits.getSize(); ++i) {
        // Place bits in LSB to MSB order.  LSB (least significant bit) is the last value in
        // "typeInfoBits".
        boolean bit = typeInfoBits.get(typeInfoBits.getSize() - 1 - i);
        // Type info bits at the left top corner. See 8.9 of JISX0510:2004 (p.46).
        int x1 = TYPE_INFO_COORDINATES[i][0];
        int y1 = TYPE_INFO_COORDINATES[i][1];
        matrix.set(x1, y1, bit);
        if (i < 8) {
            // Right top corner.
            int x2 = matrix.getWidth() - i - 1;
            int y2 = 8;
            matrix.set(x2, y2, bit);
        } else {
            // Left bottom corner.
            int x2 = 8;
            int y2 = matrix.getHeight() - 7 + (i - 8);
            matrix.set(x2, y2, bit);
        }
    }
}
Also used : BitArray(com.google.zxing.common.BitArray)

Aggregations

BitArray (com.google.zxing.common.BitArray)68 Test (org.junit.Test)28 BinaryBitmap (com.google.zxing.BinaryBitmap)8 Result (com.google.zxing.Result)8 BufferedImageLuminanceSource (com.google.zxing.BufferedImageLuminanceSource)7 WriterException (com.google.zxing.WriterException)7 GlobalHistogramBinarizer (com.google.zxing.common.GlobalHistogramBinarizer)7 BufferedImage (java.awt.image.BufferedImage)7 ResultPoint (com.google.zxing.ResultPoint)6 NotFoundException (com.google.zxing.NotFoundException)5 ReaderException (com.google.zxing.ReaderException)5 BitMatrix (com.google.zxing.common.BitMatrix)4 FinderPattern (com.google.zxing.oned.rss.FinderPattern)4 ArrayList (java.util.ArrayList)4 AbstractExpandedDecoder (com.google.zxing.oned.rss.expanded.decoders.AbstractExpandedDecoder)3 Path (java.nio.file.Path)3 DecodeHintType (com.google.zxing.DecodeHintType)2 CharacterSetECI (com.google.zxing.common.CharacterSetECI)2 ReedSolomonEncoder (com.google.zxing.common.reedsolomon.ReedSolomonEncoder)2 DataCharacter (com.google.zxing.oned.rss.DataCharacter)2