Search in sources :

Example 11 with BinaryBitmap

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

the class ByQuadrantReader method decode.

@Override
public Result decode(BinaryBitmap image, Map<DecodeHintType, ?> hints) throws NotFoundException, ChecksumException, FormatException {
    int width = image.getWidth();
    int height = image.getHeight();
    int halfWidth = width / 2;
    int halfHeight = height / 2;
    try {
        // No need to call makeAbsolute as results will be relative to original top left here
        return delegate.decode(image.crop(0, 0, halfWidth, halfHeight), hints);
    } catch (NotFoundException re) {
    // continue
    }
    try {
        Result result = delegate.decode(image.crop(halfWidth, 0, halfWidth, halfHeight), hints);
        makeAbsolute(result.getResultPoints(), halfWidth, 0);
        return result;
    } catch (NotFoundException re) {
    // continue
    }
    try {
        Result result = delegate.decode(image.crop(0, halfHeight, halfWidth, halfHeight), hints);
        makeAbsolute(result.getResultPoints(), 0, halfHeight);
        return result;
    } catch (NotFoundException re) {
    // continue
    }
    try {
        Result result = delegate.decode(image.crop(halfWidth, halfHeight, halfWidth, halfHeight), hints);
        makeAbsolute(result.getResultPoints(), halfWidth, halfHeight);
        return result;
    } catch (NotFoundException re) {
    // continue
    }
    int quarterWidth = halfWidth / 2;
    int quarterHeight = halfHeight / 2;
    BinaryBitmap center = image.crop(quarterWidth, quarterHeight, halfWidth, halfHeight);
    Result result = delegate.decode(center, hints);
    makeAbsolute(result.getResultPoints(), quarterWidth, quarterHeight);
    return result;
}
Also used : NotFoundException(com.google.zxing.NotFoundException) BinaryBitmap(com.google.zxing.BinaryBitmap) ResultPoint(com.google.zxing.ResultPoint) Result(com.google.zxing.Result)

Example 12 with BinaryBitmap

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

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

use of com.google.zxing.BinaryBitmap in project hutool by looly.

the class QrCodeUtil method decode.

/**
 * 将二维码图片解码为文本
 *
 * @param image {@link Image} 二维码图片
 * @return 解码后的文本
 */
public static String decode(Image image) {
    final MultiFormatReader formatReader = new MultiFormatReader();
    final LuminanceSource source = new BufferedImageLuminanceSource(ImageUtil.toBufferedImage(image));
    final Binarizer binarizer = new HybridBinarizer(source);
    final BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
    final HashMap<DecodeHintType, Object> hints = new HashMap<>();
    hints.put(DecodeHintType.CHARACTER_SET, CharsetUtil.UTF_8);
    Result result;
    try {
        result = formatReader.decode(binaryBitmap, hints);
    } catch (NotFoundException e) {
        throw new QrCodeException(e);
    }
    return result.getText();
}
Also used : MultiFormatReader(com.google.zxing.MultiFormatReader) DecodeHintType(com.google.zxing.DecodeHintType) HashMap(java.util.HashMap) NotFoundException(com.google.zxing.NotFoundException) HybridBinarizer(com.google.zxing.common.HybridBinarizer) Result(com.google.zxing.Result) LuminanceSource(com.google.zxing.LuminanceSource) BinaryBitmap(com.google.zxing.BinaryBitmap) Binarizer(com.google.zxing.Binarizer) HybridBinarizer(com.google.zxing.common.HybridBinarizer)

Example 15 with BinaryBitmap

use of com.google.zxing.BinaryBitmap in project android-zxing by PearceXu.

the class ByQuadrantReader method decode.

@Override
public Result decode(BinaryBitmap image, Map<DecodeHintType, ?> hints) throws NotFoundException, ChecksumException, FormatException {
    int width = image.getWidth();
    int height = image.getHeight();
    int halfWidth = width / 2;
    int halfHeight = height / 2;
    try {
        // No need to call makeAbsolute as results will be relative to original top left here
        return delegate.decode(image.crop(0, 0, halfWidth, halfHeight), hints);
    } catch (NotFoundException re) {
    // continue
    }
    try {
        Result result = delegate.decode(image.crop(halfWidth, 0, halfWidth, halfHeight), hints);
        makeAbsolute(result.getResultPoints(), halfWidth, 0);
        return result;
    } catch (NotFoundException re) {
    // continue
    }
    try {
        Result result = delegate.decode(image.crop(0, halfHeight, halfWidth, halfHeight), hints);
        makeAbsolute(result.getResultPoints(), 0, halfHeight);
        return result;
    } catch (NotFoundException re) {
    // continue
    }
    try {
        Result result = delegate.decode(image.crop(halfWidth, halfHeight, halfWidth, halfHeight), hints);
        makeAbsolute(result.getResultPoints(), halfWidth, halfHeight);
        return result;
    } catch (NotFoundException re) {
    // continue
    }
    int quarterWidth = halfWidth / 2;
    int quarterHeight = halfHeight / 2;
    BinaryBitmap center = image.crop(quarterWidth, quarterHeight, halfWidth, halfHeight);
    Result result = delegate.decode(center, hints);
    makeAbsolute(result.getResultPoints(), quarterWidth, quarterHeight);
    return result;
}
Also used : NotFoundException(com.google.zxing.NotFoundException) BinaryBitmap(com.google.zxing.BinaryBitmap) ResultPoint(com.google.zxing.ResultPoint) Result(com.google.zxing.Result)

Aggregations

BinaryBitmap (com.google.zxing.BinaryBitmap)78 Result (com.google.zxing.Result)62 HybridBinarizer (com.google.zxing.common.HybridBinarizer)61 ReaderException (com.google.zxing.ReaderException)39 Message (android.os.Message)24 Bundle (android.os.Bundle)23 BufferedImage (java.awt.image.BufferedImage)21 MultiFormatReader (com.google.zxing.MultiFormatReader)20 NotFoundException (com.google.zxing.NotFoundException)20 LuminanceSource (com.google.zxing.LuminanceSource)19 PlanarYUVLuminanceSource (com.google.zxing.PlanarYUVLuminanceSource)19 DecodeHintType (com.google.zxing.DecodeHintType)15 Handler (android.os.Handler)14 BufferedImageLuminanceSource (com.google.zxing.BufferedImageLuminanceSource)13 BufferedImageLuminanceSource (com.google.zxing.client.j2se.BufferedImageLuminanceSource)11 Hashtable (java.util.Hashtable)11 GlobalHistogramBinarizer (com.google.zxing.common.GlobalHistogramBinarizer)10 Test (org.junit.Test)10 BitArray (com.google.zxing.common.BitArray)8 Bitmap (android.graphics.Bitmap)7