java生成二维码

具体代码如下,作为一个新手,期待与你一起交流:

 1 import java.awt.Color;  
 2 import java.awt.Graphics2D;  
 3 import java.awt.image.BufferedImage;  
 4 import java.io.File;  
 5   
 6 import javax.imageio.ImageIO;  
 7   
 8 import com.swetake.util.Qrcode;
 9 public class QRCodeEncoderHandler {
10    /**
11     * 生成二维码(QRCode)图片
12     * @param content
13     * @param imgPath
14     */
15     public void encoderQRCode(String content,String imgPath){
16         try{
17             Qrcode qrcodeHandler = new Qrcode();  
18             qrcodeHandler.setQrcodeErrorCorrect('M');  
19             qrcodeHandler.setQrcodeEncodeMode('B');  
20             qrcodeHandler.setQrcodeVersion(7);  
21   
22             System.out.println(content);  
23             byte[] contentBytes = content.getBytes("utf-8");  
24   
25             BufferedImage bufImg = new BufferedImage(140, 140,  
26                     BufferedImage.TYPE_INT_RGB);  
27   
28             Graphics2D gs = bufImg.createGraphics();  
29   
30             gs.setBackground(Color.WHITE);  
31             gs.clearRect(0, 0,30000,30000);  
32   
33             // 设定图像颜色 > BLACK  
34             gs.setColor(new Color(0,0,0,255));  
35   
36             // 设置偏移量 不设置可能导致解析出错  
37             int pixoff = 2;  
38             // 输出内容 > 二维码  
39             if (contentBytes.length > 0 && contentBytes.length < 120) {  
40                 boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);  
41                 for (int i = 0; i < codeOut.length; i++) {  
42                     for (int j = 0; j < codeOut.length; j++) {  
43                         if (codeOut[j][i]) {  
44                             gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);  
45                         }  
46                     }  
47                 }  
48             } else {  
49                 System.err.println("QRCode content bytes length = "  
50                         + contentBytes.length + " not in [ 0,120 ]. ");  
51             }  
52   
53             gs.dispose();  
54             bufImg.flush();  
55   
56             File imgFile = new File(imgPath);  
57   
58             // 生成二维码QRCode图片  
59             ImageIO.write(bufImg, "png", imgFile);   
60         }catch (Exception e) {
61             // TODO: handle exception
62             e.printStackTrace(); 
63         }
64     }
65     public static void main(String[] args) {
66         String imgPath = "D://Michael.jpg";  
67           
68         String content = "姓名:**"  
69                 + "\n\r电话:135********"  
70                 + "\n\rEmail:******4@qq.com" + "\n\rEmail2:******@163.com" +"\n\rQQ :******";  
71   
72         QRCodeEncoderHandler handler = new QRCodeEncoderHandler();  
73         handler.encoderQRCode(content, imgPath);  
74   
75         System.out.println("encoder QRcode success");  
76     }
77 }

 

转载于:https://www.cnblogs.com/caoleiCoding/p/6372031.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值