java生成二维码

 1 import com.google.zxing.BarcodeFormat;
 2 import com.google.zxing.WriterException;
 3 import com.google.zxing.client.j2se.MatrixToImageWriter;
 4 import com.google.zxing.common.BitMatrix;
 5 import com.google.zxing.qrcode.QRCodeWriter;
 6 
 7 import javax.servlet.ServletOutputStream;
 8 import javax.servlet.http.HttpServletResponse;
 9 import java.io.File;
10 
11 public class CreateQrcode {
12 
13 
14     /**
15      * 生成二维码方法
16      *
17      * @param url  二维码表示数据
18      * @param resp response对象
19      * @throws Exception 抛出异常
20      */
21     public void getQrcode(String url, HttpServletResponse resp) throws Exception {
22         ServletOutputStream stream = null;
23         try {
24 
25             String format = "jpg";// 二维码的图片格式
26             File outputFile = new File("d:" + File.separator + "new.jpg");
27             stream = resp.getOutputStream();
28             QRCodeWriter qrCodeWriter = new QRCodeWriter();
29             BitMatrix bm = qrCodeWriter.encode(url, BarcodeFormat.QR_CODE, 300, 300);
30             MatrixToImageWriter.writeToStream(bm, "png", stream);
31             MatrixToImageWriter.writeToFile(bm, format, outputFile);
32         } catch (WriterException e) {
33             e.getStackTrace();
34         } finally {
35             if (stream != null) {
36                 stream.flush();
37                 stream.close();
38             }
39         }
40     }
41 }

 

转载于:https://www.cnblogs.com/PeterXu1997/p/9614858.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值