Search in sources :

Example 1 with RGBLuminanceSource

use of cn.cerc.summer.android.parts.barcode.zxing.decoding.RGBLuminanceSource in project summer-android by cn-cerc.

the class FrmScanBarcode method scanningImage.

/**
 * 识别二维码图片中的数据
 *
 * @param path
 * @return
 */
public Result scanningImage(String path) {
    if (TextUtils.isEmpty(path)) {
        return null;
    }
    Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();
    // 设置二维码内容的编码
    hints.put(DecodeHintType.CHARACTER_SET, "UTF8");
    BitmapFactory.Options options = new BitmapFactory.Options();
    // 先获取原大小
    options.inJustDecodeBounds = true;
    scanBitmap = BitmapFactory.decodeFile(path, options);
    // 获取新的大小
    options.inJustDecodeBounds = false;
    int sampleSize = (int) (options.outHeight / (float) 200);
    if (sampleSize <= 0)
        sampleSize = 1;
    options.inSampleSize = sampleSize;
    scanBitmap = BitmapFactory.decodeFile(path, options);
    RGBLuminanceSource source = new RGBLuminanceSource(scanBitmap);
    BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
    QRCodeReader reader = new QRCodeReader();
    try {
        return reader.decode(bitmap1, hints);
    } catch (NotFoundException e) {
        e.printStackTrace();
    } catch (ChecksumException e) {
        e.printStackTrace();
    } catch (FormatException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : QRCodeReader(com.google.zxing.qrcode.QRCodeReader) DecodeHintType(com.google.zxing.DecodeHintType) Hashtable(java.util.Hashtable) ChecksumException(com.google.zxing.ChecksumException) NotFoundException(com.google.zxing.NotFoundException) HybridBinarizer(com.google.zxing.common.HybridBinarizer) FormatException(com.google.zxing.FormatException) BitmapFactory(android.graphics.BitmapFactory) BinaryBitmap(com.google.zxing.BinaryBitmap) RGBLuminanceSource(cn.cerc.summer.android.parts.barcode.zxing.decoding.RGBLuminanceSource)

Aggregations

BitmapFactory (android.graphics.BitmapFactory)1 RGBLuminanceSource (cn.cerc.summer.android.parts.barcode.zxing.decoding.RGBLuminanceSource)1 BinaryBitmap (com.google.zxing.BinaryBitmap)1 ChecksumException (com.google.zxing.ChecksumException)1 DecodeHintType (com.google.zxing.DecodeHintType)1 FormatException (com.google.zxing.FormatException)1 NotFoundException (com.google.zxing.NotFoundException)1 HybridBinarizer (com.google.zxing.common.HybridBinarizer)1 QRCodeReader (com.google.zxing.qrcode.QRCodeReader)1 Hashtable (java.util.Hashtable)1