Search in sources :

Example 6 with BitMatrix

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

the class DataMaskTestCase method testMask.

private static void testMask(DataMask mask, int dimension, MaskCondition condition) {
    BitMatrix bits = new BitMatrix(dimension);
    mask.unmaskBitMatrix(bits, dimension);
    for (int i = 0; i < dimension; i++) {
        for (int j = 0; j < dimension; j++) {
            assertEquals("(" + i + ',' + j + ')', condition.isMasked(i, j), bits.get(j, i));
        }
    }
}
Also used : BitMatrix(com.google.zxing.common.BitMatrix)

Example 7 with BitMatrix

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

the class Detector method detect.

/**
   * Detects an Aztec Code in an image.
   *
   * @param isMirror if true, image is a mirror-image of original
   * @return {@link AztecDetectorResult} encapsulating results of detecting an Aztec Code
   * @throws NotFoundException if no Aztec Code can be found
   */
public AztecDetectorResult detect(boolean isMirror) throws NotFoundException {
    // 1. Get the center of the aztec matrix
    Point pCenter = getMatrixCenter();
    // 2. Get the center points of the four diagonal points just outside the bull's eye
    //  [topRight, bottomRight, bottomLeft, topLeft]
    ResultPoint[] bullsEyeCorners = getBullsEyeCorners(pCenter);
    if (isMirror) {
        ResultPoint temp = bullsEyeCorners[0];
        bullsEyeCorners[0] = bullsEyeCorners[2];
        bullsEyeCorners[2] = temp;
    }
    // 3. Get the size of the matrix and other parameters from the bull's eye
    extractParameters(bullsEyeCorners);
    // 4. Sample the grid
    BitMatrix bits = sampleGrid(image, bullsEyeCorners[shift % 4], bullsEyeCorners[(shift + 1) % 4], bullsEyeCorners[(shift + 2) % 4], bullsEyeCorners[(shift + 3) % 4]);
    // 5. Get the corners of the matrix.
    ResultPoint[] corners = getMatrixCornerPoints(bullsEyeCorners);
    return new AztecDetectorResult(bits, corners, compact, nbDataBlocks, nbLayers);
}
Also used : ResultPoint(com.google.zxing.ResultPoint) ResultPoint(com.google.zxing.ResultPoint) BitMatrix(com.google.zxing.common.BitMatrix) AztecDetectorResult(com.google.zxing.aztec.AztecDetectorResult)

Example 8 with BitMatrix

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

the class Encoder method encode.

/**
   * Encodes the given binary content as an Aztec symbol
   * 
   * @param data input data string
   * @param minECCPercent minimal percentage of error check words (According to ISO/IEC 24778:2008,
   *                      a minimum of 23% + 3 words is recommended)
   * @param userSpecifiedLayers if non-zero, a user-specified value for the number of layers
   * @return Aztec symbol matrix with metadata
   */
public static AztecCode encode(byte[] data, int minECCPercent, int userSpecifiedLayers) {
    // High-level encode
    BitArray bits = new HighLevelEncoder(data).encode();
    // stuff bits and choose symbol size
    int eccBits = bits.getSize() * minECCPercent / 100 + 11;
    int totalSizeBits = bits.getSize() + eccBits;
    boolean compact;
    int layers;
    int totalBitsInLayer;
    int wordSize;
    BitArray stuffedBits;
    if (userSpecifiedLayers != DEFAULT_AZTEC_LAYERS) {
        compact = userSpecifiedLayers < 0;
        layers = Math.abs(userSpecifiedLayers);
        if (layers > (compact ? MAX_NB_BITS_COMPACT : MAX_NB_BITS)) {
            throw new IllegalArgumentException(String.format("Illegal value %s for layers", userSpecifiedLayers));
        }
        totalBitsInLayer = totalBitsInLayer(layers, compact);
        wordSize = WORD_SIZE[layers];
        int usableBitsInLayers = totalBitsInLayer - (totalBitsInLayer % wordSize);
        stuffedBits = stuffBits(bits, wordSize);
        if (stuffedBits.getSize() + eccBits > usableBitsInLayers) {
            throw new IllegalArgumentException("Data to large for user specified layer");
        }
        if (compact && stuffedBits.getSize() > wordSize * 64) {
            // Compact format only allows 64 data words, though C4 can hold more words than that
            throw new IllegalArgumentException("Data to large for user specified layer");
        }
    } else {
        wordSize = 0;
        stuffedBits = null;
        // is the same size, but has more data.
        for (int i = 0; ; i++) {
            if (i > MAX_NB_BITS) {
                throw new IllegalArgumentException("Data too large for an Aztec code");
            }
            compact = i <= 3;
            layers = compact ? i + 1 : i;
            totalBitsInLayer = totalBitsInLayer(layers, compact);
            if (totalSizeBits > totalBitsInLayer) {
                continue;
            }
            // wordSize has changed
            if (wordSize != WORD_SIZE[layers]) {
                wordSize = WORD_SIZE[layers];
                stuffedBits = stuffBits(bits, wordSize);
            }
            int usableBitsInLayers = totalBitsInLayer - (totalBitsInLayer % wordSize);
            if (compact && stuffedBits.getSize() > wordSize * 64) {
                // Compact format only allows 64 data words, though C4 can hold more words than that
                continue;
            }
            if (stuffedBits.getSize() + eccBits <= usableBitsInLayers) {
                break;
            }
        }
    }
    BitArray messageBits = generateCheckWords(stuffedBits, totalBitsInLayer, wordSize);
    // generate mode message
    int messageSizeInWords = stuffedBits.getSize() / wordSize;
    BitArray modeMessage = generateModeMessage(compact, layers, messageSizeInWords);
    // allocate symbol
    // not including alignment lines
    int baseMatrixSize = (compact ? 11 : 14) + layers * 4;
    int[] alignmentMap = new int[baseMatrixSize];
    int matrixSize;
    if (compact) {
        // no alignment marks in compact mode, alignmentMap is a no-op
        matrixSize = baseMatrixSize;
        for (int i = 0; i < alignmentMap.length; i++) {
            alignmentMap[i] = i;
        }
    } else {
        matrixSize = baseMatrixSize + 1 + 2 * ((baseMatrixSize / 2 - 1) / 15);
        int origCenter = baseMatrixSize / 2;
        int center = matrixSize / 2;
        for (int i = 0; i < origCenter; i++) {
            int newOffset = i + i / 15;
            alignmentMap[origCenter - i - 1] = center - newOffset - 1;
            alignmentMap[origCenter + i] = center + newOffset + 1;
        }
    }
    BitMatrix matrix = new BitMatrix(matrixSize);
    // draw data bits
    for (int i = 0, rowOffset = 0; i < layers; i++) {
        int rowSize = (layers - i) * 4 + (compact ? 9 : 12);
        for (int j = 0; j < rowSize; j++) {
            int columnOffset = j * 2;
            for (int k = 0; k < 2; k++) {
                if (messageBits.get(rowOffset + columnOffset + k)) {
                    matrix.set(alignmentMap[i * 2 + k], alignmentMap[i * 2 + j]);
                }
                if (messageBits.get(rowOffset + rowSize * 2 + columnOffset + k)) {
                    matrix.set(alignmentMap[i * 2 + j], alignmentMap[baseMatrixSize - 1 - i * 2 - k]);
                }
                if (messageBits.get(rowOffset + rowSize * 4 + columnOffset + k)) {
                    matrix.set(alignmentMap[baseMatrixSize - 1 - i * 2 - k], alignmentMap[baseMatrixSize - 1 - i * 2 - j]);
                }
                if (messageBits.get(rowOffset + rowSize * 6 + columnOffset + k)) {
                    matrix.set(alignmentMap[baseMatrixSize - 1 - i * 2 - j], alignmentMap[i * 2 + k]);
                }
            }
        }
        rowOffset += rowSize * 8;
    }
    // draw mode message
    drawModeMessage(matrix, compact, matrixSize, modeMessage);
    // draw alignment marks
    if (compact) {
        drawBullsEye(matrix, matrixSize / 2, 5);
    } else {
        drawBullsEye(matrix, matrixSize / 2, 7);
        for (int i = 0, j = 0; i < baseMatrixSize / 2 - 1; i += 15, j += 16) {
            for (int k = (matrixSize / 2) & 1; k < matrixSize; k += 2) {
                matrix.set(matrixSize / 2 - j, k);
                matrix.set(matrixSize / 2 + j, k);
                matrix.set(k, matrixSize / 2 - j);
                matrix.set(k, matrixSize / 2 + j);
            }
        }
    }
    AztecCode aztec = new AztecCode();
    aztec.setCompact(compact);
    aztec.setSize(matrixSize);
    aztec.setLayers(layers);
    aztec.setCodeWords(messageSizeInWords);
    aztec.setMatrix(matrix);
    return aztec;
}
Also used : BitArray(com.google.zxing.common.BitArray) BitMatrix(com.google.zxing.common.BitMatrix)

Example 9 with BitMatrix

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

the class AztecWriter method renderResult.

private static BitMatrix renderResult(AztecCode code, int width, int height) {
    BitMatrix input = code.getMatrix();
    if (input == null) {
        throw new IllegalStateException();
    }
    int inputWidth = input.getWidth();
    int inputHeight = input.getHeight();
    int outputWidth = Math.max(width, inputWidth);
    int outputHeight = Math.max(height, inputHeight);
    int multiple = Math.min(outputWidth / inputWidth, outputHeight / inputHeight);
    int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
    int topPadding = (outputHeight - (inputHeight * multiple)) / 2;
    BitMatrix output = new BitMatrix(outputWidth, outputHeight);
    for (int inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) {
        // Write the contents of this row of the barcode
        for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {
            if (input.get(inputX, inputY)) {
                output.setRegion(outputX, outputY, multiple, multiple);
            }
        }
    }
    return output;
}
Also used : BitMatrix(com.google.zxing.common.BitMatrix)

Example 10 with BitMatrix

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

the class Decoder method decode.

public DecoderResult decode(AztecDetectorResult detectorResult) throws FormatException {
    ddata = detectorResult;
    BitMatrix matrix = detectorResult.getBits();
    boolean[] rawbits = extractBits(matrix);
    boolean[] correctedBits = correctBits(rawbits);
    byte[] rawBytes = convertBoolArrayToByteArray(correctedBits);
    String result = getEncodedData(correctedBits);
    DecoderResult decoderResult = new DecoderResult(rawBytes, result, null, null);
    decoderResult.setNumBits(correctedBits.length);
    return decoderResult;
}
Also used : DecoderResult(com.google.zxing.common.DecoderResult) BitMatrix(com.google.zxing.common.BitMatrix)

Aggregations

BitMatrix (com.google.zxing.common.BitMatrix)119 EncodeHintType (com.google.zxing.EncodeHintType)27 ResultPoint (com.google.zxing.ResultPoint)26 Test (org.junit.Test)20 Bitmap (android.graphics.Bitmap)18 QRCodeWriter (com.google.zxing.qrcode.QRCodeWriter)17 WriterException (com.google.zxing.WriterException)14 DecoderResult (com.google.zxing.common.DecoderResult)12 EnumMap (java.util.EnumMap)11 MultiFormatWriter (com.google.zxing.MultiFormatWriter)10 DetectorResult (com.google.zxing.common.DetectorResult)10 Hashtable (java.util.Hashtable)10 AztecDetectorResult (com.google.zxing.aztec.AztecDetectorResult)8 Result (com.google.zxing.Result)7 Point (com.google.zxing.aztec.detector.Detector.Point)5 SymbolShapeHint (com.google.zxing.datamatrix.encoder.SymbolShapeHint)5 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 NotFoundException (com.google.zxing.NotFoundException)4