Search in sources :

Example 16 with NotFoundException

use of com.google.zxing.NotFoundException in project android-zxing by PearceXu.

the class RSSExpandedReader method retrieveNextPair.

// not private for testing
ExpandedPair retrieveNextPair(BitArray row, List<ExpandedPair> previousPairs, int rowNumber) throws NotFoundException {
    boolean isOddPattern = previousPairs.size() % 2 == 0;
    if (startFromEven) {
        isOddPattern = !isOddPattern;
    }
    FinderPattern pattern;
    boolean keepFinding = true;
    int forcedOffset = -1;
    do {
        this.findNextPair(row, previousPairs, forcedOffset);
        pattern = parseFoundFinderPattern(row, rowNumber, isOddPattern);
        if (pattern == null) {
            forcedOffset = getNextSecondBar(row, this.startEnd[0]);
        } else {
            keepFinding = false;
        }
    } while (keepFinding);
    // When stacked symbol is split over multiple rows, there's no way to guess if this pair can be last or not.
    // boolean mayBeLast = checkPairSequence(previousPairs, pattern);
    DataCharacter leftChar = this.decodeDataCharacter(row, pattern, isOddPattern, true);
    if (!previousPairs.isEmpty() && previousPairs.get(previousPairs.size() - 1).mustBeLast()) {
        throw NotFoundException.getNotFoundInstance();
    }
    DataCharacter rightChar;
    try {
        rightChar = this.decodeDataCharacter(row, pattern, isOddPattern, false);
    } catch (NotFoundException ignored) {
        rightChar = null;
    }
    return new ExpandedPair(leftChar, rightChar, pattern, true);
}
Also used : FinderPattern(com.google.zxing.oned.rss.FinderPattern) NotFoundException(com.google.zxing.NotFoundException) ResultPoint(com.google.zxing.ResultPoint) DataCharacter(com.google.zxing.oned.rss.DataCharacter)

Example 17 with NotFoundException

use of com.google.zxing.NotFoundException 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 18 with NotFoundException

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

the class RSSExpandedReader method parseFoundFinderPattern.

private FinderPattern parseFoundFinderPattern(BitArray row, int rowNumber, boolean oddPattern) {
    // Actually we found elements 2-5.
    int firstCounter;
    int start;
    int end;
    if (oddPattern) {
        // If pattern number is odd, we need to locate element 1 *before* the current block.
        int firstElementStart = this.startEnd[0] - 1;
        // Locate element 1
        while (firstElementStart >= 0 && !row.get(firstElementStart)) {
            firstElementStart--;
        }
        firstElementStart++;
        firstCounter = this.startEnd[0] - firstElementStart;
        start = firstElementStart;
        end = this.startEnd[1];
    } else {
        // If pattern number is even, the pattern is reversed, so we need to locate element 1 *after* the current block.
        start = this.startEnd[0];
        end = row.getNextUnset(this.startEnd[1] + 1);
        firstCounter = end - this.startEnd[1];
    }
    // Make 'counters' hold 1-4
    int[] counters = this.getDecodeFinderCounters();
    System.arraycopy(counters, 0, counters, 1, counters.length - 1);
    counters[0] = firstCounter;
    int value;
    try {
        value = parseFinderValue(counters, FINDER_PATTERNS);
    } catch (NotFoundException ignored) {
        return null;
    }
    return new FinderPattern(value, new int[] { start, end }, start, end, rowNumber);
}
Also used : FinderPattern(com.google.zxing.oned.rss.FinderPattern) NotFoundException(com.google.zxing.NotFoundException) ResultPoint(com.google.zxing.ResultPoint)

Example 19 with NotFoundException

use of com.google.zxing.NotFoundException in project elastic-core-maven by OrdinaryDude.

the class DecodeQRCode method processRequest.

@Override
protected JSONStreamAware processRequest(HttpServletRequest request) throws NxtException {
    String qrCodeBase64 = Convert.nullToEmpty(request.getParameter("qrCodeBase64"));
    JSONObject response = new JSONObject();
    try {
        BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(ImageIO.read(new ByteArrayInputStream(Base64.getDecoder().decode(qrCodeBase64))))));
        Map hints = new HashMap();
        hints.put(DecodeHintType.POSSIBLE_FORMATS, EnumSet.of(BarcodeFormat.QR_CODE));
        hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
        Result qrCodeData = new MultiFormatReader().decode(binaryBitmap, hints);
        response.put("qrCodeData", qrCodeData.getText());
    } catch (IOException ex) {
        String errorMessage = "Error reading base64 byte stream";
        Logger.logErrorMessage(errorMessage, ex);
        JSONData.putException(response, ex, errorMessage);
    } catch (NullPointerException ex) {
        String errorMessage = "Invalid base64 image";
        Logger.logErrorMessage(errorMessage, ex);
        JSONData.putException(response, ex, errorMessage);
    } catch (NotFoundException ex) {
        response.put("qrCodeData", "");
    }
    return response;
}
Also used : MultiFormatReader(com.google.zxing.MultiFormatReader) HashMap(java.util.HashMap) NotFoundException(com.google.zxing.NotFoundException) IOException(java.io.IOException) HybridBinarizer(com.google.zxing.common.HybridBinarizer) Result(com.google.zxing.Result) JSONObject(org.json.simple.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BinaryBitmap(com.google.zxing.BinaryBitmap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 20 with NotFoundException

use of com.google.zxing.NotFoundException in project react-native-camera by react-native-community.

the class BarCodeScannerAsyncTask method doInBackground.

@Override
protected Result doInBackground(Void... ignored) {
    if (isCancelled() || mDelegate == null) {
        return null;
    }
    Result result = null;
    try {
        BinaryBitmap bitmap = generateBitmapFromImageData(mImageData, mWidth, mHeight);
        result = mMultiFormatReader.decodeWithState(bitmap);
    } catch (NotFoundException e) {
    // No barcode found, result is already null.
    } catch (Throwable t) {
        t.printStackTrace();
    }
    return result;
}
Also used : NotFoundException(com.google.zxing.NotFoundException) BinaryBitmap(com.google.zxing.BinaryBitmap) Result(com.google.zxing.Result)

Aggregations

NotFoundException (com.google.zxing.NotFoundException)56 ResultPoint (com.google.zxing.ResultPoint)38 Result (com.google.zxing.Result)22 BinaryBitmap (com.google.zxing.BinaryBitmap)20 DecodeHintType (com.google.zxing.DecodeHintType)12 FormatException (com.google.zxing.FormatException)12 HybridBinarizer (com.google.zxing.common.HybridBinarizer)12 FinderPattern (com.google.zxing.oned.rss.FinderPattern)9 ChecksumException (com.google.zxing.ChecksumException)8 ResultPointCallback (com.google.zxing.ResultPointCallback)8 BitArray (com.google.zxing.common.BitArray)7 BitMatrix (com.google.zxing.common.BitMatrix)7 ReaderException (com.google.zxing.ReaderException)6 DecoderResult (com.google.zxing.common.DecoderResult)6 Hashtable (java.util.Hashtable)6 Decoder (com.google.zxing.aztec.decoder.Decoder)5 DetectorResult (com.google.zxing.common.DetectorResult)5 QRCodeReader (com.google.zxing.qrcode.QRCodeReader)5 EnumMap (java.util.EnumMap)5 BitmapFactory (android.graphics.BitmapFactory)4