使用zxing解析二维码抛出com.google.zxing.NotFoundException 解决方案

普通的二维码解析的时候,没什么问题。当二维码复杂了,或者是中间有LOGO的时候就报错,而且错误还看不出来具体错误信息,就一个com.google.zxing.NotFoundException,感觉这个处理的有点恶心。

搜索了一些资料不是我的解决的方案,不过也罗列一下:

1.二维码所有bit都是0,然后分析了一下,发现我在生成二维码的时候白色像素填充使用的是透明色,这样在显示的时候因为背景是白色,所以看上去和用手机扫都没有问题,但是自己代码识别的时候就会把透明色识别为黑色,这样就导致整个二维码图片全是黑色像素,所以zxing抛出com.google.zxing.NotFoundException异常。

2.乱码。


  
  
  1. // 解码设置编码方式为:utf-8,
  2. hints.put(DecodeHintType.CHARACTER_SET, CHARSET);

3.优化精度。


  
  
  1. //优化精度
  2. hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);

4.开启PURE_BARCODE模式。(这是解决我的方案,带图片LOGO的解码方案)


  
  
  1. //复杂模式,开启PURE_BARCODE模式
  2. hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);

第四个方案,是解决我的问题的方案,你们可以试试。

It decodes fine, but if you intend to decode a complex pure image you probably want PURE_BARCODE mode.

附上我的解码代码源码。


  
  
  1. /**
  2. * 流图片解码
  3. * @param input
  4. * @return QRResult
  5. */
  6. public static QRResult decode(InputStream input) {
  7. BufferedImage image;
  8. try {
  9. if (null == input) {
  10. return new QRResult("得到的文件不存在!",300);
  11. }
  12. image = ImageIO.read(input);
  13. LuminanceSource source = new BufferedImageLuminanceSource(image);
  14. BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
  15. Map<DecodeHintType,Object> hints = new LinkedHashMap<DecodeHintType,Object>();
  16. // 解码设置编码方式为:utf-8,
  17. hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
  18. //优化精度
  19. hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
  20. //复杂模式,开启PURE_BARCODE模式
  21. hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
  22. Result result = new MultiFormatReader().decode(bitmap, hints);
  23. String txt = result.getText();
  24. return new QRResult("成功解码!",200,txt);
  25. } catch (Exception e) {
  26. LoggerUtils.error(MatrixUtil.class,"解码失败。", e);
  27. return new QRResult("解码失败,请确认的你二维码是否正确,或者图片有多个二维码!",500);
  28. }
  29. }
  30. /**
  31. * 返回值处理
  32. * @author zhou-baicheng
  33. *
  34. */
  35. public static class QRResult{
  36. public QRResult(String message,int status) {
  37. this.message = message;
  38. this.status = status;
  39. this.txt = "";
  40. }
  41. public QRResult(String message,int status,String txt) {
  42. this.message = message;
  43. this.status = status;
  44. this.txt = txt;
  45. }
  46. //解码内容
  47. private String txt;
  48. //返回的消息内容
  49. private String message;
  50. //返回的状态码,200:成功,500:错误
  51. private int status ;
  52. public String getMessage() {
  53. return message;
  54. }
  55. public void setMessage(String message) {
  56. this.message = message;
  57. }
  58. public int getStatus() {
  59. return status;
  60. }
  61. public void setStatus(int status) {
  62. this.status = status;
  63. }
  64. public String getTxt() {
  65. return txt;
  66. }
  67. public void setTxt(String txt) {
  68. this.txt = txt;
  69. }
  70. }
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值