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();

    }

    }

  • 相关阅读:
    前端使用 node-gyp 构建 Native Addon
    CHANGELOG 的实现
    深入 JavaScript 中的对象以及继承原理
    使用electron进行原生应用的打包(2)---主进程与渲染进程之间的通信
    使用electron进行原生应用的打包
    Babel编译
    HTML布局四剑客-Flex,Grid,Table,Float
    关于vtt 与 srt 字幕 的相互转换
    关于websocket
    关于jQuery中nth-child和nth-of-type的详解
  • 原文地址:https://www.cnblogs.com/chinaifae/p/10188854.html
Copyright © 2011-2022 走看看