Search in sources :

Example 6 with DecodeHintType

use of com.google.zxing.DecodeHintType 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)

Example 7 with DecodeHintType

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

the class OneDReader method doDecode.

/**
   * We're going to examine rows from the middle outward, searching alternately above and below the
   * middle, and farther out each time. rowStep is the number of rows between each successive
   * attempt above and below the middle. So we'd scan row middle, then middle - rowStep, then
   * middle + rowStep, then middle - (2 * rowStep), etc.
   * rowStep is bigger as the image is taller, but is always at least 1. We've somewhat arbitrarily
   * decided that moving up and down by about 1/16 of the image is pretty good; we try more of the
   * image if "trying harder".
   *
   * @param image The image to decode
   * @param hints Any hints that were requested
   * @return The contents of the decoded barcode
   * @throws NotFoundException Any spontaneous errors which occur
   */
private Result doDecode(BinaryBitmap image, Map<DecodeHintType, ?> hints) throws NotFoundException {
    int width = image.getWidth();
    int height = image.getHeight();
    BitArray row = new BitArray(width);
    int middle = height >> 1;
    boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER);
    int rowStep = Math.max(1, height >> (tryHarder ? 8 : 5));
    int maxLines;
    if (tryHarder) {
        // Look at the whole image, not just the center
        maxLines = height;
    } else {
        // 15 rows spaced 1/32 apart is roughly the middle half of the image
        maxLines = 15;
    }
    for (int x = 0; x < maxLines; x++) {
        // Scanning from the middle out. Determine which row we're looking at next:
        int rowStepsAboveOrBelow = (x + 1) / 2;
        // i.e. is x even?
        boolean isAbove = (x & 0x01) == 0;
        int rowNumber = middle + rowStep * (isAbove ? rowStepsAboveOrBelow : -rowStepsAboveOrBelow);
        if (rowNumber < 0 || rowNumber >= height) {
            // Oops, if we run off the top or bottom, stop
            break;
        }
        // Estimate black point for this row and load it:
        try {
            row = image.getBlackRow(rowNumber, row);
        } catch (NotFoundException ignored) {
            continue;
        }
        // handle decoding upside down barcodes.
        for (int attempt = 0; attempt < 2; attempt++) {
            if (attempt == 1) {
                // trying again?
                // reverse the row and continue
                row.reverse();
                // that start on the center line.
                if (hints != null && hints.containsKey(DecodeHintType.NEED_RESULT_POINT_CALLBACK)) {
                    Map<DecodeHintType, Object> newHints = new EnumMap<>(DecodeHintType.class);
                    newHints.putAll(hints);
                    newHints.remove(DecodeHintType.NEED_RESULT_POINT_CALLBACK);
                    hints = newHints;
                }
            }
            try {
                // Look for a barcode
                Result result = decodeRow(rowNumber, row, hints);
                // We found our barcode
                if (attempt == 1) {
                    // But it was upside down, so note that
                    result.putMetadata(ResultMetadataType.ORIENTATION, 180);
                    // And remember to flip the result points horizontally.
                    ResultPoint[] points = result.getResultPoints();
                    if (points != null) {
                        points[0] = new ResultPoint(width - points[0].getX() - 1, points[0].getY());
                        points[1] = new ResultPoint(width - points[1].getX() - 1, points[1].getY());
                    }
                }
                return result;
            } catch (ReaderException re) {
            // continue -- just couldn't decode this row
            }
        }
    }
    throw NotFoundException.getNotFoundInstance();
}
Also used : ResultPoint(com.google.zxing.ResultPoint) DecodeHintType(com.google.zxing.DecodeHintType) NotFoundException(com.google.zxing.NotFoundException) BitArray(com.google.zxing.common.BitArray) EnumMap(java.util.EnumMap) ResultPoint(com.google.zxing.ResultPoint) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException)

Example 8 with DecodeHintType

use of com.google.zxing.DecodeHintType 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 9 with DecodeHintType

use of com.google.zxing.DecodeHintType 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 10 with DecodeHintType

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

the class AbstractBlackBoxTestCase method decode.

private boolean decode(BinaryBitmap source, float rotation, String expectedText, Map<?, ?> expectedMetadata, boolean tryHarder) throws ReaderException {
    String suffix = String.format(" (%srotation: %d)", tryHarder ? "try harder, " : "", (int) rotation);
    Map<DecodeHintType, Object> hints = new EnumMap<>(DecodeHintType.class);
    if (tryHarder) {
        hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
    }
    // Try in 'pure' mode mostly to exercise PURE_BARCODE code paths for exceptions;
    // not expected to pass, generally
    Result result = null;
    try {
        Map<DecodeHintType, Object> pureHints = new EnumMap<>(hints);
        pureHints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
        result = barcodeReader.decode(source, pureHints);
    } catch (ReaderException re) {
    // continue
    }
    if (result == null) {
        result = barcodeReader.decode(source, hints);
    }
    if (expectedFormat != result.getBarcodeFormat()) {
        log.info(String.format("Format mismatch: expected '%s' but got '%s'%s", expectedFormat, result.getBarcodeFormat(), suffix));
        return false;
    }
    String resultText = result.getText();
    if (!expectedText.equals(resultText)) {
        log.info(String.format("Content mismatch: expected '%s' but got '%s'%s", expectedText, resultText, suffix));
        return false;
    }
    Map<ResultMetadataType, ?> resultMetadata = result.getResultMetadata();
    for (Map.Entry<?, ?> metadatum : expectedMetadata.entrySet()) {
        ResultMetadataType key = ResultMetadataType.valueOf(metadatum.getKey().toString());
        Object expectedValue = metadatum.getValue();
        Object actualValue = resultMetadata == null ? null : resultMetadata.get(key);
        if (!expectedValue.equals(actualValue)) {
            log.info(String.format("Metadata mismatch for key '%s': expected '%s' but got '%s'", key, expectedValue, actualValue));
            return false;
        }
    }
    return true;
}
Also used : DecodeHintType(com.google.zxing.DecodeHintType) ResultMetadataType(com.google.zxing.ResultMetadataType) EnumMap(java.util.EnumMap) Map(java.util.Map) EnumMap(java.util.EnumMap) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException)

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