Search in sources :

Example 1 with BitmapLuminanceSource

use of com.uuzuche.lib_zxing.camera.BitmapLuminanceSource in project android-zxingLibrary by yipianfengye.

the class CodeUtils method analyzeBitmap.

/**
     * 解析二维码图片工具类
     * @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();
        }
    }
}
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) WriterException(com.google.zxing.WriterException) Result(com.google.zxing.Result) Bitmap(android.graphics.Bitmap) BinaryBitmap(com.google.zxing.BinaryBitmap) BitmapLuminanceSource(com.uuzuche.lib_zxing.camera.BitmapLuminanceSource) BitmapFactory(android.graphics.BitmapFactory) BinaryBitmap(com.google.zxing.BinaryBitmap) Vector(java.util.Vector)

Aggregations

Bitmap (android.graphics.Bitmap)1 BitmapFactory (android.graphics.BitmapFactory)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 WriterException (com.google.zxing.WriterException)1 HybridBinarizer (com.google.zxing.common.HybridBinarizer)1 BitmapLuminanceSource (com.uuzuche.lib_zxing.camera.BitmapLuminanceSource)1 Hashtable (java.util.Hashtable)1 Vector (java.util.Vector)1