springMVC生成二维码

12 篇文章 0 订阅

Zxing是Google提供的工具,提供了二维码的生成与解析的方法,现在使用Java利用Zxing生成二维码

1),二维码的生成

将Zxing-core.jar 包加入到classpath下。

我的下载地址:http://i.cnblogs.com/Files.aspx 下zxing.zip包

1.RqCodeController 类

复制代码
 1     private static final Log logger = LogFactory.getLog(RqCodeController.class);
 2 
 3     @RequestMapping("/gen.json")
 4     public void gen(String url, HttpServletResponse response, Integer width, Integer height ) {
 5         
 6         try {
 7 
 8             int iWidth = (width == null?200: width);
 9             int iHeight = (height==null?200: height);
10             
11             MatrixToImageWriter.createRqCode(url, iWidth, iHeight
12                     , response.getOutputStream());
13         
14         } catch (Exception e) {
15 
16             logger.error(String.format("生成二维码失败: url: %s", url), e);
17             
18         }
19         
20         
21     }
复制代码

 

2,MatrixToImageWriter类的方法

复制代码
 1 package com.web.util;
 2 
 3 import java.awt.Graphics2D;
 4 import java.awt.Image;
 5 import java.awt.image.BufferedImage;
 6 import java.io.File;
 7 import java.io.IOException;
 8 import java.io.OutputStream;
 9 import java.util.Hashtable;
10 
11 import javax.imageio.ImageIO;
12 
13 import org.springframework.core.io.ClassPathResource;
14 
15 import com.google.zxing.BarcodeFormat;
16 import com.google.zxing.EncodeHintType;
17 import com.google.zxing.MultiFormatWriter;
18 import com.google.zxing.WriterException;
19 import com.google.zxing.common.BitMatrix;
20 
21 /**
22  * 二维码生成工具
23  */
24 public class MatrixToImageWriter {
25 
26     private static final int BLACK = 0xFF000000;
27     private static final int WHITE = 0xFFFFFFFF;
28     private static final int MARGIN = 1; //边框
29     
30     private static final String FORMAT = "png";
31 
32     private MatrixToImageWriter() {
33     }
34 
35     public static void createRqCode(String textOrUrl, int width, int height, OutputStream toStream)
36             throws WriterException, IOException {
37 
38         Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
39         hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 内容所使用字符集编码
40         hints.put(EncodeHintType.MARGIN, new Integer(MARGIN));
41 
42         BitMatrix bitMatrix = new MultiFormatWriter().encode(textOrUrl, BarcodeFormat.QR_CODE, width, height, hints);
43 
44         BufferedImage image = toBufferedImage(bitMatrix);
45         applyLogo(image);//应用LOGO
46 
47         writeToStream(image, FORMAT, toStream);
48 
49     }
50 
51     private static void applyLogo(BufferedImage image) throws IOException {
52 
53         Graphics2D gs = image.createGraphics();
54 
55         ClassPathResource resource = new ClassPathResource("logo.png");//logo图片
56         
57         // 载入logo
58         Image img = ImageIO.read(resource.getFile());
59 
60         int left = image.getWidth() / 2 - img.getWidth(null) / 2;
61         int top = image.getHeight() / 2 - img.getHeight(null) / 2;
62 
63         gs.drawImage(img, left, top, null);
64         gs.dispose();
65         img.flush();
66 
67     }
68 
69     private static BufferedImage toBufferedImage(BitMatrix matrix) {
70         int width = matrix.getWidth();
71         int height = matrix.getHeight();
72         BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
73         for (int x = 0; x < width; x++) {
74             for (int y = 0; y < height; y++) {
75                 image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
76             }
77         }
78         return image;
79     }
80 
81     public static void writeToFile(BufferedImage image, String format, File file) throws IOException {
82         
83         if (!ImageIO.write(image, format, file)) {
84             throw new IOException("Could not write an image of format " + format + " to " + file);
85         }
86     }
87 
88     public static void writeToStream(BufferedImage image, String format, OutputStream stream) throws IOException {
89         if (!ImageIO.write(image, format, stream)) {
90             throw new IOException("Could not write an image of format " + format);
91         }
92     }
93 
94 }
复制代码

上述编写的代码,就可传出一个二进制数,然后前端使用图片的格式将二进制数展现出来,就是一个二维码。

 

下面是页面生成,可以是链接,可以是文本

复制代码
 1 /*获取页面二维码*/
 2 function share2dImg(link){
 3     $("#shareImg").attr("src","/rqcode/gen.json?url="+link+"&width=200&height=200")     
 4 }
 5 
 6 $(function(){
 7 
 8     /*微信分享的执行*/
 9     var invitationCode = ajaxGetInfo();//不管登录与否,都传空,获取邀请码
10     var shareTitle = "送有8888元!";//分享的标题
11     var shareDesc = "送有8888元!";//分享的描述
12     var shareLink = "https://www.baidu.com/index.php?tn=monline_3_dg";//分享的链接
13     weixinShare(shareTitle,shareDesc,shareLink);
14 
15     //点击立即邀请,弹出界面框
16     $("#toInvite").click(function(){
17         $(".share-dialog").show();
18     });
19 
20     //点击弹出界面框,回到基本页面
21     $(".share-dialog").click(function(){
22         $(this).hide();
23     });
24 
25     share2dImg(encodeURIComponent(shareLink))//获取分享出去的二维码    
26 
27 });
复制代码

 

对于二维码的解析,需要zxing一个辅助类( BufferedImageLuminanceSource),可以直接用。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值