Search in sources :

Example 71 with BinaryBitmap

use of com.google.zxing.BinaryBitmap in project incubator-weex by apache.

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 72 with BinaryBitmap

use of com.google.zxing.BinaryBitmap in project keepass2android by PhilippC.

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) {
    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;
    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 73 with BinaryBitmap

use of com.google.zxing.BinaryBitmap in project titanium-barcode by mwaylabs.

the class DecodeThread 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 = CameraManager.get().buildLuminanceSource(data, width, height);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    try {
        rawResult = multiFormatReader.decodeWithState(bitmap);
    } catch (ReaderException re) {
        // Log and continue
        Log.d("DecodeThread", re.toString());
    } finally {
        multiFormatReader.reset();
    }
    if (rawResult != null) {
        long end = System.currentTimeMillis();
        Log.v(TAG, "Found barcode (" + (end - start) + " ms):\n" + rawResult.toString());
        Message message = Message.obtain(activity.getHandler(), Id.DECODE_SUCCEEDED, rawResult);
        Bundle bundle = new Bundle();
        bundle.putParcelable(BARCODE_BITMAP, source.renderCroppedGreyscaleBitmap());
        message.setData(bundle);
        message.sendToTarget();
    } else {
        Message message = Message.obtain(activity.getHandler(), Id.DECODE_FAILED);
        message.sendToTarget();
    }
}
Also used : Message(android.os.Message) 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 74 with BinaryBitmap

use of com.google.zxing.BinaryBitmap in project QrCodeScanner by ekibun.

the class Decoder method decode.

public static Result decode(byte[] data, int width, int height, Rect crop) {
    Result result = null;
    try {
        Hashtable<DecodeHintType, Object> hints = new Hashtable<>();
        hints.put(DecodeHintType.CHARACTER_SET, "ISO-8859-1");
        hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
        hints.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE);
        /*
            Collection<BarcodeFormat> barcodeFormats = new ArrayList<>();
            barcodeFormats.add(BarcodeFormat.QR_CODE);
            barcodeFormats.add(BarcodeFormat.CODE_39);
            barcodeFormats.add(BarcodeFormat.CODE_93);
            barcodeFormats.add(BarcodeFormat.CODE_128);
            hints.put(DecodeHintType.POSSIBLE_FORMATS, barcodeFormats);*/
        PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data, width, height, crop.left, crop.top, crop.width(), crop.height(), false);
        // BinaryBitmap bitmap1 = new BinaryBitmap(new GlobalHistogramBinarizer(source));
        BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
        result = new QRCodeReader().decode(bitmap1, hints);
    } catch (NotFoundException e) {
    // e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
Also used : QRCodeReader(com.google.zxing.qrcode.QRCodeReader) DecodeHintType(com.google.zxing.DecodeHintType) Hashtable(java.util.Hashtable) PlanarYUVLuminanceSource(com.google.zxing.PlanarYUVLuminanceSource) NotFoundException(com.google.zxing.NotFoundException) BinaryBitmap(com.google.zxing.BinaryBitmap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) NotFoundException(com.google.zxing.NotFoundException) ReaderException(com.google.zxing.ReaderException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Result(com.google.zxing.Result)

Example 75 with BinaryBitmap

use of com.google.zxing.BinaryBitmap in project HL4A by HL4A.

the class 二维码 method 同步解析.

public static 返回值<String> 同步解析(Bitmap $图片) {
    MultiFormatReader $解析器 = new MultiFormatReader();
    // 解码的参数
    Hashtable<DecodeHintType, Object> $设置 = new Hashtable<DecodeHintType, Object>(2);
    // 可以解析的编码类型
    无序集合<BarcodeFormat> $所有格式 = new 无序集合<BarcodeFormat>(反射.取所有枚举(BarcodeFormat.class));
    $设置.put(DecodeHintType.POSSIBLE_FORMATS, $所有格式);
    // 设置继续的字符编码格式为UTF8
    // 设置.put(DecodeHintType.CHARACTER_SET, "UTF8");
    // 设置解析配置参数
    $解析器.setHints($设置);
    try {
        return 返回值.创建($解析器.decodeWithState(new BinaryBitmap(new HybridBinarizer(new 图片源码($图片)))).getText());
    } catch (Exception $错误) {
        return 返回值.创建(null, $错误);
    }
}
Also used : MultiFormatReader(com.google.zxing.MultiFormatReader) DecodeHintType(com.google.zxing.DecodeHintType) 间.收集.无序集合(间.收集.无序集合) Hashtable(java.util.Hashtable) BarcodeFormat(com.google.zxing.BarcodeFormat) BinaryBitmap(com.google.zxing.BinaryBitmap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) WriterException(com.google.zxing.WriterException) NotFoundException(com.google.zxing.NotFoundException)

Aggregations

BinaryBitmap (com.google.zxing.BinaryBitmap)81 Result (com.google.zxing.Result)64 HybridBinarizer (com.google.zxing.common.HybridBinarizer)63 ReaderException (com.google.zxing.ReaderException)40 Message (android.os.Message)25 Bundle (android.os.Bundle)24 MultiFormatReader (com.google.zxing.MultiFormatReader)21 NotFoundException (com.google.zxing.NotFoundException)21 BufferedImage (java.awt.image.BufferedImage)21 LuminanceSource (com.google.zxing.LuminanceSource)19 PlanarYUVLuminanceSource (com.google.zxing.PlanarYUVLuminanceSource)19 DecodeHintType (com.google.zxing.DecodeHintType)16 Handler (android.os.Handler)14 BufferedImageLuminanceSource (com.google.zxing.BufferedImageLuminanceSource)13 Hashtable (java.util.Hashtable)12 BufferedImageLuminanceSource (com.google.zxing.client.j2se.BufferedImageLuminanceSource)11 GlobalHistogramBinarizer (com.google.zxing.common.GlobalHistogramBinarizer)10 Test (org.junit.Test)10 Bitmap (android.graphics.Bitmap)8 BitArray (com.google.zxing.common.BitArray)8