Search in sources :

Example 61 with BitArray

use of com.google.zxing.common.BitArray in project zxing by zxing.

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 62 with BitArray

use of com.google.zxing.common.BitArray in project zxing by zxing.

the class MatrixUtil method makeTypeInfoBits.

// Make bit vector of type information. On success, store the result in "bits" and return true.
// Encode error correction level and mask pattern. See 8.9 of
// JISX0510:2004 (p.45) for details.
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());
    }
}
Also used : BitArray(com.google.zxing.common.BitArray) WriterException(com.google.zxing.WriterException)

Example 63 with BitArray

use of com.google.zxing.common.BitArray in project zxing by zxing.

the class EncoderTest method testModeMessage.

private static void testModeMessage(boolean compact, int layers, int words, String expected) {
    BitArray in = Encoder.generateModeMessage(compact, layers, words);
    assertEquals("generateModeMessage() failed", stripSpace(expected), stripSpace(in.toString()));
}
Also used : BitArray(com.google.zxing.common.BitArray)

Example 64 with BitArray

use of com.google.zxing.common.BitArray in project zxing by zxing.

the class EncoderTest method testHighLevelEncodeString.

private static void testHighLevelEncodeString(String s, int expectedReceivedBits) {
    BitArray bits = new HighLevelEncoder(s.getBytes(StandardCharsets.ISO_8859_1)).encode();
    int receivedBitCount = stripSpace(bits.toString()).length();
    assertEquals("highLevelEncode() failed for input string: " + s, expectedReceivedBits, receivedBitCount);
    assertEquals(s, Decoder.highLevelDecode(toBooleanArray(bits)));
}
Also used : BitArray(com.google.zxing.common.BitArray) ResultPoint(com.google.zxing.ResultPoint)

Example 65 with BitArray

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

the class MatrixUtil method makeTypeInfoBits.

// Make bit vector of type information. On success, store the result in "bits" and return true.
// Encode error correction level and mask pattern. See 8.9 of
// JISX0510:2004 (p.45) for details.
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());
    }
}
Also used : BitArray(com.google.zxing.common.BitArray) WriterException(com.google.zxing.WriterException)

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