Search in sources :

Example 81 with BinaryBitmap

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

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