首页 > 代码库 > javaweb二维码

javaweb二维码

QRCode.java程序

package my;import java.awt.Color;import java.awt.Graphics2D;import java.awt.image.BufferedImage;import java.io.IOException;import java.io.UnsupportedEncodingException;import javax.imageio.ImageIO;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.swetake.util.Qrcode;public class QRCode extends HttpServlet{    //重写HttpServlet里面的service方法(请求对象,相应对象)    public void service(HttpServletRequest request, HttpServletResponse response){        try {            request.setCharacterEncoding("utf-8");//设置接收字符的编码            String content=request.getParameter("content");//得到文本框中的类容            Qrcode qrcode=new Qrcode();//创建Qrcode对象            qrcode.setQrcodeErrorCorrect(‘M‘);//设置排错率 L%7 M%15 Q%25 H%30            qrcode.setQrcodeEncodeMode(‘B‘);//设置编码 B为二维码            qrcode.setQrcodeVersion(7);//设置版本号1-40            //先以ISO-8859-1解码(默认编码),转化成UTF-8编码字符编码的转换            byte[] bt = new String(content.getBytes("ISO-8859-1"),"UTF-8").getBytes();            //创建图像数据的缓冲区            //BufferedImage.TYPE_INT_BGR 创建一个具有8位RGB颜色的分量图片            BufferedImage image = new BufferedImage(140,140,BufferedImage.TYPE_INT_BGR);            //创建画笔工具            Graphics2D g2D = image.createGraphics();            //设置颜色            g2D.setBackground(Color.WHITE);            //绘制矩形            g2D.clearRect(0, 0, 140, 140);            //绘制前景色            g2D.setColor(Color.BLACK);                        if(bt.length > 0){                //返回二维码的数据                boolean[][] s = qrcode.calQrcode(bt);                for(int i = 0; i < s.length; i++){                    for(int j = 0; j < s.length; j++){                        if(s[j][i]){                            //布尔数组填充图片                            g2D.fillRect(j * 3 + 2, i * 3 + 2, 3, 3);                                                    }                    }                }            }            g2D.dispose();//释放画笔            image.flush();//残留数据推出缓冲区            //以流的方式将image二维码输出            ImageIO.write(image, "jpg", response.getOutputStream());        } catch (UnsupportedEncodingException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}

index.jsp

<%@ page language="java"  pageEncoding="utf-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <script type="text/javascript">        function qrcode(){            var value=document.getElementById(‘content‘).value;            var image=document.getElementById(‘image‘);            image.src=‘qr.do?content=‘+value;            }    </script>  </head>    <body>       <input type="text" id="content" width=100px height= 100px/>       <input type="button" value="http://www.mamicode.com/生成二维码" onclick="qrcode()"/>       <img id="image" />  </body></html>

web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5"     xmlns="http://java.sun.com/xml/ns/javaee"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">       <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>    <!-- 定义一个servlet -->  <servlet>      <servlet-name>qrcode</servlet-name>      <servlet-class>my.QRCode</servlet-class>  </servlet>    <servlet-mapping>      <servlet-name>qrcode</servlet-name>      <url-pattern>/qr.do</url-pattern>  </servlet-mapping></web-app>

 

javaweb二维码