java生成二维码

1.所需jar包

2.代码部分

 1 public class CreateQRCode {
 2 
 3     public static void main(String[] args) throws IOException {
 4         // TODO Auto-generated method stub
 5 
 6         Qrcode x = new Qrcode();
 7         x.setQrcodeErrorCorrect('M');//纠错等级
 8         x.setQrcodeEncodeMode('B');//N代表数字,A代表a-Z, B代表其他字符
 9         x.setQrcodeVersion(7);//版本
10         
11         String qrData = "http://www.ggyyv.com/";
12         int width = 67 + 12 * (x.getQrcodeVersion() - 1);
13         int height = 67 + 12 * (x.getQrcodeVersion() - 1);
14         
15         BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
16         Graphics2D gs = bufferedImage.createGraphics();
17         
18         gs.setBackground(Color.WHITE);
19         gs.setColor(Color.BLACK);
20         gs.clearRect(0, 0, width, height);
21         
22         int pixoff = 2;//偏移量
23         
24         byte[] b = qrData.getBytes("gb2312");
25         if(b.length > 0 && b.length < 120){
26             boolean[][] s = x.calQrcode(b);
27             
28             for(int i = 0; i < s.length;++i){
29                 for(int j = 0; j < s.length;++j){
30                     if(s[j][i]){
31                         gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
32                     }
33                 }
34             }
35         }
36         gs.dispose();
37         bufferedImage.flush();
38         
39         ImageIO.write(bufferedImage, "png", new File("qrcode.png"));
40     }
41 
42 }

 

posted on 2017-09-03 13:11  老邱2  阅读(135)  评论(0)    收藏  举报

导航