Search in sources :

Example 11 with HybridBinarizer

use of com.google.zxing.common.HybridBinarizer in project smartmodule by carozhu.

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;
    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 {
            // 预览界面最终取到的是个bitmap,然后对其进行解码
            rawResult = multiFormatReader.decodeWithState(bitmap);
        } catch (ReaderException re) {
        // continue
        } finally {
            multiFormatReader.reset();
        }
    }
    Handler handler = activity.getCaptrueHandler();
    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 12 with HybridBinarizer

use of com.google.zxing.common.HybridBinarizer in project zxing-lib by kennydude.

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();
            Bitmap grayscaleBitmap = toBitmap(source, source.renderCroppedGreyscaleBitmap());
            bundle.putParcelable(DecodeThread.BARCODE_BITMAP, grayscaleBitmap);
            message.setData(bundle);
            message.sendToTarget();
        }
    } else {
        if (handler != null) {
            Message message = Message.obtain(handler, R.id.decode_failed);
            message.sendToTarget();
        }
    }
}
Also used : Bitmap(android.graphics.Bitmap) BinaryBitmap(com.google.zxing.BinaryBitmap) 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 13 with HybridBinarizer

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

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

use of com.google.zxing.common.HybridBinarizer in project Signal-Android by WhisperSystems.

the class ScanningThread method getScannedData.

@Nullable
private String getScannedData(byte[] data, int width, int height, int orientation) {
    try {
        if (orientation == 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;
        }
        PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data, width, height, 0, 0, width, height, false);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Result result = reader.decode(bitmap, hints);
        if (result != null)
            return result.getText();
    } catch (NullPointerException | ChecksumException | FormatException e) {
        Log.w(TAG, e);
    } catch (NotFoundException e) {
    // Thanks ZXing...
    }
    return null;
}
Also used : PlanarYUVLuminanceSource(com.google.zxing.PlanarYUVLuminanceSource) ChecksumException(com.google.zxing.ChecksumException) NotFoundException(com.google.zxing.NotFoundException) BinaryBitmap(com.google.zxing.BinaryBitmap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) FormatException(com.google.zxing.FormatException) Result(com.google.zxing.Result) Nullable(android.support.annotation.Nullable)

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