java生成二维码技术

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java生成二维码技术相关的知识,希望对你有一定的参考价值。

代码来源潭州免费视频教程

需要Qrcode_swetake.jar类库,jquery.js

后台新建EcardServlet.java,修改web.xml的servlet映射

public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //处理字符编码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String content =request.getParameter("content");
        //生成二维码
        Qrcode qrcode = new Qrcode();
        qrcode.setQrcodeErrorCorrect(‘M‘);//容错率L 7%,M 15%,Q 25%,H 30%
        qrcode.setQrcodeEncodeMode(‘B‘);//编码模式Binary
        qrcode.setQrcodeVersion(15);//1-40
        byte[] contentBytes = content.getBytes();
        int width = 235,height=235;
        BufferedImage image=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        
        //画笔
        Graphics2D gsGraphics2d = image.createGraphics();
        gsGraphics2d.setBackground(Color.WHITE);
        gsGraphics2d.setColor(Color.BLACK);
        gsGraphics2d.clearRect(0, 0, width, height);//绘制空白矩形
        
        //处理二维码
        int pixoff=2;//设置偏移量
        boolean[][] code=qrcode.calQrcode(contentBytes);
        for (int i = 0; i < code.length; i++) {
            for (int j = 0; j < code.length; j++) {
                if (code[i][j]) {
                    gsGraphics2d.fillRect(i*3+pixoff, j*3+pixoff, 3, 3);//画二维码的小方块3*3
                }
            }
        }
        gsGraphics2d.dispose();//释放画笔资源
        image.flush();//清空缓冲区,不然造成资源浪费
        
        String fileName =UUID.randomUUID()+".jpg";
        File file = new File(request.getServletContext().getRealPath("/images")+"/"+fileName);
        ImageIO.write(image, "JPEG", file);
        PrintWriter outPrintWriter = response.getWriter();
        outPrintWriter.println(fileName);
        outPrintWriter.flush();
        outPrintWriter.close();
        
    }

前台修改index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>java 二维码扫一扫</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
    <style type="text/css">
        *{margin:0;padding:0;}
        .input_btn{width:200px;height:40px;border-radius:5px;background:#d206c8;color:red;font-weight:bold;transition:all 0.5s ease;}
        .input_btn:hover{background:#e07185;color:fff;transition:all 0.5s ;}
        .ecard{float:left;}
    </style>
  </head>
  
  <body>
    <div class="ecarBox">
        <h1>java 二维码扫一扫</h1>
        <br />
        <p>
            <span>信息:</span>
            <input type ="text" class="input_box" id="web"  placeholder="请输入有效信息..." />
            <br />
            <input type="button" value="生成二维码" class="input_btn" />
            
        </p>
    </div>
    
    <div class="ecardOuterBox"></div>
    
    <script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>
    <script type="text/javascript" >
        $(function(){
            $(".input_btn").click(getEcard);
        });
        function getEcard(){//获取二维码的方法
            var web=$("#web").val();
            $.ajax({
                type:"post",
                url:"EcardServlet",
                data:{"content":web},
                success:function(data){
                    $(".ecardOuterBox").empty();
                    $(".ecardOuterBox").append("<div class=‘ecard‘><img src=‘images/"+ data +"‘/></div>");
                }
            });
        }
    </script>
  </body>
</html>

 

 预览图片如下:

 

技术分享

 

以上是关于java生成二维码技术的主要内容,如果未能解决你的问题,请参考以下文章

java二维码扫码登录运用了啥技术

java使用qcord生成的二维码,手机可以扫,扫描枪扫不出来,或者很难扫出来?

java二维码生成技术

pbootcms对接微信扫码登录代码核心片段和步骤(前后端)

利用java代码生成二维码

java 生成二维码后如何给该二维码添加信息