Java Code Examples for com.google.zxing.MultiFormatReader#decodeWithState()

The following examples show how to use com.google.zxing.MultiFormatReader#decodeWithState() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DecodeUtils.java    From myapplication with Apache License 2.0 6 votes vote down vote up
public Result 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);

    BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));
    try {
        rawResult = multiFormatReader.decodeWithState(binaryBitmap);
    } catch (ReaderException re) {
        // continue
    } finally {
        multiFormatReader.reset();
    }

    return rawResult;
}
 
Example 2
Source File: DecodeUtils.java    From SimplifyReader with Apache License 2.0 6 votes vote down vote up
public String decodeWithZxing(byte[] data, int width, int height, Rect crop) {
    MultiFormatReader multiFormatReader = new MultiFormatReader();
    multiFormatReader.setHints(changeZXingDecodeDataMode());

    Result rawResult = null;
    PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data, width, height,
            crop.left, crop.top, crop.width(), crop.height(), false);

    if (source != null) {
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        try {
            rawResult = multiFormatReader.decodeWithState(bitmap);
        } catch (ReaderException re) {
            // continue
        } finally {
            multiFormatReader.reset();
        }
    }

    return rawResult != null ? rawResult.getText() : null;
}
 
Example 3
Source File: DecodeUtils.java    From SimplifyReader with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 4
Source File: Utils.java    From code-scanner with MIT License 5 votes vote down vote up
@Nullable
public static Result decodeLuminanceSource(@NonNull final MultiFormatReader reader,
        @NonNull final LuminanceSource luminanceSource) throws ReaderException {
    try {
        return reader.decodeWithState(new BinaryBitmap(new HybridBinarizer(luminanceSource)));
    } catch (final NotFoundException e) {
        return reader.decodeWithState(
                new BinaryBitmap(new HybridBinarizer(luminanceSource.invert())));
    } finally {
        reader.reset();
    }
}
 
Example 5
Source File: CodeUtils.java    From UVCCameraZxing with Apache License 2.0 5 votes vote down vote up
public static void analyzeBitmap(Bitmap bitmap, AnalyzeCallback analyzeCallback) {
    MultiFormatReader multiFormatReader = new MultiFormatReader();

    // 解码的参数
    Hashtable<DecodeHintType, Object> hints = new Hashtable<>(2);
    // 可以解析的编码类型
    Vector<BarcodeFormat> decodeFormats = new Vector<>();
    // 这里设置可扫描的类型,我这里选择了都支持
    decodeFormats.addAll(DecodeFormatManager.ONE_D_FORMATS);
    decodeFormats.addAll(DecodeFormatManager.QR_CODE_FORMATS);
    decodeFormats.addAll(DecodeFormatManager.DATA_MATRIX_FORMATS);

    hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
    // 设置继续的字符编码格式为UTF8
    // hints.put(DecodeHintType.CHARACTER_SET, "UTF8");
    // 设置解析配置参数
    multiFormatReader.setHints(hints);

    // 开始对图像资源解码
    Result rawResult = null;
    try {
        rawResult = multiFormatReader.decodeWithState(
                new BinaryBitmap(new HybridBinarizer(new BitmapLuminanceSource(bitmap))));
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (rawResult != null) {
        if (analyzeCallback != null) {
            analyzeCallback.onAnalyzeSuccess(bitmap, rawResult.getText());
        }
    } else {
        if (analyzeCallback != null) {
            analyzeCallback.onAnalyzeFailed();
        }
    }
}
 
Example 6
Source File: RxQrBarTool.java    From RxTools-master with Apache License 2.0 4 votes vote down vote up
/**
 * 解析图片中的 二维码 或者 条形码
 *
 * @param photo 待解析的图片
 * @return Result 解析结果,解析识别时返回NULL
 */
public static Result decodeFromPhoto(Bitmap photo) {
    Result rawResult = null;
    if (photo != null) {
        Bitmap smallBitmap = RxImageTool.zoomBitmap(photo, photo.getWidth() / 2, photo.getHeight() / 2);// 为防止原始图片过大导致内存溢出,这里先缩小原图显示,然后释放原始Bitmap占用的内存
        photo.recycle(); // 释放原始图片占用的内存,防止out of memory异常发生

        MultiFormatReader multiFormatReader = new MultiFormatReader();

        // 解码的参数
        Hashtable<DecodeHintType, Object> hints = new Hashtable<>(2);
        // 可以解析的编码类型
        Vector<BarcodeFormat> decodeFormats = new Vector<>();
        if (decodeFormats.isEmpty()) {
            decodeFormats = new Vector<>();

            Vector<BarcodeFormat> PRODUCT_FORMATS = new Vector<>(5);
            PRODUCT_FORMATS.add(BarcodeFormat.UPC_A);
            PRODUCT_FORMATS.add(BarcodeFormat.UPC_E);
            PRODUCT_FORMATS.add(BarcodeFormat.EAN_13);
            PRODUCT_FORMATS.add(BarcodeFormat.EAN_8);
            // PRODUCT_FORMATS.add(BarcodeFormat.RSS14);
            Vector<BarcodeFormat> ONE_D_FORMATS = new Vector<>(PRODUCT_FORMATS.size() + 4);
            ONE_D_FORMATS.addAll(PRODUCT_FORMATS);
            ONE_D_FORMATS.add(BarcodeFormat.CODE_39);
            ONE_D_FORMATS.add(BarcodeFormat.CODE_93);
            ONE_D_FORMATS.add(BarcodeFormat.CODE_128);
            ONE_D_FORMATS.add(BarcodeFormat.ITF);
            Vector<BarcodeFormat> QR_CODE_FORMATS = new Vector<>(1);
            QR_CODE_FORMATS.add(BarcodeFormat.QR_CODE);
            Vector<BarcodeFormat> DATA_MATRIX_FORMATS = new Vector<>(1);
            DATA_MATRIX_FORMATS.add(BarcodeFormat.DATA_MATRIX);

            // 这里设置可扫描的类型,我这里选择了都支持
            decodeFormats.addAll(ONE_D_FORMATS);
            decodeFormats.addAll(QR_CODE_FORMATS);
            decodeFormats.addAll(DATA_MATRIX_FORMATS);
        }
        hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
        // 设置继续的字符编码格式为UTF8
        // hints.put(DecodeHintType.CHARACTER_SET, "UTF8");
        // 设置解析配置参数
        multiFormatReader.setHints(hints);

        // 开始对图像资源解码
        try {
            rawResult = multiFormatReader.decodeWithState(new BinaryBitmap(new HybridBinarizer(new BitmapLuminanceSource(smallBitmap))));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return rawResult;
}
 
Example 7
Source File: DecodeImgThread.java    From zxing with MIT License 4 votes vote down vote up
@Override
public void run() {
    super.run();

    if (TextUtils.isEmpty(imgPath) || callback == null) {
        return;
    }

    Bitmap scanBitmap = getBitmap(imgPath, 400, 400);

    MultiFormatReader multiFormatReader = new MultiFormatReader();
    // 解码的参数
    Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(2);
    // 可以解析的编码类型
    Vector<BarcodeFormat> decodeFormats = new Vector<BarcodeFormat>();


    // 扫描的类型  一维码和二维码
    decodeFormats.addAll(DecodeFormatManager.ONE_D_FORMATS);
    decodeFormats.addAll(DecodeFormatManager.QR_CODE_FORMATS);
    decodeFormats.addAll(DecodeFormatManager.DATA_MATRIX_FORMATS);

    hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
    // 设置解析的字符编码格式为UTF8
    //  hints.put(DecodeHintType.CHARACTER_SET, "UTF8");
    // 设置解析配置参数
    multiFormatReader.setHints(hints);
    // 开始对图像资源解码
    Result rawResult = null;
    try {
        rawResult = multiFormatReader.decodeWithState(new BinaryBitmap(new HybridBinarizer(new BitmapLuminanceSource(scanBitmap))));

        Log.i("解析结果", rawResult.getText());

    } catch (Exception e) {
        e.printStackTrace();
        //  Log.i("解析的图片结果","失败");
    }

    if (rawResult != null) {
        callback.onImageDecodeSuccess(rawResult);
    } else {
        callback.onImageDecodeFailed();
    }


}
 
Example 8
Source File: ZxingUtil.java    From smart-farmer-android with Apache License 2.0 4 votes vote down vote up
/**
 * 解析二维码图片工具类
 */
public static void analyzeBitmap(String path, AnalyzeCallback analyzeCallback) {

    /**
     * 首先判断图片的大小,若图片过大,则执行图片的裁剪操作,防止OOM
     */
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true; // 先获取原大小
    Bitmap mBitmap = BitmapFactory.decodeFile(path, options);
    options.inJustDecodeBounds = false; // 获取新的大小

    int sampleSize = (int) (options.outHeight / (float) 400);

    if (sampleSize <= 0)
        sampleSize = 1;
    options.inSampleSize = sampleSize;
    mBitmap = BitmapFactory.decodeFile(path, options);

    MultiFormatReader multiFormatReader = new MultiFormatReader();

    // 解码的参数
    Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(2);
    // 可以解析的编码类型
    Vector<BarcodeFormat> decodeFormats = new Vector<BarcodeFormat>();
    if (decodeFormats == null || decodeFormats.isEmpty()) {
        decodeFormats = new Vector<BarcodeFormat>();

        // 这里设置可扫描的类型,我这里选择了都支持
        decodeFormats.addAll(DecodeFormatManager.ONE_D_FORMATS);
        decodeFormats.addAll(DecodeFormatManager.QR_CODE_FORMATS);
        decodeFormats.addAll(DecodeFormatManager.DATA_MATRIX_FORMATS);
    }
    hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
    // 设置继续的字符编码格式为UTF8
    // hints.put(DecodeHintType.CHARACTER_SET, "UTF8");
    // 设置解析配置参数
    multiFormatReader.setHints(hints);

    // 开始对图像资源解码
    Result rawResult = null;
    try {
        rawResult = multiFormatReader.decodeWithState(new BinaryBitmap(new HybridBinarizer(new BitmapLuminanceSource(mBitmap))));
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (rawResult != null) {
        if (analyzeCallback != null) {
            analyzeCallback.onAnalyzeSuccess(mBitmap, rawResult.getText());
        }
    } else {
        if (analyzeCallback != null) {
            analyzeCallback.onAnalyzeFailed();
        }
    }
}
 
Example 9
Source File: CodeUtils.java    From vinci with Apache License 2.0 4 votes vote down vote up
/**
 * 解析二维码图片工具类
 *
 * @param analyzeCallback
 */
public static void analyzeBitmap(String path, AnalyzeCallback analyzeCallback) {

    /**
     * 首先判断图片的大小,若图片过大,则执行图片的裁剪操作,防止OOM
     */
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true; // 先获取原大小
    Bitmap mBitmap = BitmapFactory.decodeFile(path, options);
    options.inJustDecodeBounds = false; // 获取新的大小

    int sampleSize = (int) (options.outHeight / (float) 400);

    if (sampleSize <= 0)
        sampleSize = 1;
    options.inSampleSize = sampleSize;
    mBitmap = BitmapFactory.decodeFile(path, options);

    MultiFormatReader multiFormatReader = new MultiFormatReader();

    // 解码的参数
    Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(2);
    // 可以解析的编码类型
    Vector<BarcodeFormat> decodeFormats = new Vector<BarcodeFormat>();
    if (decodeFormats == null || decodeFormats.isEmpty()) {
        decodeFormats = new Vector<BarcodeFormat>();

        // 这里设置可扫描的类型,我这里选择了都支持
        decodeFormats.addAll(DecodeFormatManager.ONE_D_FORMATS);
        decodeFormats.addAll(DecodeFormatManager.QR_CODE_FORMATS);
        decodeFormats.addAll(DecodeFormatManager.DATA_MATRIX_FORMATS);
    }
    hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
    // 设置继续的字符编码格式为UTF8
    // hints.put(DecodeHintType.CHARACTER_SET, "UTF8");
    // 设置解析配置参数
    multiFormatReader.setHints(hints);

    // 开始对图像资源解码
    Result rawResult = null;
    try {
        rawResult = multiFormatReader.decodeWithState(new BinaryBitmap(new HybridBinarizer(new BitmapLuminanceSource(mBitmap))));
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (rawResult != null) {
        if (analyzeCallback != null) {
            analyzeCallback.onAnalyzeSuccess(mBitmap, rawResult.getText());
        }
    } else {
        if (analyzeCallback != null) {
            analyzeCallback.onAnalyzeFailed();
        }
    }
}