|
JSP生成条形码
实例说明
条形码是用于区分产品的一组编号,但是以条码形式呈现给用户,例如图书上的ISBN编码、超市商品的条形码、各种电子产品和保修凭证上的条形码等等。本实例在JSP页面实现了条形码的在线生成功能用户在如图1所示的页面中可以输入要生成条形码的产品编号,也可以包含英文字母,单击“生成条形码”按钮,在图2所示的页面中会生成各种编码格式的条形码图片。
图一 输入产品编号
图二 打印出的条形码
技术要点
本实例使用了开源的JBarcodeBean组件。该主页的下载网址为http://jbarcodebean.sourceforge.net/。它是一个JFC Swing兼容的JavaBean组件,主要用来产生条形码。JBarcodeBean支持当前一些流行的条形码格式,例如Code 128,Code 39,ExtendedCode 39,Codabar Interleaved Code 25 ,MSI ,EAN-13,EAN-8等。
本实例使用的JBarcodeBean组件版本为1.1.5。下面分别介绍实例中应用到该组件的关键技术。
(1)设置条形码显示文本
条形码都是根据产品编号生成的,那么这个产品编号就是条形码的文本,通过JBarcodeBean类的setShowText()方法可以设置生成的条形码图片是否包含这个文本信息。其语法格式如下:
setShowText(Boolean show)
show:如果需要显示条形码文本,设置该参数为true,否则设置为。
(2)输出条形码
JBarcodeBean类的gifEncode()方法用于输出GIF格式的条形码图片,输出方式可以是任何的Java输出流。其语法格式如下:
gifEncode(OutputStream out)
out:任何类型的Java输出流对象。
本实例使用gifEncode()方法将条形码输出到应答对象的输出流,客户端会直接显示GIF格式的条形码图片。关键代码如下。 barcode.setCodeType(codeType); //设置指定编码类型
barcode.setCode(request.getParameter("code")); // 设置条形码数值
barcode.setBarcodeHeight(70); //设置条形码的高度
barcode.setCheckDigit(false);
barcode.setFont(barcode.getFont().deriveFont(14)); //设置文本字体
barcode.gifEncode(out); //输出条形码
开发步骤
(1)编写index.jsp首页面,在首页面中定义接收产品编号的文本框和执行条码生成的提交按钮。程序关键代码如下。<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP生成条形码</title>
</head>
<body>
<form method="get" action="result.jsp">
请输入产品编号:
<input name="code" type="text">
<input type="submit" value="生成条形码">
</form>
</body>
</html>
(2)编写result.jsp页面,这个页面将接收首页中用户输入的产品编号,然后把将产品编号传递给ServletTest类,并指定不同的编码格式,在页面中显示各种编码的条形码。关键代码如下。 <body>
<table border="1" width="300">
<tr align="center" bgcolor="#8AC52D">
<td>Code 11编码</td>
<td>Code 128编码</td>
<td>Code 39 3:1编码</td>
<td>Code 39_2:1编码</td>
</tr>
<tr align="center">
<td><img src="ServletTest?code=${param.code }&type=<%=CodeType.Code_11%>"></td>
<td><img src="ServletTest?code=${param.code }&type=<%=CodeType.Code_128%>"></td>
<td><img src="ServletTest?code=${param.code }&type=<%=CodeType.Code_39_3_1%>"></td>
<td><img src="ServletTest?code=${param.code }&type=<%=CodeType.Code_39_2_1%>"></td>
</tr>
<tr align="center" bgcolor="#8AC52D">
<td>Code 93编码</td>
<td>Code 93 Extended编码</td>
<td>Ext Code 39 3:1编码</td>
<td>Ext Code 39 2:1编码</td>
</tr>
<tr align="center">
<td><img src="ServletTest?code=${param.code }&type=<%=CodeType.Code_93%>"></td>
<td><img src="ServletTest?code=${param.code }&type=<%=CodeType.Code_93_Extended%>"></td>
<td><img src="ServletTest?code=${param.code }&type=<%=CodeType.Ext_Code_39_3_1%>"></td>
<td><img src="ServletTest?code=${param.code }&type=<%=CodeType.Ext_Code_39_2_1%>"></td>
</tr>
<tr align="center" bgcolor="#8AC52D">
<td>InterLeaved 25 3:1编码</td>
<td>InterLeaved 25 2:1编码</td>
<td>MSI model10 check编码</td>
<td>Codabar 3:1编码</td>
</tr>
<tr align="center">
<td><img src="ServletTest?code=${param.code }&type=<%=CodeType.InterLeaved_25_3_1%>"></td>
<td><img src="ServletTest?code=${param.code }&type=<%=CodeType.InterLeaved_25_2_1%>"></td>
<td><img src="ServletTest?code=${param.code }&type=<%=CodeType.MSI_model10_check%>"></td>
<td><img src="ServletTest?code=${param.code }&type=<%=CodeType.Codabar_3_1%>"></td>
</tr>
<tr align="center" bgcolor="#8AC52D">
<td>Codabar_2_1编码</td>
<td>EAN_13编码</td>
<td>EAN_8编码</td>
<td> </td>
</tr>
<tr align="center">
<td><img src="ServletTest?code=${param.code }&type=<%=CodeType.Codabar_2_1%>"></td>
<td><img src="ServletTest?code=${param.code }&type=<%=CodeType.EAN_13%>"></td>
<td><img src="ServletTest?code=${param.code }&type=<%=CodeType.EAN_8%>"></td>
<td> </td>
</tr>
</table>
</body>
(3)编写CodeType接口,在该接口中定义静态的常量,使用这些常量分别是代表不同的编码格式,关键代码如下。 <span style="font-family:Microsoft YaHei;"><span style="font-size: 14px;">public interface CodeType {
public static int Code_11=0x1;
public static int Code_128=0x2;
public static int Code_39_3_1=0x3;
public static int Code_39_2_1=0x4;
public static int Ext_Code_39_3_1=0x5;
public static int Ext_Code_39_2_1=0x6;
public static int Code_93=0x7;
public static int Code_93_Extended=0x8;
public static int InterLeaved_25_3_1=0x9;
public static int InterLeaved_25_2_1=0x10;
public static int MSI_model10_check=0x11;
public static int Codabar_3_1=0x12;
public static int Codabar_2_1=0x13;
public static int EAN_13=0x14;
public static int EAN_8=0x15;
(4)编写ServletTest类,它是处理条码生成请求的Servlet。在给类中创建JBarcodeBean类的全局成员变量barcode,它将被其他方法调用。关键代码如下。 public class ServletTestextends HttpServlet {
JBarcodeBean barcode; 编写Servlet的初始化方法init(),在该方法中初始化JBarcodeBean类的对象,并设置该对象在生成条形码时,显示条形码的文本信息。关键代码如下。
public void init(ServletConfig conf) throwsServletException {
super.init(conf);
barcode = new JBarcodeBean(); // 初始化条形码工具类
barcode.setShowText(true); //设置生成的条形码显示文本信息
}
编写processRequest()方法,在本实例中该方法处理处理GET方法和POST方法的页面请求,它通过matchType()方法确定生成条形码的编码类型,然后生成生成条形码的其他参数信息,例如生成条形码的产品编号、条形码的高度、编码类型等。最后,将生成的条形码输出到客户端的输出流。关键代码如下。 protected synchronized void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
response.setContentType("image/gif"); // 设置响应对象的数据类型为GIF图片
OutputStream out = response.getOutputStream(); // 获取响应对象的输出流
BarcodeStrategy codeType = null; // 创建条形码类型对象
String typeStr = request.getParameter("type");
int type = 0;
if (typeStr == null || typeStr.isEmpty()) {
type = Code_11;
} else {
type = Integer.parseInt(typeStr);
}
codeType = matchType(type); // 匹配指定编码类型
barcode.setCodeType(codeType); // 设置指定编码类型
barcode.setCode(request.getParameter("code"));// 设置条形码数值
barcode.setBarcodeHeight(70); // 设置条形码的高度
barcode.setCheckDigit(false);
barcode.setFont(barcode.getFont().deriveFont(14)); // 设置文本字体
barcode.gifEncode(out); // 输出条形码图像数据
}
编写matchType()方法,该方法用于生成和页面请求的编码格式相匹配的条形编码对象。关键代码如下。 private BarcodeStrategy matchType(int type) {
BarcodeStrategy codeType = null;
switch (type) {
// 设置条形码的编码类型
case Code_128:
codeType = new Code128();
break;
case Code_39_3_1:
codeType = new Code39();
break;
case Code_39_2_1:
codeType = new Code39_2to1();
break;
case Ext_Code_39_3_1:
codeType = new ExtendedCode39();
break;
case Ext_Code_39_2_1:
codeType = new ExtendedCode39_2to1();
break;
case Code_93:
codeType = new Code93();
break;
case Code_93_Extended:
codeType = new Code93Extended();
break;
case InterLeaved_25_3_1:
codeType = new Interleaved25();
break;
case InterLeaved_25_2_1:
codeType = new Interleaved25_2to1();
break;
case MSI_model10_check:
codeType = new MSI();
break;
case Codabar_3_1:
codeType = new Codabar();
break;
case Codabar_2_1:
codeType = new Codabar_2to1();
break;
case EAN_13:
codeType = new Ean13();
break;
case EAN_8:
codeType = new Ean8();
break;
default:
codeType = new Code11();
}
return codeType;
}
……// 省略部分代码
}
|