Search in sources :

Example 11 with ReaderException

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

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 12 with ReaderException

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

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 = (startGuardRange[1] + startGuardRange[0]) / 2.0f;
    float right = (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, rowNumber), new ResultPoint(right, 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 13 with ReaderException

use of com.google.zxing.ReaderException in project android-zxingLibrary by yipianfengye.

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;
    //modify here
    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];
    }
    // Here we are swapping, that's the difference to #11
    int tmp = width;
    width = height;
    height = tmp;
    PlanarYUVLuminanceSource source = CameraManager.get().buildLuminanceSource(rotatedData, width, height);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    try {
        rawResult = multiFormatReader.decodeWithState(bitmap);
    } catch (ReaderException re) {
    // continue
    } finally {
        multiFormatReader.reset();
    }
    if (rawResult != null) {
        long end = System.currentTimeMillis();
        Log.d(TAG, "Found barcode (" + (end - start) + " ms):\n" + rawResult.toString());
        Message message = Message.obtain(fragment.getHandler(), R.id.decode_succeeded, rawResult);
        Bundle bundle = new Bundle();
        bundle.putParcelable(DecodeThread.BARCODE_BITMAP, source.renderCroppedGreyscaleBitmap());
        message.setData(bundle);
        //Log.d(TAG, "Sending decode succeeded message...");
        message.sendToTarget();
    } else {
        Message message = Message.obtain(fragment.getHandler(), R.id.decode_failed);
        message.sendToTarget();
    }
}
Also used : Message(android.os.Message) PlanarYUVLuminanceSource(com.uuzuche.lib_zxing.camera.PlanarYUVLuminanceSource) Bundle(android.os.Bundle) BinaryBitmap(com.google.zxing.BinaryBitmap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException)

Example 14 with ReaderException

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

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 15 with ReaderException

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

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)

Aggregations

ReaderException (com.google.zxing.ReaderException)39 Result (com.google.zxing.Result)35 BinaryBitmap (com.google.zxing.BinaryBitmap)26 HybridBinarizer (com.google.zxing.common.HybridBinarizer)21 Bundle (android.os.Bundle)13 Message (android.os.Message)13 PlanarYUVLuminanceSource (com.google.zxing.PlanarYUVLuminanceSource)9 Handler (android.os.Handler)8 ResultPoint (com.google.zxing.ResultPoint)8 BufferedImage (java.awt.image.BufferedImage)7 BufferedImageLuminanceSource (com.google.zxing.BufferedImageLuminanceSource)6 LuminanceSource (com.google.zxing.LuminanceSource)6 ArrayList (java.util.ArrayList)6 MultiFormatReader (com.google.zxing.MultiFormatReader)5 BitArray (com.google.zxing.common.BitArray)5 Path (java.nio.file.Path)5 BarcodeFormat (com.google.zxing.BarcodeFormat)4 DecodeHintType (com.google.zxing.DecodeHintType)4 ResultPointCallback (com.google.zxing.ResultPointCallback)4 DetectorResult (com.google.zxing.common.DetectorResult)4