zoukankan      html  css  js  c++  java
  • 使用zxing二维码识别

    1.多二维码识别 (同一张图片中多二维码识别)

      直接上代码舒服:

    pom文件:

    1         <!-- QR Code -->
    2         <dependency>
    3             <groupId>com.google.zxing</groupId>
    4             <artifactId>core</artifactId>
    5             <version>3.3.3</version>
    6         </dependency>、
     1    /**
     2      * Parse multiple qr codes(解析多个二维码)
     3      *  使用java实现
     4      *
     5      * @param bufferedImage image
     6      * @return QRCode analysis results
     7      */
     8     @Override
     9     public Result[] analysisQRCodeOfMore(BufferedImage bufferedImage) {
    10         QRCodeMultiReader qrCodeMultiReader = new QRCodeMultiReader();
    11         Result[] results = null;
    12         try {
    13             BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage)));
    14             Map hints = new Hashtable();
    15             hints.put(EncodeHintType.CHARACTER_SET, SystemConstants.SYS_ENC);
    16             hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);   //优化精度
    17             hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE); //复杂模式,开启PURE_BARCODE模式; 带图片LOGO的解码方案
    18             hints.put(DecodeHintType.POSSIBLE_FORMATS,BarcodeFormat.QR_CODE);//指定识别的二维码格式
    19             results = qrCodeMultiReader.decodeMultiple(binaryBitmap, hints);
    20         } catch (NotFoundException e) {
    21             //e.printStackTrace();
    22             System.err.println("二维码识别中...");
    23         }
    24         return results;
    25     }

    注意:开启精度优化与复杂模式会消耗识别时间!

    在多二维码识别中,应该说是zxing的一个小bug,在识别过程中对于模糊一点的图片,会一直抛异常 (二维码识别中...) 直至二维码识别不出来,或者二维码识别出来;此问题没来得及细细研究,等日后补充;

    2.单二维码识别:

     1     private String decodeQRCode(Mat src) {
     2         if (src == null) {
     3             return null;
     4         }
     5         BufferedImage image;
     6         try {
     7             image = ImageUtils.mat2BufferedImage(src, ".jpg");
     8             LuminanceSource source = new BufferedImageLuminanceSource(image);
     9             Binarizer binarizer = new HybridBinarizer(source);
    10             BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
    11             Map<DecodeHintType, Object> hints = new HashMap<>();
    12             hints.put(DecodeHintType.CHARACTER_SET, SystemConstants.SYS_ENC);
    13             hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
    14             Result resultText = new MultiFormatReader().decode(binaryBitmap, hints);
    15             return resultText.getText();
    16         } catch (NotFoundException e) {
    17             LogUtils.info("[QRCodeServiceImpl] decodeQRCode image transform failed.");
    18         }
    19         return null;
    20     }

    以上代码使用opencv格式的图片来转换为BufferedImage为需要的入参;

    3. (扩展) 多二维码识别与多二维码识别不同点:

    1  public Result[] decodeMultiple(BinaryBitmap image, Map<DecodeHintType,?> hints)

    单二维码识别函数:

    1 public Result decode(BinaryBitmap image, Map<DecodeHintType,?> hints)

    完毕!

  • 相关阅读:
    CSS从大图片上截取小图标的操作以及三角形的画法
    CSS3样式问题
    spilt()的用法
    如何测试一个网站
    在C/C++中static有什么用途
    对集成测试中自顶向下集成和自底向上集成两个策略的理解
    缺陷记录应包含的内容?
    主键、外键的作用,索引的优点与不足
    什么是兼容性测试
    测试用例的作用
  • 原文地址:https://www.cnblogs.com/routine/p/10421439.html
Copyright © 2011-2022 走看看