Search in sources :

Example 1 with PlanarYUVLuminanceSource

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

the class DecodeUtils method decodeWithZxing.

public String decodeWithZxing(byte[] data, int width, int height, Rect crop) {
    MultiFormatReader multiFormatReader = new MultiFormatReader();
    multiFormatReader.setHints(changeZXingDecodeDataMode());
    Result rawResult = null;
    PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data, width, height, crop.left, crop.top, crop.width(), crop.height(), false);
    if (source != null) {
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        try {
            rawResult = multiFormatReader.decodeWithState(bitmap);
        } catch (ReaderException re) {
        // continue
        } finally {
            multiFormatReader.reset();
        }
    }
    return rawResult != null ? rawResult.getText() : null;
}
Also used : MultiFormatReader(com.google.zxing.MultiFormatReader) PlanarYUVLuminanceSource(com.google.zxing.PlanarYUVLuminanceSource) BinaryBitmap(com.google.zxing.BinaryBitmap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException)

Example 2 with PlanarYUVLuminanceSource

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

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 3 with PlanarYUVLuminanceSource

use of com.google.zxing.PlanarYUVLuminanceSource in project BGAQRCode-Android by bingoogolapple.

the class ZXingView method processData.

@Override
public String processData(byte[] data, int width, int height, boolean isRetry) {
    String result = null;
    Result rawResult = null;
    try {
        PlanarYUVLuminanceSource source = null;
        Rect rect = mScanBoxView.getScanBoxAreaRect(height);
        if (rect != null) {
            source = new PlanarYUVLuminanceSource(data, width, height, rect.left, rect.top, rect.width(), rect.height(), false);
        } else {
            source = new PlanarYUVLuminanceSource(data, width, height, 0, 0, width, height, false);
        }
        rawResult = mMultiFormatReader.decodeWithState(new BinaryBitmap(new HybridBinarizer(source)));
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        mMultiFormatReader.reset();
    }
    if (rawResult != null) {
        result = rawResult.getText();
    }
    return result;
}
Also used : Rect(android.graphics.Rect) PlanarYUVLuminanceSource(com.google.zxing.PlanarYUVLuminanceSource) BinaryBitmap(com.google.zxing.BinaryBitmap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) Result(com.google.zxing.Result)

Example 4 with PlanarYUVLuminanceSource

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

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();
    Camera.Size size = activity.getCameraManager().getPreviewSize();
    byte[] rotatedData = new byte[data.length];
    for (int y = 0; y < size.height; y++) {
        for (int x = 0; x < size.width; x++) rotatedData[x * size.height + size.height - y - 1] = data[x + y * size.width];
    }
    int tmp = size.width;
    size.width = size.height;
    size.height = tmp;
    String resultStr = null;
    Rect cropRect = activity.getCropRect();
    if (null == cropRect) {
        activity.initCrop();
    }
    cropRect = activity.getCropRect();
    TLog.d(TAG, cropRect.toString());
    mDecodeUtils.setDataMode(activity.getDataMode());
    String zbarStr = mDecodeUtils.decodeWithZbar(rotatedData, size.width, size.height, cropRect);
    String zxingStr = mDecodeUtils.decodeWithZxing(rotatedData, size.width, size.height, cropRect);
    if (!TextUtils.isEmpty(zbarStr)) {
        mDecodeMode = DecodeUtils.DECODE_MODE_ZBAR;
        resultStr = zbarStr;
    } else if (!TextUtils.isEmpty(zxingStr)) {
        mDecodeMode = DecodeUtils.DECODE_MODE_ZXING;
        resultStr = zxingStr;
    }
    Handler handler = activity.getHandler();
    if (!TextUtils.isEmpty(resultStr)) {
        long end = System.currentTimeMillis();
        if (handler != null) {
            Message message = Message.obtain(handler, Constants.ID_DECODE_SUCCESS, resultStr);
            Bundle bundle = new Bundle();
            PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(rotatedData, size.width, size.height, cropRect.left, cropRect.top, cropRect.width(), cropRect.height(), false);
            bundle.putInt(DecodeThread.DECODE_MODE, mDecodeMode);
            bundle.putString(DecodeThread.DECODE_TIME, (end - start) + "ms");
            bundleThumbnail(source, bundle);
            message.setData(bundle);
            message.sendToTarget();
        }
    } else {
        if (handler != null) {
            Message message = Message.obtain(handler, Constants.ID_DECODE_FAILED);
            message.sendToTarget();
        }
    }
}
Also used : Rect(android.graphics.Rect) Message(android.os.Message) Bundle(android.os.Bundle) PlanarYUVLuminanceSource(com.google.zxing.PlanarYUVLuminanceSource) Handler(android.os.Handler) Camera(android.hardware.Camera)

Example 5 with PlanarYUVLuminanceSource

use of com.google.zxing.PlanarYUVLuminanceSource 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

PlanarYUVLuminanceSource (com.google.zxing.PlanarYUVLuminanceSource)13 BinaryBitmap (com.google.zxing.BinaryBitmap)11 Result (com.google.zxing.Result)11 HybridBinarizer (com.google.zxing.common.HybridBinarizer)11 ReaderException (com.google.zxing.ReaderException)10 Handler (android.os.Handler)9 Bundle (android.os.Bundle)8 Message (android.os.Message)8 Rect (android.graphics.Rect)3 Camera (android.hardware.Camera)2 Bitmap (android.graphics.Bitmap)1 Size (android.hardware.Camera.Size)1 Nullable (android.support.annotation.Nullable)1 ChecksumException (com.google.zxing.ChecksumException)1 FormatException (com.google.zxing.FormatException)1 MultiFormatReader (com.google.zxing.MultiFormatReader)1 NotFoundException (com.google.zxing.NotFoundException)1