Search in sources :

Example 96 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 97 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)

Example 98 with BitMatrix

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

the class DetectorTest method testErrorInParameterLocator.

// Test that we can tolerate errors in the parameter locator bits
private static void testErrorInParameterLocator(String data) throws Exception {
    AztecCode aztec = Encoder.encode(data.getBytes(StandardCharsets.ISO_8859_1), 25, Encoder.DEFAULT_AZTEC_LAYERS);
    // pseudo-random, but deterministic
    Random random = new Random(aztec.getMatrix().hashCode());
    int layers = aztec.getLayers();
    boolean compact = aztec.isCompact();
    List<Point> orientationPoints = getOrientationPoints(aztec);
    for (boolean isMirror : new boolean[] { false, true }) {
        for (BitMatrix matrix : getRotations(aztec.getMatrix())) {
            // Systematically try every possible 1- and 2-bit error.
            for (int error1 = 0; error1 < orientationPoints.size(); error1++) {
                for (int error2 = error1; error2 < orientationPoints.size(); error2++) {
                    BitMatrix copy = isMirror ? transpose(matrix) : clone(matrix);
                    copy.flip(orientationPoints.get(error1).getX(), orientationPoints.get(error1).getY());
                    if (error2 > error1) {
                        // if error2 == error1, we only test a single error
                        copy.flip(orientationPoints.get(error2).getX(), orientationPoints.get(error2).getY());
                    }
                    // The detector doesn't seem to work when matrix bits are only 1x1.  So magnify.
                    AztecDetectorResult r = new Detector(makeLarger(copy, 3)).detect(isMirror);
                    assertNotNull(r);
                    assertEquals(r.getNbLayers(), layers);
                    assertEquals(r.isCompact(), compact);
                    DecoderResult res = new Decoder().decode(r);
                    assertEquals(data, res.getText());
                }
            }
            // Try a few random three-bit errors;
            for (int i = 0; i < 5; i++) {
                BitMatrix copy = clone(matrix);
                Collection<Integer> errors = new TreeSet<>();
                while (errors.size() < 3) {
                    // Quick and dirty way of getting three distinct integers between 1 and n.
                    errors.add(random.nextInt(orientationPoints.size()));
                }
                for (int error : errors) {
                    copy.flip(orientationPoints.get(error).getX(), orientationPoints.get(error).getY());
                }
                try {
                    new Detector(makeLarger(copy, 3)).detect(false);
                    fail("Should not reach here");
                } catch (NotFoundException expected) {
                // continue
                }
            }
        }
    }
}
Also used : AztecCode(com.google.zxing.aztec.encoder.AztecCode) NotFoundException(com.google.zxing.NotFoundException) Point(com.google.zxing.aztec.detector.Detector.Point) BitMatrix(com.google.zxing.common.BitMatrix) Decoder(com.google.zxing.aztec.decoder.Decoder) Point(com.google.zxing.aztec.detector.Detector.Point) Random(java.util.Random) TreeSet(java.util.TreeSet) DecoderResult(com.google.zxing.common.DecoderResult) AztecDetectorResult(com.google.zxing.aztec.AztecDetectorResult)

Example 99 with BitMatrix

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

the class DetectorTest method getRotations.

// Returns a list of the four rotations of the BitMatrix.
private static Iterable<BitMatrix> getRotations(BitMatrix matrix0) {
    BitMatrix matrix90 = rotateRight(matrix0);
    BitMatrix matrix180 = rotateRight(matrix90);
    BitMatrix matrix270 = rotateRight(matrix180);
    return Arrays.asList(matrix0, matrix90, matrix180, matrix270);
}
Also used : BitMatrix(com.google.zxing.common.BitMatrix)

Example 100 with BitMatrix

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

the class DetectorTest method makeLarger.

// Zooms a bit matrix so that each bit is factor x factor
private static BitMatrix makeLarger(BitMatrix input, int factor) {
    int width = input.getWidth();
    BitMatrix output = new BitMatrix(width * factor);
    for (int inputY = 0; inputY < width; inputY++) {
        for (int inputX = 0; inputX < width; inputX++) {
            if (input.get(inputX, inputY)) {
                output.setRegion(inputX * factor, inputY * factor, factor, factor);
            }
        }
    }
    return output;
}
Also used : BitMatrix(com.google.zxing.common.BitMatrix) Point(com.google.zxing.aztec.detector.Detector.Point)

Aggregations

BitMatrix (com.google.zxing.common.BitMatrix)114 ResultPoint (com.google.zxing.ResultPoint)26 EncodeHintType (com.google.zxing.EncodeHintType)23 Test (org.junit.Test)20 Bitmap (android.graphics.Bitmap)15 QRCodeWriter (com.google.zxing.qrcode.QRCodeWriter)14 DecoderResult (com.google.zxing.common.DecoderResult)12 WriterException (com.google.zxing.WriterException)10 DetectorResult (com.google.zxing.common.DetectorResult)10 MultiFormatWriter (com.google.zxing.MultiFormatWriter)9 Hashtable (java.util.Hashtable)9 AztecDetectorResult (com.google.zxing.aztec.AztecDetectorResult)8 EnumMap (java.util.EnumMap)8 Result (com.google.zxing.Result)7 ArrayList (java.util.ArrayList)6 Point (com.google.zxing.aztec.detector.Detector.Point)5 SymbolShapeHint (com.google.zxing.datamatrix.encoder.SymbolShapeHint)5 NotFoundException (com.google.zxing.NotFoundException)4 BitArray (com.google.zxing.common.BitArray)4 FinderPatternInfo (com.google.zxing.qrcode.detector.FinderPatternInfo)4