您的位置:首页 > 编程语言 > Java开发

javaweb使用Zxing生成二维码

2015-08-06 10:48 681 查看
下载包 zxing2.2jar

javaweb程序 创建一个 servlet

import java.io.FileOutputStream;
import java.io.IOException;
 
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.oned.Code128Writer;
 
/** 
 * @Description: 生成条码(CODE128格式) <span></span> */
public class BarCode1DServlet extends HttpServlet {
 
    /**   
     * @Fields serialVersionUID : default serialVersionUID 
     */
    private static final long serialVersionUID = 1L;
     
    private static final String KEY = "keycode";
    private static final String WIDTH = "mwidth";
    private static final String HEIGHT = "mheight";
    private static final String IMAGETYPE = "png";
 
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String keycode = req.getParameter(KEY);
        if (keycode != null && !"".equals(keycode)) {
            ServletOutputStream stream = null;
            try {
                Code128Writer writer = new Code128Writer();
                int width=180;
                int height=60;
                String mwidth = req.getParameter(WIDTH);
                if (mwidth != null && !"".equals(mwidth.trim())) {
                    try{
                        width=Integer.valueOf(mwidth);
                    } catch (NumberFormatException e) {
                                        //TODO output to log 
                    }
                } 
                String mheight = req.getParameter(HEIGHT);
                if (mheight != null && !"".equals(mheight.trim())) {
                    try{
                        height = Integer.valueOf(mheight);
                    } catch (NumberFormatException e) {
                        //TODO output to log 
                    }
                }
                int codeWidth = 3 + // start guard
                        (7 * 6) + // left bars
                        5 + // middle guard
                        (7 * 6) + // right bars
                        3; // end guard
                codeWidth = Math.max(codeWidth, width);
                stream = resp.getOutputStream();
                BitMatrix bitMatrix = new MultiFormatWriter().encode(keycode,
                        BarcodeFormat.EAN_13, codeWidth, height, null);
     
                MatrixToImageWriter.writeToStream(bitMatrix, IMAGETYPE,
                        stream);
                
                
            } catch (WriterException e) {
                e.printStackTrace();
            } finally {
                if (stream != null) {
                    stream.flush();
                    stream.close();
                }
            }
        }
    }
 
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        this.doGet(req, resp);
    }
     
     
 
}


jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</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">
	-->
  </head>
  
  <body>
    This is my JSP page. <br>
    <img alt="条码" src="${pageContext.request.contextPath}/BarCode1DServlet?keycode=6923450657713">6923450657713</img>
  </body>
</html>


源代码 下载 地址 http://download.csdn.net/detail/hongwangzhang/8966489
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: