补充说明:识别文件中的条形码-zxing
/**
* 根据图片路径解析图片中的条形码,返回Result对象
*
* @param path
* @return
*/
protected Result scanningImage(String path) {
if (TextUtils.isEmpty(path)) {
return null;
}
// DecodeHintType 和EncodeHintType
Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();
hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); //
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();
MultiFormatReader multiFormatReader = new MultiFormatReader();
try {
return multiFormatReader.decode(bitmap1, hints);
} catch (NotFoundException e) {
e.printStackTrace();
}
return null;
}
在识别过程主要使用了MultiFormatReader
类,它可以同时解析一维/二维的条码,而QRCodeReader
类仅能识别二维码。