多个二维码的图片读取返回数组,在异常里处理
public static String decode(File file) throws Exception {
BufferedImage image;
image = ImageIO.read(file);
if (image == null) {
return null;
}
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result=null;
Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
//优化精度
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
//复杂模式,开启PURE_BARCODE模式
hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
try{
result = new MultiFormatReader().decode(bitmap, hints);
}catch(Exception e){
e.printStackTrace();
QRCodeMultiReader qc = new QRCodeMultiReader();//一张图片有多张二维码取最后一个
Result[] r = qc.decodeMultiple(bitmap, hints);
if(r!=null && r.length>0){
String resultStrTemp = r[r.length-1].getText();
return resultStrTemp;
}
}
String resultStr = result.getText();
return resultStr;
}