• 使用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)

    完毕!

  • 相关阅读:
    DataTables 控件使用和心得 (2)
    dataTables 添加行内操作按钮
    datatables 配套bootstrap3样式使用小结(1)
    loadrunner总体使用篇
    使用CEF类库处理HTTP请求
    27部优秀的黑客纪录片
    CEF3开发者系列之进程和线程
    cef3的各个接口你知道几个
    [你必须知道的.NET]第二十九回:.NET十年(上)
    [你必须知道的.NET]第二十八回:说说Name这回事儿
  • 原文地址:https://www.cnblogs.com/routine/p/10421439.html
Copyright © 2020-2023  润新知