Search in sources :

Example 1 with BitmapLuminanceSource

use of com.vondear.rxtools.module.scaner.BitmapLuminanceSource in project RxTools by vondear.

the class RxQrBarTool method decodeFromPhoto.

/**
 * 解析图片中的 二维码 或者 条形码
 *
 * @param photo 待解析的图片
 * @return Result 解析结果,解析识别时返回NULL
 */
public static Result decodeFromPhoto(Bitmap photo) {
    Result rawResult = null;
    if (photo != null) {
        // 为防止原始图片过大导致内存溢出,这里先缩小原图显示,然后释放原始Bitmap占用的内存
        Bitmap smallBitmap = RxImageTool.zoomBitmap(photo, photo.getWidth() / 2, photo.getHeight() / 2);
        // 释放原始图片占用的内存,防止out of memory异常发生
        photo.recycle();
        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;
}
Also used : MultiFormatReader(com.google.zxing.MultiFormatReader) DecodeHintType(com.google.zxing.DecodeHintType) Hashtable(java.util.Hashtable) BarcodeFormat(com.google.zxing.BarcodeFormat) HybridBinarizer(com.google.zxing.common.HybridBinarizer) Result(com.google.zxing.Result) Bitmap(android.graphics.Bitmap) BinaryBitmap(com.google.zxing.BinaryBitmap) BitmapLuminanceSource(com.vondear.rxtools.module.scaner.BitmapLuminanceSource) BinaryBitmap(com.google.zxing.BinaryBitmap) Vector(java.util.Vector)

Aggregations

Bitmap (android.graphics.Bitmap)1 BarcodeFormat (com.google.zxing.BarcodeFormat)1 BinaryBitmap (com.google.zxing.BinaryBitmap)1 DecodeHintType (com.google.zxing.DecodeHintType)1 MultiFormatReader (com.google.zxing.MultiFormatReader)1 Result (com.google.zxing.Result)1 HybridBinarizer (com.google.zxing.common.HybridBinarizer)1 BitmapLuminanceSource (com.vondear.rxtools.module.scaner.BitmapLuminanceSource)1 Hashtable (java.util.Hashtable)1 Vector (java.util.Vector)1