Search in sources :

Example 1 with RGBLuminanceSource

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

the class QRCodeDecoder method syncDecodeQRCode.

/**
     * 同步解析bitmap二维码。该方法是耗时操作,请在子线程中调用。
     *
     * @param bitmap 要解析的二维码图片
     * @return 返回二维码图片里的内容 或 null
     */
public static String syncDecodeQRCode(Bitmap bitmap) {
    Result result = null;
    RGBLuminanceSource source = null;
    try {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int[] pixels = new int[width * height];
        bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
        source = new RGBLuminanceSource(width, height, pixels);
        result = new MultiFormatReader().decode(new BinaryBitmap(new HybridBinarizer(source)), HINTS);
        return result.getText();
    } catch (Exception e) {
        e.printStackTrace();
        if (source != null) {
            try {
                result = new MultiFormatReader().decode(new BinaryBitmap(new GlobalHistogramBinarizer(source)), HINTS);
                return result.getText();
            } catch (Throwable e2) {
                e2.printStackTrace();
            }
        }
        return null;
    }
}
Also used : MultiFormatReader(com.google.zxing.MultiFormatReader) GlobalHistogramBinarizer(com.google.zxing.common.GlobalHistogramBinarizer) BinaryBitmap(com.google.zxing.BinaryBitmap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) RGBLuminanceSource(com.google.zxing.RGBLuminanceSource) Result(com.google.zxing.Result)

Example 2 with RGBLuminanceSource

use of com.google.zxing.RGBLuminanceSource 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 3 with RGBLuminanceSource

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

Aggregations

BinaryBitmap (com.google.zxing.BinaryBitmap)3 RGBLuminanceSource (com.google.zxing.RGBLuminanceSource)3 Result (com.google.zxing.Result)3 HybridBinarizer (com.google.zxing.common.HybridBinarizer)3 MultiFormatReader (com.google.zxing.MultiFormatReader)2 ReaderException (com.google.zxing.ReaderException)2 Bitmap (android.graphics.Bitmap)1 GlobalHistogramBinarizer (com.google.zxing.common.GlobalHistogramBinarizer)1