Search in sources :

Example 21 with DecodeHintType

use of com.google.zxing.DecodeHintType in project zxing by zxing.

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.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 22 with DecodeHintType

use of com.google.zxing.DecodeHintType in project weex-example by KalicyZhou.

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.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)

Aggregations

DecodeHintType (com.google.zxing.DecodeHintType)22 EnumMap (java.util.EnumMap)17 Bundle (android.os.Bundle)5 Result (com.google.zxing.Result)5 MultiFormatReader (com.google.zxing.MultiFormatReader)4 ReaderException (com.google.zxing.ReaderException)4 BarcodeFormat (com.google.zxing.BarcodeFormat)3 BinaryBitmap (com.google.zxing.BinaryBitmap)3 NotFoundException (com.google.zxing.NotFoundException)3 ResultPoint (com.google.zxing.ResultPoint)3 BitmapFactory (android.graphics.BitmapFactory)2 BitArray (com.google.zxing.common.BitArray)2 HybridBinarizer (com.google.zxing.common.HybridBinarizer)2 Hashtable (java.util.Hashtable)2 Test (org.junit.Test)2 Bitmap (android.graphics.Bitmap)1 BufferedImageLuminanceSource (com.google.zxing.BufferedImageLuminanceSource)1 ChecksumException (com.google.zxing.ChecksumException)1 FormatException (com.google.zxing.FormatException)1 LuminanceSource (com.google.zxing.LuminanceSource)1