Search in sources :

Example 1 with AztecCode

use of com.google.zxing.aztec.encoder.AztecCode 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)

Aggregations

NotFoundException (com.google.zxing.NotFoundException)1 AztecDetectorResult (com.google.zxing.aztec.AztecDetectorResult)1 Decoder (com.google.zxing.aztec.decoder.Decoder)1 Point (com.google.zxing.aztec.detector.Detector.Point)1 AztecCode (com.google.zxing.aztec.encoder.AztecCode)1 BitMatrix (com.google.zxing.common.BitMatrix)1 DecoderResult (com.google.zxing.common.DecoderResult)1 Random (java.util.Random)1 TreeSet (java.util.TreeSet)1