Search in sources :

Example 31 with HybridBinarizer

use of com.google.zxing.common.HybridBinarizer 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)

Aggregations

BinaryBitmap (com.google.zxing.BinaryBitmap)31 HybridBinarizer (com.google.zxing.common.HybridBinarizer)31 Result (com.google.zxing.Result)30 ReaderException (com.google.zxing.ReaderException)21 Bundle (android.os.Bundle)13 Message (android.os.Message)13 MultiFormatReader (com.google.zxing.MultiFormatReader)11 PlanarYUVLuminanceSource (com.google.zxing.PlanarYUVLuminanceSource)11 Handler (android.os.Handler)8 LuminanceSource (com.google.zxing.LuminanceSource)7 NotFoundException (com.google.zxing.NotFoundException)5 BufferedImage (java.awt.image.BufferedImage)5 BufferedImageLuminanceSource (com.google.zxing.client.j2se.BufferedImageLuminanceSource)4 Bitmap (android.graphics.Bitmap)3 BufferedImageLuminanceSource (com.google.zxing.BufferedImageLuminanceSource)3 ChecksumException (com.google.zxing.ChecksumException)3 FormatException (com.google.zxing.FormatException)3 RGBLuminanceSource (com.google.zxing.RGBLuminanceSource)3 MultipleBarcodeReader (com.google.zxing.multi.MultipleBarcodeReader)3 IOException (java.io.IOException)3