使用zxing识别图片二维码com.google.zxing.NotFoundException的问题
最开始只使用了扫描单个二维码的方法,一直com.google.zxing.NotFoundException
后面增加了扫描多个二维码方法成功了。
public class QrBarToolUtils {
//一张图有多个二维码
public static Result[] decodeMultiFromPhoto(Bitmap photo) {
HashMap<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
hints.put(DecodeHintType.POSSIBLE_FORMATS, EnumSet.allOf(BarcodeFormat.class));
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8"); // 指定编码方式,防止中文乱码
Result[] rawResults = null;
int width = photo.getWidth();
int height = photo.getHeight();
int[] pixels = new int[width * height];
photo.getPixels(pixels, 0, width, 0, 0, width, height);
// 新建一个RGBLuminanceSource对象
RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);
// 将图片转换成二进制图片
QRCodeMultiReader reader = new QRCodeMultiReader();// 初始化解析对象
try {
rawResults = reader.decodeMultiple(new BinaryBitmap(new HybridBinarizer(source)), hints);
} catch (Exception e) {
e.printStackTrace();
try {
rawResults = reader.decodeMultiple(new BinaryBitmap(new GlobalHistogramBinarizer(source)), hints);
} catch (Exception secondE) {
secondE.printStackTrace();
}
}
return rawResults;
}
/**
*/
public static Result decodeFromPhoto(Bitmap smallBitmap ) {
Result rawResult = null;
if (smallBitmap != null) {
//注释掉 回收bitmap 容易出问题。
//Bitmap smallBitmap = ImageToolUtils.zoomBitmap(photo, photo.getWidth() / 2, photo.getHeight() / 2);// 为防止原始图片过大导致内存溢出,这里先缩小原图显示,然后释放原始Bitmap占用的内存
//photo.recycle(); // 释放原始图片占用的内存,防止out of memory异常发生
MultiFormatReader multiFormatReader = new MultiFormatReader();
// 解码的参数
Hashtable<DecodeHintType, Object> hints = new Hashtable<>(2);
// 可以解析的编码类型
Vector<BarcodeFormat> decodeFormats = new Vector<>();
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);
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8"); // 指定编码方式,防止中文乱码
// 设置继续的字符编码格式为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();
try {
rawResult = multiFormatReader.decodeWithState(new BinaryBitmap(new GlobalHistogramBinarizer(new BitmapLuminanceSource(smallBitmap))));
} catch (Exception secondE) {
secondE.printStackTrace();
}
}
if (rawResult == null) {
Result[] rawResults = decodeMultiFromPhoto(smallBitmap);
if (rawResults != null && rawResults.length > 0) {
rawResult = rawResults[0];
}
}
}
return rawResult;
}
}
代码调用
QrBarToolUtils.decodeFromPhoto(bitmap)
代码是demo可以优化,自行优化吧。