Search in sources :

Example 16 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 17 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 18 with ReaderException

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

the class BenchmarkAsyncTask method decode.

private static BenchmarkItem decode(MultiFormatReader reader, File file) {
    Bitmap imageBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
    if (imageBitmap == null) {
        Log.e(TAG, "Couldn't open " + file);
        return null;
    }
    int width = imageBitmap.getWidth();
    int height = imageBitmap.getHeight();
    int[] pixels = new int[width * height];
    imageBitmap.getPixels(pixels, 0, width, 0, 0, width, height);
    RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);
    BenchmarkItem item = new BenchmarkItem(file, RUNS);
    for (int x = 0; x < RUNS; x++) {
        boolean success;
        Result result = null;
        // Using this call instead of getting the time should eliminate a lot of variability due to
        // scheduling and what else is happening in the system.
        long now = Debug.threadCpuTimeNanos();
        try {
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
            result = reader.decodeWithState(bitmap);
            success = true;
        } catch (ReaderException ignored) {
            success = false;
        }
        now = Debug.threadCpuTimeNanos() - now;
        if (x == 0) {
            item.setDecoded(success);
            item.setFormat(result != null ? result.getBarcodeFormat() : null);
        }
        item.addResult((int) (now / 1000));
    }
    return item;
}
Also used : Bitmap(android.graphics.Bitmap) BinaryBitmap(com.google.zxing.BinaryBitmap) BinaryBitmap(com.google.zxing.BinaryBitmap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) RGBLuminanceSource(com.google.zxing.RGBLuminanceSource) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException)

Example 19 with ReaderException

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

the class DecodeUtils method decodeWithZxing.

public String decodeWithZxing(Bitmap bitmap) {
    MultiFormatReader multiFormatReader = new MultiFormatReader();
    multiFormatReader.setHints(changeZXingDecodeDataMode());
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int[] pixels = new int[width * height];
    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
    Result rawResult = null;
    RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);
    if (source != null) {
        BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));
        try {
            rawResult = multiFormatReader.decodeWithState(binaryBitmap);
        } catch (ReaderException re) {
        // continue
        } finally {
            multiFormatReader.reset();
        }
    }
    return rawResult != null ? rawResult.getText() : null;
}
Also used : MultiFormatReader(com.google.zxing.MultiFormatReader) BinaryBitmap(com.google.zxing.BinaryBitmap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) RGBLuminanceSource(com.google.zxing.RGBLuminanceSource) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException)

Example 20 with ReaderException

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

the class ZXingScannerView method onPreviewFrame.

@Override
public void onPreviewFrame(byte[] data, Camera camera) {
    if (mResultHandler == null) {
        return;
    }
    try {
        Camera.Parameters parameters = camera.getParameters();
        Camera.Size size = parameters.getPreviewSize();
        int width = size.width;
        int height = size.height;
        if (DisplayUtils.getScreenOrientation(getContext()) == Configuration.ORIENTATION_PORTRAIT) {
            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;
        }
        Result rawResult = null;
        PlanarYUVLuminanceSource source = buildLuminanceSource(data, width, height);
        if (source != null) {
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
            try {
                rawResult = mMultiFormatReader.decodeWithState(bitmap);
            } catch (ReaderException re) {
            // continue
            } catch (NullPointerException npe) {
            // This is terrible
            } catch (ArrayIndexOutOfBoundsException aoe) {
            } finally {
                mMultiFormatReader.reset();
            }
        }
        final Result finalRawResult = rawResult;
        if (finalRawResult != null) {
            Handler handler = new Handler(Looper.getMainLooper());
            handler.post(new Runnable() {

                @Override
                public void run() {
                    // Stopping the preview can take a little long.
                    // So we want to set result handler to null to discard subsequent calls to
                    // onPreviewFrame.
                    ResultHandler tmpResultHandler = mResultHandler;
                    mResultHandler = null;
                    stopCameraPreview();
                    if (tmpResultHandler != null) {
                        tmpResultHandler.handleResult(finalRawResult);
                    }
                }
            });
        } else {
            camera.setOneShotPreviewCallback(this);
        }
    } catch (RuntimeException e) {
        // TODO: Terrible hack. It is possible that this method is invoked after camera is released.
        Log.e(TAG, e.toString(), e);
    }
}
Also used : Handler(android.os.Handler) HybridBinarizer(com.google.zxing.common.HybridBinarizer) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException) PlanarYUVLuminanceSource(com.google.zxing.PlanarYUVLuminanceSource) Camera(android.hardware.Camera) BinaryBitmap(com.google.zxing.BinaryBitmap)

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 ArrayList (java.util.ArrayList)6 LuminanceSource (com.google.zxing.LuminanceSource)5 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