Search in sources :

Example 56 with ReaderException

use of com.google.zxing.ReaderException in project incubator-weex by apache.

the class MultiDetector method detectMulti.

public DetectorResult[] detectMulti(Map<DecodeHintType, ?> hints) throws NotFoundException {
    BitMatrix image = getImage();
    ResultPointCallback resultPointCallback = hints == null ? null : (ResultPointCallback) hints.get(DecodeHintType.NEED_RESULT_POINT_CALLBACK);
    MultiFinderPatternFinder finder = new MultiFinderPatternFinder(image, resultPointCallback);
    FinderPatternInfo[] infos = finder.findMulti(hints);
    if (infos.length == 0) {
        throw NotFoundException.getNotFoundInstance();
    }
    List<DetectorResult> result = new ArrayList<>();
    for (FinderPatternInfo info : infos) {
        try {
            result.add(processFinderPatternInfo(info));
        } catch (ReaderException e) {
        // ignore
        }
    }
    if (result.isEmpty()) {
        return EMPTY_DETECTOR_RESULTS;
    } else {
        return result.toArray(new DetectorResult[result.size()]);
    }
}
Also used : ResultPointCallback(com.google.zxing.ResultPointCallback) ArrayList(java.util.ArrayList) DetectorResult(com.google.zxing.common.DetectorResult) BitMatrix(com.google.zxing.common.BitMatrix) FinderPatternInfo(com.google.zxing.qrcode.detector.FinderPatternInfo) ReaderException(com.google.zxing.ReaderException)

Example 57 with ReaderException

use of com.google.zxing.ReaderException in project incubator-weex by apache.

the class GenericMultipleBarcodeReader method doDecodeMultiple.

private void doDecodeMultiple(BinaryBitmap image, Map<DecodeHintType, ?> hints, List<Result> results, int xOffset, int yOffset, int currentDepth) {
    if (currentDepth > MAX_DEPTH) {
        return;
    }
    Result result;
    try {
        result = delegate.decode(image, hints);
    } catch (ReaderException ignored) {
        return;
    }
    boolean alreadyFound = false;
    for (Result existingResult : results) {
        if (existingResult.getText().equals(result.getText())) {
            alreadyFound = true;
            break;
        }
    }
    if (!alreadyFound) {
        results.add(translateResultPoints(result, xOffset, yOffset));
    }
    ResultPoint[] resultPoints = result.getResultPoints();
    if (resultPoints == null || resultPoints.length == 0) {
        return;
    }
    int width = image.getWidth();
    int height = image.getHeight();
    float minX = width;
    float minY = height;
    float maxX = 0.0f;
    float maxY = 0.0f;
    for (ResultPoint point : resultPoints) {
        if (point == null) {
            continue;
        }
        float x = point.getX();
        float y = point.getY();
        if (x < minX) {
            minX = x;
        }
        if (y < minY) {
            minY = y;
        }
        if (x > maxX) {
            maxX = x;
        }
        if (y > maxY) {
            maxY = y;
        }
    }
    // Decode left of barcode
    if (minX > MIN_DIMENSION_TO_RECUR) {
        doDecodeMultiple(image.crop(0, 0, (int) minX, height), hints, results, xOffset, yOffset, currentDepth + 1);
    }
    // Decode above barcode
    if (minY > MIN_DIMENSION_TO_RECUR) {
        doDecodeMultiple(image.crop(0, 0, width, (int) minY), hints, results, xOffset, yOffset, currentDepth + 1);
    }
    // Decode right of barcode
    if (maxX < width - MIN_DIMENSION_TO_RECUR) {
        doDecodeMultiple(image.crop((int) maxX, 0, width - (int) maxX, height), hints, results, xOffset + (int) maxX, yOffset, currentDepth + 1);
    }
    // Decode below barcode
    if (maxY < height - MIN_DIMENSION_TO_RECUR) {
        doDecodeMultiple(image.crop(0, (int) maxY, width, height - (int) maxY), hints, results, xOffset, yOffset + (int) maxY, currentDepth + 1);
    }
}
Also used : ResultPoint(com.google.zxing.ResultPoint) ResultPoint(com.google.zxing.ResultPoint) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException)

Example 58 with ReaderException

use of com.google.zxing.ReaderException in project incubator-weex by apache.

the class UPCEANReader method decodeRow.

/**
 * <p>Like {@link #decodeRow(int, BitArray, Map)}, but
 * allows caller to inform method about where the UPC/EAN start pattern is
 * found. This allows this to be computed once and reused across many implementations.</p>
 *
 * @param rowNumber row index into the image
 * @param row encoding of the row of the barcode image
 * @param startGuardRange start/end column where the opening start pattern was found
 * @param hints optional hints that influence decoding
 * @return {@link Result} encapsulating the result of decoding a barcode in the row
 * @throws NotFoundException if no potential barcode is found
 * @throws ChecksumException if a potential barcode is found but does not pass its checksum
 * @throws FormatException if a potential barcode is found but format is invalid
 */
public Result decodeRow(int rowNumber, BitArray row, int[] startGuardRange, Map<DecodeHintType, ?> hints) throws NotFoundException, ChecksumException, FormatException {
    ResultPointCallback resultPointCallback = hints == null ? null : (ResultPointCallback) hints.get(DecodeHintType.NEED_RESULT_POINT_CALLBACK);
    if (resultPointCallback != null) {
        resultPointCallback.foundPossibleResultPoint(new ResultPoint((startGuardRange[0] + startGuardRange[1]) / 2.0f, rowNumber));
    }
    StringBuilder result = decodeRowStringBuffer;
    result.setLength(0);
    int endStart = decodeMiddle(row, startGuardRange, result);
    if (resultPointCallback != null) {
        resultPointCallback.foundPossibleResultPoint(new ResultPoint(endStart, rowNumber));
    }
    int[] endRange = decodeEnd(row, endStart);
    if (resultPointCallback != null) {
        resultPointCallback.foundPossibleResultPoint(new ResultPoint((endRange[0] + endRange[1]) / 2.0f, rowNumber));
    }
    // Make sure there is a quiet zone at least as big as the end pattern after the barcode. The
    // spec might want more whitespace, but in practice this is the maximum we can count on.
    int end = endRange[1];
    int quietEnd = end + (end - endRange[0]);
    if (quietEnd >= row.getSize() || !row.isRange(end, quietEnd, false)) {
        throw NotFoundException.getNotFoundInstance();
    }
    String resultString = result.toString();
    // UPC/EAN should never be less than 8 chars anyway
    if (resultString.length() < 8) {
        throw FormatException.getFormatInstance();
    }
    if (!checkChecksum(resultString)) {
        throw ChecksumException.getChecksumInstance();
    }
    float left = (float) (startGuardRange[1] + startGuardRange[0]) / 2.0f;
    float right = (float) (endRange[1] + endRange[0]) / 2.0f;
    BarcodeFormat format = getBarcodeFormat();
    Result decodeResult = new Result(resultString, // no natural byte representation for these barcodes
    null, new ResultPoint[] { new ResultPoint(left, (float) rowNumber), new ResultPoint(right, (float) rowNumber) }, format);
    int extensionLength = 0;
    try {
        Result extensionResult = extensionReader.decodeRow(rowNumber, row, endRange[1]);
        decodeResult.putMetadata(ResultMetadataType.UPC_EAN_EXTENSION, extensionResult.getText());
        decodeResult.putAllMetadata(extensionResult.getResultMetadata());
        decodeResult.addResultPoints(extensionResult.getResultPoints());
        extensionLength = extensionResult.getText().length();
    } catch (ReaderException re) {
    // continue
    }
    int[] allowedExtensions = hints == null ? null : (int[]) hints.get(DecodeHintType.ALLOWED_EAN_EXTENSIONS);
    if (allowedExtensions != null) {
        boolean valid = false;
        for (int length : allowedExtensions) {
            if (extensionLength == length) {
                valid = true;
                break;
            }
        }
        if (!valid) {
            throw NotFoundException.getNotFoundInstance();
        }
    }
    if (format == BarcodeFormat.EAN_13 || format == BarcodeFormat.UPC_A) {
        String countryID = eanManSupport.lookupCountryIdentifier(resultString);
        if (countryID != null) {
            decodeResult.putMetadata(ResultMetadataType.POSSIBLE_COUNTRY, countryID);
        }
    }
    return decodeResult;
}
Also used : ResultPointCallback(com.google.zxing.ResultPointCallback) ResultPoint(com.google.zxing.ResultPoint) BarcodeFormat(com.google.zxing.BarcodeFormat) ResultPoint(com.google.zxing.ResultPoint) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException)

Example 59 with ReaderException

use of com.google.zxing.ReaderException in project incubator-weex by apache.

the class DecodeHandler method decode.

/**
 * Decode the data within the viewfinder rectangle, and time how long it took. For efficiency,
 * reuse the same reader objects from one decode to the next.
 *
 * @param data   The YUV preview frame.
 * @param width  The width of the preview frame.
 * @param height The height of the preview frame.
 */
private void decode(byte[] data, int width, int height) {
    long start = System.currentTimeMillis();
    Result rawResult = null;
    // PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(data, width, height);
    byte[] rotatedData = new byte[data.length];
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) rotatedData[x * height + height - y - 1] = data[x + y * width];
    }
    int tmp = width;
    width = height;
    height = tmp;
    PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(rotatedData, width, height);
    if (source != null) {
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        try {
            rawResult = multiFormatReader.decodeWithState(bitmap);
        } catch (ReaderException re) {
        // continue
        } finally {
            multiFormatReader.reset();
        }
    }
    Handler handler = activity.getHandler();
    if (rawResult != null) {
        // Don't log the barcode contents for security.
        long end = System.currentTimeMillis();
        Log.d(TAG, "Found barcode in " + (end - start) + " ms");
        if (handler != null) {
            Message message = Message.obtain(handler, R.id.decode_succeeded, rawResult);
            Bundle bundle = new Bundle();
            bundleThumbnail(source, bundle);
            message.setData(bundle);
            message.sendToTarget();
        }
    } else {
        if (handler != null) {
            Message message = Message.obtain(handler, R.id.decode_failed);
            message.sendToTarget();
        }
    }
}
Also used : Message(android.os.Message) PlanarYUVLuminanceSource(com.google.zxing.PlanarYUVLuminanceSource) Bundle(android.os.Bundle) Handler(android.os.Handler) BinaryBitmap(com.google.zxing.BinaryBitmap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException)

Example 60 with ReaderException

use of com.google.zxing.ReaderException in project keepass2android by PhilippC.

the class DecodeHandler method decode.

/**
 * Decode the data within the viewfinder rectangle, and time how long it took. For efficiency,
 * reuse the same reader objects from one decode to the next.
 *
 * @param data   The YUV preview frame.
 * @param width  The width of the preview frame.
 * @param height The height of the preview frame.
 */
private void decode(byte[] data, int width, int height) {
    byte[] rotatedData = new byte[data.length];
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) rotatedData[x * height + height - y - 1] = data[x + y * width];
    }
    int tmp = width;
    width = height;
    height = tmp;
    data = rotatedData;
    long start = System.currentTimeMillis();
    Result rawResult = null;
    PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(data, width, height);
    if (source != null) {
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        try {
            rawResult = multiFormatReader.decodeWithState(bitmap);
        } catch (ReaderException re) {
        // continue
        } finally {
            multiFormatReader.reset();
        }
    }
    Handler handler = activity.getHandler();
    if (rawResult != null) {
        // Don't log the barcode contents for security.
        long end = System.currentTimeMillis();
        Log.d(TAG, "Found barcode in " + (end - start) + " ms");
        if (handler != null) {
            Message message = Message.obtain(handler, R.id.decode_succeeded, rawResult);
            Bundle bundle = new Bundle();
            bundleThumbnail(source, bundle);
            message.setData(bundle);
            message.sendToTarget();
        }
    } else {
        if (handler != null) {
            Message message = Message.obtain(handler, R.id.decode_failed);
            message.sendToTarget();
        }
    }
}
Also used : Message(android.os.Message) PlanarYUVLuminanceSource(com.google.zxing.PlanarYUVLuminanceSource) Bundle(android.os.Bundle) Handler(android.os.Handler) BinaryBitmap(com.google.zxing.BinaryBitmap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException)

Aggregations

ReaderException (com.google.zxing.ReaderException)66 Result (com.google.zxing.Result)58 BinaryBitmap (com.google.zxing.BinaryBitmap)39 HybridBinarizer (com.google.zxing.common.HybridBinarizer)34 Message (android.os.Message)25 Bundle (android.os.Bundle)24 PlanarYUVLuminanceSource (com.google.zxing.PlanarYUVLuminanceSource)16 ResultPoint (com.google.zxing.ResultPoint)16 Handler (android.os.Handler)14 ArrayList (java.util.ArrayList)10 BarcodeFormat (com.google.zxing.BarcodeFormat)8 ResultPointCallback (com.google.zxing.ResultPointCallback)8 DetectorResult (com.google.zxing.common.DetectorResult)8 BufferedImage (java.awt.image.BufferedImage)8 LuminanceSource (com.google.zxing.LuminanceSource)7 MultiFormatReader (com.google.zxing.MultiFormatReader)7 BitArray (com.google.zxing.common.BitArray)7 BufferedImageLuminanceSource (com.google.zxing.BufferedImageLuminanceSource)6 DecodeHintType (com.google.zxing.DecodeHintType)6 NotFoundException (com.google.zxing.NotFoundException)5