zoukankan      html  css  js  c++  java
  • java springMVC生成二维码

    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),可以直接用。

    偶遇晨光原创

    2016-02-25

    爱生活,更爱给我带来生活的人
  • 相关阅读:
    HDU 6197 array array array nlogn求最长子序列 思维
    HDU 6198 number number number 矩阵快速幂 找规律
    HDU 6195 cable cable cable 思维
    urumuqi 网络赛 H skiing DP
    urumqi 网络赛 E Half-consecutive Numbers 数论
    urumqi 网络赛 C Coconut 水题
    UVA 122 Trees on the level 二叉树 广搜
    POJ3468【线段树lazy操作】
    线段树萌新讲解+一道水题【一点一滴】
    hdoj5792 【树状数组】【未完待续】
  • 原文地址:https://www.cnblogs.com/chenyq/p/5216363.html
Copyright © 2011-2022 走看看