javaweb使用Zxing生成二维码
2015-08-06 10:48
681 查看
下载包 zxing2.2jar
javaweb程序 创建一个 servlet
jsp页面
源代码 下载 地址 http://download.csdn.net/detail/hongwangzhang/8966489
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
相关文章推荐
- Eclipse中项目编码格式转换(转码)
- java缓存系统
- java缓存系统
- JAVA 异步机制与同步机制的区别
- Java UDP通信
- java反射获得运行时属性的值
- Java并发编程笔记
- MyEclipse 快捷键
- 深入java单例模式
- java图片截取上传
- 解析Java中的队列和用LinkedList集合模拟队列的方法
- java中不同类型的数据放到一个数组中
- java的几个版本以及jre,jdk等概念——【转载】JDK、Java SE、Java EE、Java ME我该选
- Java——class
- JAVA多线程学习笔记<3> 停止线程
- Java transient关键字使用小记
- Myeclipse10中出现Cannot return from outside a function or method错误提示
- 几周内搞定Java的10个方法
- 使用Spring Boot快速构建应用
- Hdu 5351 MZL's Border 2015ACM多校对抗赛第五场