Search in sources :

Example 91 with BitMatrix

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

the class BitMatrixParser method readCodewords.

/**
   * <p>Reads the bits in the {@link BitMatrix} representing the finder pattern in the
   * correct order in order to reconstruct the codewords bytes contained within the
   * QR Code.</p>
   *
   * @return bytes encoded within the QR Code
   * @throws FormatException if the exact number of bytes expected is not read
   */
byte[] readCodewords() throws FormatException {
    FormatInformation formatInfo = readFormatInformation();
    Version version = readVersion();
    // Get the data mask for the format used in this QR Code. This will exclude
    // some bits from reading as we wind through the bit matrix.
    DataMask dataMask = DataMask.values()[formatInfo.getDataMask()];
    int dimension = bitMatrix.getHeight();
    dataMask.unmaskBitMatrix(bitMatrix, dimension);
    BitMatrix functionPattern = version.buildFunctionPattern();
    boolean readingUp = true;
    byte[] result = new byte[version.getTotalCodewords()];
    int resultOffset = 0;
    int currentByte = 0;
    int bitsRead = 0;
    // Read columns in pairs, from right to left
    for (int j = dimension - 1; j > 0; j -= 2) {
        if (j == 6) {
            // Skip whole column with vertical alignment pattern;
            // saves time and makes the other code proceed more cleanly
            j--;
        }
        // Read alternatingly from bottom to top then top to bottom
        for (int count = 0; count < dimension; count++) {
            int i = readingUp ? dimension - 1 - count : count;
            for (int col = 0; col < 2; col++) {
                // Ignore bits covered by the function pattern
                if (!functionPattern.get(j - col, i)) {
                    // Read a bit
                    bitsRead++;
                    currentByte <<= 1;
                    if (bitMatrix.get(j - col, i)) {
                        currentByte |= 1;
                    }
                    // If we've made a whole byte, save it off
                    if (bitsRead == 8) {
                        result[resultOffset++] = (byte) currentByte;
                        bitsRead = 0;
                        currentByte = 0;
                    }
                }
            }
        }
        // readingUp = !readingUp; // switch directions
        readingUp ^= true;
    }
    if (resultOffset != version.getTotalCodewords()) {
        throw FormatException.getFormatInstance();
    }
    return result;
}
Also used : BitMatrix(com.google.zxing.common.BitMatrix)

Example 92 with BitMatrix

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

the class AlignmentPatternFinder method crossCheckVertical.

/**
   * <p>After a horizontal scan finds a potential alignment pattern, this method
   * "cross-checks" by scanning down vertically through the center of the possible
   * alignment pattern to see if the same proportion is detected.</p>
   *
   * @param startI row where an alignment pattern was detected
   * @param centerJ center of the section that appears to cross an alignment pattern
   * @param maxCount maximum reasonable number of modules that should be
   * observed in any reading state, based on the results of the horizontal scan
   * @return vertical center of alignment pattern, or {@link Float#NaN} if not found
   */
private float crossCheckVertical(int startI, int centerJ, int maxCount, int originalStateCountTotal) {
    BitMatrix image = this.image;
    int maxI = image.getHeight();
    int[] stateCount = crossCheckStateCount;
    stateCount[0] = 0;
    stateCount[1] = 0;
    stateCount[2] = 0;
    // Start counting up from center
    int i = startI;
    while (i >= 0 && image.get(centerJ, i) && stateCount[1] <= maxCount) {
        stateCount[1]++;
        i--;
    }
    // If already too many modules in this state or ran off the edge:
    if (i < 0 || stateCount[1] > maxCount) {
        return Float.NaN;
    }
    while (i >= 0 && !image.get(centerJ, i) && stateCount[0] <= maxCount) {
        stateCount[0]++;
        i--;
    }
    if (stateCount[0] > maxCount) {
        return Float.NaN;
    }
    // Now also count down from center
    i = startI + 1;
    while (i < maxI && image.get(centerJ, i) && stateCount[1] <= maxCount) {
        stateCount[1]++;
        i++;
    }
    if (i == maxI || stateCount[1] > maxCount) {
        return Float.NaN;
    }
    while (i < maxI && !image.get(centerJ, i) && stateCount[2] <= maxCount) {
        stateCount[2]++;
        i++;
    }
    if (stateCount[2] > maxCount) {
        return Float.NaN;
    }
    int stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2];
    if (5 * Math.abs(stateCountTotal - originalStateCountTotal) >= 2 * originalStateCountTotal) {
        return Float.NaN;
    }
    return foundPatternCross(stateCount) ? centerFromEnd(stateCount, i) : Float.NaN;
}
Also used : BitMatrix(com.google.zxing.common.BitMatrix)

Example 93 with BitMatrix

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

the class UPCEWriter method main.

public static void main(String[] args) throws Exception {
    BitMatrix bm = new UPCEWriter().encode("12345670", BarcodeFormat.UPC_E, 200, 100, null);
    System.out.println(bm);
}
Also used : BitMatrix(com.google.zxing.common.BitMatrix)

Example 94 with BitMatrix

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

the class DecoderTest method testAztecResult.

@Test
public void testAztecResult() throws FormatException {
    BitMatrix matrix = BitMatrix.parse("X X X X X     X X X       X X X     X X X     \n" + "X X X     X X X     X X X X     X X X     X X \n" + "  X   X X       X   X   X X X X     X     X X \n" + "  X   X X     X X     X     X   X       X   X \n" + "  X X   X X         X               X X     X \n" + "  X X   X X X X X X X X X X X X X X X     X   \n" + "  X X X X X                       X   X X X   \n" + "  X   X   X   X X X X X X X X X   X X X   X X \n" + "  X   X X X   X               X   X X       X \n" + "  X X   X X   X   X X X X X   X   X X X X   X \n" + "  X X   X X   X   X       X   X   X   X X X   \n" + "  X   X   X   X   X   X   X   X   X   X   X   \n" + "  X X X   X   X   X       X   X   X X   X X   \n" + "  X X X X X   X   X X X X X   X   X X X   X X \n" + "X X   X X X   X               X   X   X X   X \n" + "  X       X   X X X X X X X X X   X   X     X \n" + "  X X   X X                       X X   X X   \n" + "  X X X   X X X X X X X X X X X X X X   X X   \n" + "X     X     X     X X   X X               X X \n" + "X   X X X X X   X X X X X     X   X   X     X \n" + "X X X   X X X X           X X X       X     X \n" + "X X     X X X     X X X X     X X X     X X   \n" + "    X X X     X X X       X X X     X X X X   \n", "X ", "  ");
    AztecDetectorResult r = new AztecDetectorResult(matrix, NO_POINTS, false, 30, 2);
    DecoderResult result = new Decoder().decode(r);
    assertEquals("88888TTTTTTTTTTTTTTTTTTTTTTTTTTTTTT", result.getText());
    assertArrayEquals(new byte[] { -11, 85, 85, 117, 107, 90, -42, -75, -83, 107, 90, -42, -75, -83, 107, 90, -42, -75, -83, 107, 90, -42, -80 }, result.getRawBytes());
    assertEquals(180, result.getNumBits());
}
Also used : DecoderResult(com.google.zxing.common.DecoderResult) BitMatrix(com.google.zxing.common.BitMatrix) AztecDetectorResult(com.google.zxing.aztec.AztecDetectorResult) Test(org.junit.Test)

Example 95 with BitMatrix

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

the class DecoderTest method testDecodeTooManyErrors2.

@Test(expected = FormatException.class)
public void testDecodeTooManyErrors2() throws FormatException {
    BitMatrix matrix = BitMatrix.parse("" + ". X X . . X . X X . . . X . . X X X . . . X X . X X . \n" + "X X . X X . . X . . . X X . . . X X . X X X . X . X X \n" + ". . . . X . . . X X X . X X . X X X X . X X . . X . . \n" + "X . X X . . X . . . X X . X X . X . X X . . . . . X . \n" + "X X . X . . X . X X . . . . . X X . . . . . X . . . X \n" + "X . . X . . . . . . X . . . X . X X X X X X X . . . X \n" + "X . . X X . . X . . X X . . . . . X . . . . . X X X . \n" + ". . X X X X . X . . . . . X X X X X X . . . . . . X X \n" + "X . . . X . X X X X X X . . X X X . X . X X X X X X . \n" + "X . . X X X . X X X X X X X X X X X X X . . . X . X X \n" + ". . . . X X . . . X . . . . . . . X X . . . X X . X . \n" + ". . . X X X . . X X . X X X X X . X . . X . . . . . . \n" + "X . . . . X . X . X . X . . . X . X . X X . X X . X X \n" + "X . X . . X . X . X . X . X . X . X . . . . . X . X X \n" + "X . X X X . . X . X . X . . . X . X . X X X . . . X X \n" + "X X X X X X X X . X . X X X X X . X . X . X . X X X . \n" + ". . . . . . . X . X . . . . . . . X X X X . . . X X X \n" + "X X . . X . . X . X X X X X X X X X X X X X . . X . X \n" + "X X X . X X X X . . X X X X . . X . . . . X . . X X X \n" + ". . X X X X X . X . . . . X X X X . . X X X . X . X . \n" + ". . X X . X . X . . . X . X X . X X . . . . X X . . . \n" + "X . . . X . X . X X X X X X . . X . X X X X X . X . . \n" + ". X . . . X X X . . . . . X . . . . . X X X X X . X . \n" + "X . . X . X X X X . X . X . . . . X . X X . X . . X . \n" + "X . . . X X . X . X X X X X X X X . X X X X . . X X . \n" + ". X X X X . . X . . X X X . X X . . X . . . . X X X . \n" + "X X . . . X X . . X . X . . . . X X . X . . X . X . X \n", "X ", ". ");
    AztecDetectorResult r = new AztecDetectorResult(matrix, NO_POINTS, true, 16, 4);
    new Decoder().decode(r);
}
Also used : BitMatrix(com.google.zxing.common.BitMatrix) AztecDetectorResult(com.google.zxing.aztec.AztecDetectorResult) Test(org.junit.Test)

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