利用QRCode实现待logo的二维码的创建

 1 import java.awt.Color;
 2 import java.awt.Graphics2D;
 3 import java.awt.Image;
 4 import java.awt.image.BufferedImage;
 5 import java.io.File;
 6 
 7 import javax.imageio.ImageIO;
 8 
 9 import com.swetake.util.Qrcode;
10 
11 
12 
13 /**
14  * @author GH
15  *
16  */
17 public class BarcodeFactory {
18     
19     
20     public static int createQRCode(String content, String imgPath,String ccbPath,int version) {    
21         try {    
22             Qrcode qrcodeHandler = new Qrcode();    
23             //设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小    
24             qrcodeHandler.setQrcodeErrorCorrect('M');    
25             //N代表数字,A代表字符a-Z,B代表其他字符  
26             qrcodeHandler.setQrcodeEncodeMode('B');   
27             // 设置设置二维码版本,取值范围1-40,值越大尺寸越大,可存储的信息越大    
28             qrcodeHandler.setQrcodeVersion(version);   
29             // 图片尺寸    
30             int imgSize =67 + 12 * (version - 1) ;  
31     
32             byte[] contentBytes = content.getBytes("gb2312");    
33             BufferedImage image = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_RGB);    
34             Graphics2D gs = image.createGraphics();    
35     
36             gs.setBackground(Color.WHITE);    
37             gs.clearRect(0, 0, imgSize, imgSize);    
38     
39             // 设定图像颜色 > BLACK    
40             gs.setColor(Color.BLUE);    
41     
42             // 设置偏移量 不设置可能导致解析出错    
43             int pixoff = 2;    
44             // 输出内容 > 二维码    
45             if (contentBytes.length > 0 && contentBytes.length < 130) {  
46                 boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);  
47                 for (int i = 0; i < codeOut.length; i++) {  
48                     for (int j = 0; j < codeOut.length; j++) {  
49                         if (codeOut[j][i]) {  
50                             gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);  
51                         }  
52                     }  
53                 }  
54             } else {    
55                 System.err.println("QRCode content bytes length = "    
56                         + contentBytes.length + " not in [ 0,125]. ");    
57                 return -1;  
58             }    
59             Image logo = ImageIO.read(new File(ccbPath));//实例化一个Image对象。  
60             int widthLogo = logo.getWidth(null)>image.getWidth()*2/10?(image.getWidth()*2/10):logo.getWidth(null),   
61                 heightLogo = logo.getHeight(null)>image.getHeight()*2/10?(image.getHeight()*2/10):logo.getWidth(null);  
62              
63              /** 
64                * logo放在中心 
65               */  
66             int x = (image.getWidth() - widthLogo) / 2;  
67             int y = (image.getHeight() - heightLogo) / 2;  
68             gs.drawImage(logo, x, y, widthLogo, heightLogo, null);  
69             gs.dispose();    
70             image.flush();    
71     
72             // 生成二维码QRCode图片    
73             File imgFile = new File(imgPath);    
74             ImageIO.write(image, "png", imgFile);    
75     
76         } catch (Exception e)   
77         {    
78             e.printStackTrace();    
79             return -100;  
80         }    
81           
82         return 0;  
83     }    
84     public static void main(String[] args) {
85              String imgPath = "D:/二维码生成/logo_QRCode.png";   
86             String logoPath = "D:/logo/logo1.jpg";
87             String encoderContent = "https://blog.csdn.net/gao36951/article/details/41149049";
88             BarcodeFactory.createQRCode(encoderContent, imgPath, logoPath,8); 
89     }
90 }

 

posted @ 2018-03-26 11:19  曾将  阅读(480)  评论(0编辑  收藏  举报
//目录 欢迎评论,一起学习,对大家有用请点个赞。