Search in sources :

Example 41 with EnumMap

use of java.util.EnumMap 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 42 with EnumMap

use of java.util.EnumMap in project zxing by zxing.

the class DataMatrixWriterTestCase method testDataMatrixImageWriter.

@Test
public void testDataMatrixImageWriter() {
    Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
    hints.put(EncodeHintType.DATA_MATRIX_SHAPE, SymbolShapeHint.FORCE_SQUARE);
    int bigEnough = 64;
    DataMatrixWriter writer = new DataMatrixWriter();
    BitMatrix matrix = writer.encode("Hello Google", BarcodeFormat.DATA_MATRIX, bigEnough, bigEnough, hints);
    assertNotNull(matrix);
    assertTrue(bigEnough >= matrix.getWidth());
    assertTrue(bigEnough >= matrix.getHeight());
}
Also used : EncodeHintType(com.google.zxing.EncodeHintType) BitMatrix(com.google.zxing.common.BitMatrix) EnumMap(java.util.EnumMap) SymbolShapeHint(com.google.zxing.datamatrix.encoder.SymbolShapeHint) Test(org.junit.Test)

Example 43 with EnumMap

use of java.util.EnumMap in project BarcodeEye by BarcodeEye.

the class DecodeHintManager method parseDecodeHints.

static Map<DecodeHintType, Object> parseDecodeHints(Intent intent) {
    Bundle extras = intent.getExtras();
    if (extras == null || extras.isEmpty()) {
        return null;
    }
    Map<DecodeHintType, Object> hints = new EnumMap<DecodeHintType, Object>(DecodeHintType.class);
    for (DecodeHintType hintType : DecodeHintType.values()) {
        if (hintType == DecodeHintType.CHARACTER_SET || hintType == DecodeHintType.NEED_RESULT_POINT_CALLBACK || hintType == DecodeHintType.POSSIBLE_FORMATS) {
            // This hint is specified in another way
            continue;
        }
        String hintName = hintType.name();
        if (extras.containsKey(hintName)) {
            if (hintType.getValueType().equals(Void.class)) {
                // Void hints are just flags: use the constant specified by the DecodeHintType
                hints.put(hintType, Boolean.TRUE);
            } else {
                Object hintData = extras.get(hintName);
                if (hintType.getValueType().isInstance(hintData)) {
                    hints.put(hintType, hintData);
                } else {
                    Log.w(TAG, "Ignoring hint " + hintType + " because it is not assignable from " + hintData);
                }
            }
        }
    }
    Log.i(TAG, "Hints from the Intent: " + hints);
    return hints;
}
Also used : DecodeHintType(com.google.zxing.DecodeHintType) Bundle(android.os.Bundle) EnumMap(java.util.EnumMap)

Example 44 with EnumMap

use of java.util.EnumMap in project BarcodeEye by BarcodeEye.

the class DecodeHintManager method parseDecodeHints.

static Map<DecodeHintType, ?> parseDecodeHints(Uri inputUri) {
    String query = inputUri.getEncodedQuery();
    if (query == null || query.isEmpty()) {
        return null;
    }
    // Extract parameters
    Map<String, String> parameters = splitQuery(query);
    Map<DecodeHintType, Object> hints = new EnumMap<DecodeHintType, Object>(DecodeHintType.class);
    for (DecodeHintType hintType : DecodeHintType.values()) {
        if (hintType == DecodeHintType.CHARACTER_SET || hintType == DecodeHintType.NEED_RESULT_POINT_CALLBACK || hintType == DecodeHintType.POSSIBLE_FORMATS) {
            // This hint is specified in another way
            continue;
        }
        String parameterName = hintType.name();
        String parameterText = parameters.get(parameterName);
        if (parameterText == null) {
            continue;
        }
        if (hintType.getValueType().equals(Object.class)) {
            // This is an unspecified type of hint content. Use the value as is.
            // TODO: Can we make a different assumption on this?
            hints.put(hintType, parameterText);
            continue;
        }
        if (hintType.getValueType().equals(Void.class)) {
            // Void hints are just flags: use the constant specified by DecodeHintType
            hints.put(hintType, Boolean.TRUE);
            continue;
        }
        if (hintType.getValueType().equals(String.class)) {
            // A string hint: use the decoded value.
            hints.put(hintType, parameterText);
            continue;
        }
        if (hintType.getValueType().equals(Boolean.class)) {
            // An empty parameter is simply a flag-style parameter, assuming true
            if (parameterText.isEmpty()) {
                hints.put(hintType, Boolean.TRUE);
            } else if ("0".equals(parameterText) || "false".equalsIgnoreCase(parameterText) || "no".equalsIgnoreCase(parameterText)) {
                hints.put(hintType, Boolean.FALSE);
            } else {
                hints.put(hintType, Boolean.TRUE);
            }
            continue;
        }
        if (hintType.getValueType().equals(int[].class)) {
            // Strip a trailing comma as in Java style array initialisers.
            if (!parameterText.isEmpty() && parameterText.charAt(parameterText.length() - 1) == ',') {
                parameterText = parameterText.substring(0, parameterText.length() - 1);
            }
            String[] values = COMMA.split(parameterText);
            int[] array = new int[values.length];
            for (int i = 0; i < values.length; i++) {
                try {
                    array[i] = Integer.parseInt(values[i]);
                } catch (NumberFormatException ignored) {
                    Log.w(TAG, "Skipping array of integers hint " + hintType + " due to invalid numeric value: '" + values[i] + '\'');
                    array = null;
                    break;
                }
            }
            if (array != null) {
                hints.put(hintType, array);
            }
            continue;
        }
        Log.w(TAG, "Unsupported hint type '" + hintType + "' of type " + hintType.getValueType());
    }
    Log.i(TAG, "Hints from the URI: " + hints);
    return hints;
}
Also used : DecodeHintType(com.google.zxing.DecodeHintType) EnumMap(java.util.EnumMap)

Example 45 with EnumMap

use of java.util.EnumMap in project weex-example by KalicyZhou.

the class DecodeHintManager method parseDecodeHints.

static Map<DecodeHintType, Object> parseDecodeHints(Intent intent) {
    Bundle extras = intent.getExtras();
    if (extras == null || extras.isEmpty()) {
        return null;
    }
    Map<DecodeHintType, Object> hints = new EnumMap<>(DecodeHintType.class);
    for (DecodeHintType hintType : DecodeHintType.values()) {
        if (hintType == DecodeHintType.CHARACTER_SET || hintType == DecodeHintType.NEED_RESULT_POINT_CALLBACK || hintType == DecodeHintType.POSSIBLE_FORMATS) {
            // This hint is specified in another way
            continue;
        }
        String hintName = hintType.name();
        if (extras.containsKey(hintName)) {
            if (hintType.getValueType().equals(Void.class)) {
                // Void hints are just flags: use the constant specified by the DecodeHintType
                hints.put(hintType, Boolean.TRUE);
            } else {
                Object hintData = extras.get(hintName);
                if (hintType.getValueType().isInstance(hintData)) {
                    hints.put(hintType, hintData);
                } else {
                    Log.w(TAG, "Ignoring hint " + hintType + " because it is not assignable from " + hintData);
                }
            }
        }
    }
    Log.i(TAG, "Hints from the Intent: " + hints);
    return hints;
}
Also used : DecodeHintType(com.google.zxing.DecodeHintType) Bundle(android.os.Bundle) EnumMap(java.util.EnumMap)

Aggregations

EnumMap (java.util.EnumMap)156 Map (java.util.Map)30 HashMap (java.util.HashMap)23 Collection (java.util.Collection)22 ArrayList (java.util.ArrayList)21 Test (org.junit.Test)18 DecodeHintType (com.google.zxing.DecodeHintType)17 List (java.util.List)14 IOException (java.io.IOException)12 EncodeHintType (com.google.zxing.EncodeHintType)11 BitMatrix (com.google.zxing.common.BitMatrix)11 Set (java.util.Set)11 Occur (org.apache.lucene.search.BooleanClause.Occur)10 Iterator (java.util.Iterator)9 ImmutableMap (com.google.common.collect.ImmutableMap)7 TreeMap (java.util.TreeMap)7 NoSuchElementException (java.util.NoSuchElementException)6 Bundle (android.os.Bundle)5 QRCodeWriter (com.google.zxing.qrcode.QRCodeWriter)5 URL (java.net.URL)5