use of com.google.zxing.zxing.image.RGBLuminanceSource in project QRCode by 5peak2me.
the class MipcaActivityCapture 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;
}
Aggregations