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

    参考:https://www.cnblogs.com/bigroc/p/7496995.html

    导入jar包:

    链接:https://pan.baidu.com/s/1r91QawdMbMCOZ1YIQtWd3Q
    提取码:l2id
    复制这段内容后打开百度网盘手机App,操作更方便哦


    CreateQRCodeServlet.java
     1 package cn.xxx.web.servlet;
     2 
     3 import com.swetake.util.Qrcode;
     4 
     5 import javax.imageio.ImageIO;
     6 import javax.servlet.ServletException;
     7 import javax.servlet.annotation.WebServlet;
     8 import javax.servlet.http.HttpServlet;
     9 import javax.servlet.http.HttpServletRequest;
    10 import javax.servlet.http.HttpServletResponse;
    11 import java.awt.Color;
    12 import java.awt.Graphics2D;
    13 import java.awt.image.BufferedImage;
    14 import java.io.File;
    15 import java.io.IOException;
    16 
    17 @WebServlet("/createQRCodeServlet")
    18 public class CreateQRCodeServlet extends HttpServlet {
    19     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    20 
    21         //服务器通知浏览器不要缓存
    22         response.setHeader("pragma","no-cache");
    23         response.setHeader("cache-control","no-cache");
    24         response.setHeader("expires","0");
    25 
    26         // 获取会议号
    27         String mid = request.getParameter("mid");
    28 
    29         //计算二维码图片的高宽比
    30         // API文档规定计算图片宽高的方式 ,v是本次测试的版本号
    31         int v = 6;
    32         int width = 67 + 12 * (v - 1);
    33         int height = 67 + 12 * (v - 1);
    34 
    35 
    36         Qrcode x = new Qrcode();
    37         /**
    38          * 纠错等级分为
    39          * level L : 最大 7% 的错误能够被纠正;
    40          * level M : 最大 15% 的错误能够被纠正;
    41          * level Q : 最大 25% 的错误能够被纠正;
    42          * level H : 最大 30% 的错误能够被纠正;
    43          */
    44         x.setQrcodeErrorCorrect('L');
    45         x.setQrcodeEncodeMode('B');//注意版本信息 N代表数字 、A代表 a-z,A-Z、B代表 其他)
    46         x.setQrcodeVersion(v);//版本号  1-40
    47         String qrData = mid;//内容信息(此处保存的是会议号)
    48 
    49         byte[] d = qrData.getBytes("utf-8");//汉字转格式需要抛出异常
    50 
    51         //缓冲区
    52         BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
    53 
    54         //绘图
    55         Graphics2D gs = bufferedImage.createGraphics();
    56 
    57         gs.setBackground(Color.WHITE);
    58         gs.setColor(Color.BLACK);
    59         gs.clearRect(0, 0, width, height);
    60 
    61         //偏移量
    62         int pixoff = 2;
    63 
    64 
    65         /**
    66          * 1.注意for循环里面的i,j的顺序,
    67          *   s[j][i]二维数组的j,i的顺序要与这个方法中的 gs.fillRect(j*3+pixoff,i*3+pixoff, 3, 3);
    68          *   顺序匹配,否则会出现解析图片是一串数字
    69          * 2.注意此判断if (d.length > 0 && d.length < 120)
    70          *   是否会引起字符串长度大于120导致生成代码不执行,二维码空白
    71          *   根据自己的字符串大小来设置此配置
    72          */
    73         if (d.length > 0 && d.length < 120) {
    74             boolean[][] s = x.calQrcode(d);
    75 
    76             for (int i = 0; i < s.length; i++) {
    77                 for (int j = 0; j < s.length; j++) {
    78                     if (s[j][i]) {
    79                         gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
    80                     }
    81                 }
    82             }
    83         }
    84         gs.dispose();
    85         bufferedImage.flush();
    86         //设置图片格式,与输出的路径,输出到响应当中
    87         ImageIO.write(bufferedImage, "png", response.getOutputStream());
    88     }
    89 
    90 
    91     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    92         this.doPost(request, response);
    93     }
    94 }

    qrcode.jsp

     1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
     2 <html>
     3     <head>
     4         <title>我的会议号</title>
     5         <style>
     6             div{
     7                 text-align: center;
     8                 margin-top: 220px;
     9             }
    10         </style>
    11     </head>
    12     <body>
    13         <div>
    14            <h2>请扫描此二维码查看您的会议号:</h2>
    15             <br/>
    16             <img src="${pageContext.request.contextPath}/createQRCodeServlet?mid=${mid}"/>
    17         </div>
    18     </body>
    19 </html>

    网页上的效果:

  • 相关阅读:
    Maven 配置篇之 pom.xml
    Maven 配置篇之 pom.xml
    Building Seam 2.0 Application with NetBeans 6.1(Maven2)
    TestNG 使 Java 单元测试轻而易举
    文本加密和解密
    DLL/OCX中的MFC对话框不能处理Tab和回车键的问题 .
    如何在编辑框中使用IAutoComplete接口
    演练:创建和使用静态库 (C++)
    MFC Activex OCX Javascript 互相访问问题,线程回调javascript
    VC++学习方法及书籍推荐 .
  • 原文地址:https://www.cnblogs.com/FengZeng666/p/11732200.html
Copyright © 2011-2022 走看看