首页 > 代码库 > servlet中用ZXing生成条码二维码并输出到web页面的示例

servlet中用ZXing生成条码二维码并输出到web页面的示例

1、生成条码的servlet代码:

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.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.oned.Code128Writer;

/** 
 * @Description: 生成条码(CODE128格式)  */
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 = "JPEG";

	@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 
					}
				}
				stream = resp.getOutputStream();
				BitMatrix m = writer.encode(keycode, BarcodeFormat.CODE_128, width, height);
				MatrixToImageWriter.writeToStream(m, 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);
	}
	
	

}

2、生成qrcode二维码的servlet示例

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.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

/** 
 * @Description: 生成二维码 (QR格式)
 * @author lwei
 */
public class BarCode2DServlet extends HttpServlet {

	/**   
	 * @Fields serialVersionUID : serialVersionUID 
	 */ 
	
	private static final long serialVersionUID = 1L;
	
	private static final String KEY = "keycode";
	private static final String SIZE = "msize";
	private static final String IMAGETYPE = "JPEG";

	@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 {
				int size=129;
				String msize = req.getParameter(SIZE);
				if (msize != null && !"".equals(msize.trim())) {
					try{
						size=Integer.valueOf(msize);
					} catch (NumberFormatException e) {
						//TODO output to log
					}
				}
				stream = resp.getOutputStream();
				QRCodeWriter writer = new QRCodeWriter();
				BitMatrix m = writer.encode(keycode, BarcodeFormat.QR_CODE, size, size);
				MatrixToImageWriter.writeToStream(m, 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);
	}
	
}

3、web.xml中的配置

        <servlet>
  		<servlet-name>barCode</servlet-name>
  		<servlet-class>com.XXX.XXX.common.servlet.BarCode1DServlet</servlet-class>
  	</servlet>
  	<servlet-mapping>
  		<servlet-name>barCode</servlet-name>
  		<url-pattern>/barCode</url-pattern>
  	</servlet-mapping>
  	<servlet>
  		<servlet-name>qrCode</servlet-name>
  		<servlet-class>com.XXX.XXX.common.servlet.BarCode2DServlet</servlet-class>
  	</servlet>
  	<servlet-mapping>
  		<servlet-name>qrCode</servlet-name>
  		<url-pattern>/qrCode</url-pattern>
  	</servlet-mapping>

4、页面引用


<img alt="条码" src="http://www.mamicode.com/${pageContext.request.contextPath}/barCode?keycode=RT100200300400"></img>
    <img alt="二维码" src="http://www.mamicode.com/${pageContext.request.contextPath}/qrCode?keycode=RT100200300400500"></img>

够详细吧....

servlet中用ZXing生成条码二维码并输出到web页面的示例