Search in sources :

Example 51 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) {
            int rotationCount = getRotationCount();
            if (rotationCount == 1 || rotationCount == 3) {
                int tmp = width;
                width = height;
                height = tmp;
            }
            data = getRotatedData(data, camera);
        }
        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();
            }
            if (rawResult == null) {
                LuminanceSource invertedSource = source.invert();
                bitmap = new BinaryBitmap(new HybridBinarizer(invertedSource));
                try {
                    rawResult = mMultiFormatReader.decodeWithState(bitmap);
                } catch (NotFoundException e) {
                // continue
                } 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 : NotFoundException(com.google.zxing.NotFoundException) 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) LuminanceSource(com.google.zxing.LuminanceSource) PlanarYUVLuminanceSource(com.google.zxing.PlanarYUVLuminanceSource) Camera(android.hardware.Camera) BinaryBitmap(com.google.zxing.BinaryBitmap)

Example 52 with ReaderException

use of com.google.zxing.ReaderException in project QRCode by 5peak2me.

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(activity.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(activity.getHandler(), R.id.decode_failed);
        message.sendToTarget();
    }
}
Also used : Message(android.os.Message) PlanarYUVLuminanceSource(com.google.zxing.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 53 with ReaderException

use of com.google.zxing.ReaderException in project QRCode by 5peak2me.

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(activity.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(activity.getHandler(), R.id.decode_failed);
        message.sendToTarget();
    }
}
Also used : Message(android.os.Message) PlanarYUVLuminanceSource(com.google.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 54 with ReaderException

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

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);
    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 55 with ReaderException

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

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)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