zoukankan      html  css  js  c++  java
  • java生成二维码打印到浏览器

    java生成二维码打印到浏览器

     

    解决方法:

    pom.xml的依赖两个jar包:

    <!-- https://mvnrepository.com/artifact/com.google.zxing/core -->

    <dependency>

       <groupId>com.google.zxing</groupId>

       <artifactId>core</artifactId>

       <version>3.2.1</version>

    </dependency>

    <dependency>

       <groupId>com.google.zxing</groupId>

       <artifactId>javase</artifactId>

       <version>3.2.1</version>

    </dependency>

    源码:

    import com.google.zxing.BarcodeFormat;

    import com.google.zxing.EncodeHintType;

    import com.google.zxing.MultiFormatWriter;

    import com.google.zxing.WriterException;

    import com.google.zxing.client.j2se.MatrixToImageWriter;

    import com.google.zxing.common.BitMatrix;

    import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)

    throws ServletException, IOException {

    try {

       String payurl = "weixin://wxpay/bizpayur?";

           //生成二维码

       Map<EncodeHintType, Object>  hints=new HashMap<EncodeHintType, Object>();

           // 指定纠错等级  

           hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);  

           // 指定编码格式  

           hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");  

           hints.put(EncodeHintType.MARGIN, 1);

         

    BitMatrix bitMatrix = new MultiFormatWriter().encode(payurl,BarcodeFormat.QR_CODE, defaultWidthAndHeight, defaultWidthAndHeight, hints);

    OutputStream out = response.getOutputStream();

    MatrixToImageWriter.writeToStream(bitMatrix, "png", out);//输出二维码

               out.flush();

               out.close();

               

           } catch (Exception e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    }

  • 相关阅读:
    189. go学习1
    [Access][Microsoft][ODBC 驱动程序管理器] 无效的字符串或缓冲区长度 Invalid string or buffer length
    聊聊我对 GraphQL 的一些认知
    gin 源码阅读(2)
    gin 源码阅读(1)
    自动化测试感悟——感悟10条
    Python转exe神器pyinstaller
    在用Python时遇到的坑
    Python BeautifulSoup库 常用方法
    DCDC反馈电路串联的电阻
  • 原文地址:https://www.cnblogs.com/chinaifae/p/10188854.html
Copyright © 2011-2022 走看看