Search in sources :

Example 1 with AztecWriter

use of com.google.zxing.aztec.AztecWriter in project zxing by zxing.

the class EncoderTest method testWriter.

private static void testWriter(String data, String charset, int eccPercent, boolean compact, int layers) throws FormatException {
    // 1. Perform an encode-decode round-trip because it can be lossy.
    // 2. Aztec Decoder currently always decodes the data with a LATIN-1 charset:
    String expectedData = new String(data.getBytes(Charset.forName(charset)), StandardCharsets.ISO_8859_1);
    Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
    hints.put(EncodeHintType.CHARACTER_SET, charset);
    hints.put(EncodeHintType.ERROR_CORRECTION, eccPercent);
    AztecWriter writer = new AztecWriter();
    BitMatrix matrix = writer.encode(data, BarcodeFormat.AZTEC, 0, 0, hints);
    AztecCode aztec = Encoder.encode(data.getBytes(Charset.forName(charset)), eccPercent, Encoder.DEFAULT_AZTEC_LAYERS);
    assertEquals("Unexpected symbol format (compact)", compact, aztec.isCompact());
    assertEquals("Unexpected nr. of layers", layers, aztec.getLayers());
    BitMatrix matrix2 = aztec.getMatrix();
    assertEquals(matrix, matrix2);
    AztecDetectorResult r = new AztecDetectorResult(matrix, NO_POINTS, aztec.isCompact(), aztec.getCodeWords(), aztec.getLayers());
    DecoderResult res = new Decoder().decode(r);
    assertEquals(expectedData, res.getText());
    // Check error correction by introducing up to eccPercent/2 errors
    int ecWords = aztec.getCodeWords() * eccPercent / 100 / 2;
    Random random = getPseudoRandom();
    for (int i = 0; i < ecWords; i++) {
        // don't touch the core
        int x = random.nextBoolean() ? random.nextInt(aztec.getLayers() * 2) : matrix.getWidth() - 1 - random.nextInt(aztec.getLayers() * 2);
        int y = random.nextBoolean() ? random.nextInt(aztec.getLayers() * 2) : matrix.getHeight() - 1 - random.nextInt(aztec.getLayers() * 2);
        matrix.flip(x, y);
    }
    r = new AztecDetectorResult(matrix, NO_POINTS, aztec.isCompact(), aztec.getCodeWords(), aztec.getLayers());
    res = new Decoder().decode(r);
    assertEquals(expectedData, res.getText());
}
Also used : BitMatrix(com.google.zxing.common.BitMatrix) AztecWriter(com.google.zxing.aztec.AztecWriter) Decoder(com.google.zxing.aztec.decoder.Decoder) ResultPoint(com.google.zxing.ResultPoint) EncodeHintType(com.google.zxing.EncodeHintType) Random(java.util.Random) DecoderResult(com.google.zxing.common.DecoderResult) AztecDetectorResult(com.google.zxing.aztec.AztecDetectorResult) EnumMap(java.util.EnumMap)

Example 2 with AztecWriter

use of com.google.zxing.aztec.AztecWriter in project zxing by zxing.

the class EncoderTest method testAztecWriter.

@Test
public void testAztecWriter() throws Exception {
    for (int i = 0; i < 1000; i++) {
        testWriter("€ 1 sample data.", "ISO-8859-1", 25, true, 2);
        testWriter("€ 1 sample data.", "ISO-8859-15", 25, true, 2);
        testWriter("€ 1 sample data.", "UTF-8", 25, true, 2);
        testWriter("€ 1 sample data.", "UTF-8", 100, true, 3);
        testWriter("€ 1 sample data.", "UTF-8", 300, true, 4);
        testWriter("€ 1 sample data.", "UTF-8", 500, false, 5);
        // Test AztecWriter defaults
        String data = "In ut magna vel mauris malesuada";
        AztecWriter writer = new AztecWriter();
        BitMatrix matrix = writer.encode(data, BarcodeFormat.AZTEC, 0, 0);
        AztecCode aztec = Encoder.encode(data.getBytes(StandardCharsets.ISO_8859_1), Encoder.DEFAULT_EC_PERCENT, Encoder.DEFAULT_AZTEC_LAYERS);
        BitMatrix expectedMatrix = aztec.getMatrix();
        assertEquals(matrix, expectedMatrix);
    }
}
Also used : BitMatrix(com.google.zxing.common.BitMatrix) AztecWriter(com.google.zxing.aztec.AztecWriter) ResultPoint(com.google.zxing.ResultPoint) Test(org.junit.Test)

Aggregations

ResultPoint (com.google.zxing.ResultPoint)2 AztecWriter (com.google.zxing.aztec.AztecWriter)2 BitMatrix (com.google.zxing.common.BitMatrix)2 EncodeHintType (com.google.zxing.EncodeHintType)1 AztecDetectorResult (com.google.zxing.aztec.AztecDetectorResult)1 Decoder (com.google.zxing.aztec.decoder.Decoder)1 DecoderResult (com.google.zxing.common.DecoderResult)1 EnumMap (java.util.EnumMap)1 Random (java.util.Random)1 Test (org.junit.Test)1