使用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. }
  • 11
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
`com.google.zxing.NotFoundException` 是一个异常类,表示在使用 ZXing 库进行二维码识别时未能找到有效的二维码。通常出现这种情况是因为图片中不存在二维码二维码不清晰或者损坏等原因。 要解决这个问题,可以尝试以下几种方法: 1. 确保图片中存在二维码使用图片编辑软件查看图片中是否存在二维码,或者使用其他二维码识别工具验证图片中是否存在二维码。 2. 确保二维码清晰:使用高清晰度的图片进行识别,或者尝试调整图片的对比度和亮度。 3. 使用多个二维码识别算法:ZXing 库提供了多个二维码识别算法,可以尝试使用不同的算法进行识别。 以下是一个使用 ZXing 库进行二维码识别的示例代码: ```java import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import com.google.zxing.BinaryBitmap; import com.google.zxing.MultiFormatReader; import com.google.zxing.NotFoundException; import com.google.zxing.Result; import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.common.HybridBinarizer; public class QRCodeReader { public static void main(String[] args) { try { File file = new File("qrcode.png"); BufferedImage image = ImageIO.read(file); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image))); Result result = new MultiFormatReader().decode(bitmap); System.out.println(result.getText()); } catch (IOException e) { e.printStackTrace(); } catch (NotFoundException e) { e.printStackTrace(); } } } ``` 在上面的示例代码中,我们使用 `MultiFormatReader` 类进行二维码识别。如果识别失败,则会抛出 `NotFoundException` 异常,可以根据异常信息进行排查。
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值