在java中生成二维码,并直接输出到jsp页面

     在java中生成的二维码不存到磁盘里要直接输出到页面上,这就需要把生成的二维码直接以流的形式输出到页面上,我用的是myeclipse 和 tomcat

     它的原理是:在加载页面时,根据img的src(code.jspx)找到对应的servlet(CodeServlet),在servlet中编辑二维码中的内容,调用生成二维码的类EncoderHandler中的encoderQRcoder方法

   1.首先要导入jar包(将jar包放到lib目录下)

   导入jar包的名称为:Qrcode_swetake

   下载jar包的连接http://download.csdn.net/detail/huakaihualuo1223/4527504

    2.在java中生成二维码并以流的形式输出

package servlet;
import java.awt.Color;  
import java.awt.Graphics2D;  
import java.awt.image.BufferedImage;  
  
import javax.imageio.ImageIO;  
import javax.servlet.http.HttpServletResponse;  
  
import com.swetake.util.Qrcode;  
  
  
public class EncodeHandler {  
      
    public void encoderQRCoder(String content, HttpServletResponse response) {  
        try {  
            Qrcode handler = new Qrcode();  
            handler.setQrcodeErrorCorrect('M');  
            handler.setQrcodeEncodeMode('B');  
            handler.setQrcodeVersion(7);  
              
            System.out.println(content);  
            byte[] contentBytes = content.getBytes("UTF-8");  
              
            BufferedImage bufImg = new BufferedImage(141,141, BufferedImage.TYPE_INT_RGB);  //控制二维码图像的显示区域的大小,大小相反
              
            Graphics2D gs = bufImg.createGraphics();  
              
            gs.setBackground(Color.WHITE);  
            gs.clearRect(0, 0, 200, 200);  
              
            //设定图像颜色:BLACK   
            gs.setColor(Color.BLACK);  
              
            //设置偏移量  不设置肯能导致解析出错   
            int pixoff = 2;  
            //输出内容:二维码   
            if(contentBytes.length > 0 && contentBytes.length < 124) {  
                boolean[][] codeOut = handler.calQrcode(contentBytes);  
                for(int i = 0; i < codeOut.length; i++) {  
                    for(int j = 0; j < codeOut.length; j++) {  
                        if(codeOut[j][i]) {  
                            gs.fillRect(j * 3 + pixoff, i * 3 + pixoff,3, 3);  
                        }  
                    }  
                }  
            } else {  
                System.err.println("QRCode content bytes length = " + contentBytes.length + " not in [ 0,120 ]. ");  
            }  
              
            gs.dispose();  
            bufImg.flush();  
              
              
              
            //生成二维码QRCode图片   
            ImageIO.write(bufImg, "jpg", response.getOutputStream());  
              
              
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
}  

3.建立一个servlet


package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CodeServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		
		doPost(request,response);
		 
	}

	 
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
 
		
		String content = "姓名:张二强 QQ:906928204";  
        EncodeHandler encoder= new EncodeHandler() ;  
        encoder.encoderQRCoder(content, response);  

	}

}


4在web.xml中的配置



<servlet>
   
    <servlet-name>CodeServlet</servlet-name>
    <servlet-class>servlet.CodeServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>CodeServlet</servlet-name>
    <url-pattern>/servlet/CodeServlet</url-pattern>
  </servlet-mapping>


5.jsp中的img(其中img的src为对应的CodeServlet的url)


二维码图标生成: <img  style="height:50px;width:50px" src="servlet/CodeServlet" />

截图如下:





原文出自:http://blog.csdn.net/huakaihualuo1223/article/details/7910232

(原文中有些错误,主要是生成的二维码不能解析,这是二维码显示的尺寸太小造成的,在这里已经改过来了)

请多注意24,29行的地方,尺寸要协调好~~~~





还有一种是在二维码中添加logo图片,这以后再为大家讲述喽~~~





你可能感兴趣的:(jsp,二维码)