码上敲享录 > java入门知识分享 > java生成二维码打印到浏览器

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

上一章章节目录下一章 2018-08-09已有1901人阅读 评论(0)

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

}

}

本文链接:http://www.yayihouse.com/yayishuwu/chapter/1451

0

有建议,请留言!

  • *您的姓名:

  • *所在城市:

  • *您的联系电话:

    *您的QQ:

  • 咨询问题:

  • 提 交