首先要去http://barcode4j.sourceforge.net/ 下载一个它的专属jar包,那个版本都可以,这里介绍的是version2.0的版本。
一般它有2种用法,一种是生成条形码的图片,那样可以直接生成图片,在jar包里面的example下面有例子,这里就不作介绍了;另一种用法是在web开发里面使用的,通过jar包里面提供的BarcodeServlet。在web开发里面会使用到三个jar文件,barcode4j,commons-cli-1.0.jar和avalon-framework-4.2.0.jar
只要你建立一个web工程,导入三个jar包,编写web.xml
<servlet> <servlet-name>BarcodeServlet</servlet-name> <servlet-class>org.krysalis.barcode4j.servlet.BarcodeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>BarcodeServlet</servlet-name> <url-pattern>/barcode</url-pattern> </servlet-mapping>
编写一个index.jsp文件
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<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>
<table border="1">
<tr>
<td>
<h1>
code39
</h1>
<img
src="<%=request.getContextPath()%>/barcode?msg=JCN090008381&type=code39&fmt=jpeg"
height="100px" width=300px />
</td>
<td>
<h1>
code128
</h1>
<img
src="<%=request.getContextPath()%>/barcode?msg=0123456789&type=code128&fmt=jpeg"
height="100px" width=300px />
</td>
<td>
<h1>
Codabar
</h1>
<img
src="<%=request.getContextPath()%>/barcode?msg=0123456789&type=codabar&fmt=jpeg"
height="100px" width=300px />
</td>
</tr>
<tr>
<td>
<h1>
intl2of5
</h1>
<img
src="<%=request.getContextPath()%>/barcode?msg=01234567890540&type=intl2of5&fmt=jpeg"
height="100px" width=300px />
</td>
<td>
<h1>
upc-a
</h1>
<img
src="<%=request.getContextPath()%>/barcode?msg=012345678912&type=upc-a&fmt=jpeg"
height="100px" width=300px />
</td>
<td>
<h1>
ean-13
</h1>
<img
src="<%=request.getContextPath()%>/barcode?msg=123456789234&type=ean-13&fmt=jpeg"
height="100px" width=300px />
</td>
<td>
<h1>
ean-8
</h1>
<img
src="<%=request.getContextPath()%>/barcode?msg=20123451&type=ean-8&fmt=jpeg"
height="100px" width=300px />
</td>
</tr>
<tr>
<td>
<h1>
postnet
</h1>
<img
src="<%=request.getContextPath()%>/barcode?msg=01234567890540&type=postnet&fmt=jpeg"
height="100px" width=300px />
</td>
<td>
<h1>
royal-mail-cbc
</h1>
<img
src="<%=request.getContextPath()%>/barcode?msg=012345AS678912&type=royal-mail-cbc&fmt=jpeg"
height="100px" width=300px />
</td>
<td>
<h1>
pdf417
</h1>
<img
src="<%=request.getContextPath()%>/barcode?msg=200123457893&type=pdf417&fmt=jpeg"
height="100px" width=300px />
</td>
<td>
<h1>
datamatrix
</h1>
<img
src="<%=request.getContextPath()%>/barcode?msg=20123451&type=datamatrix&fmt=jpeg"
height="100px" width=300px />
</td>
</tr>
<tr>
<td>
<img src="<%=request.getContextPath()%>/barcode?msg=JCN090008381"/>
</td>
<td><img src="<%=request.getContextPath()%>/barcode?msg=JCN090008381&type=code39&fmt=jpeg&height=1cm&hrfont=Arial&res=100"/></td>
<td><img src="<%=request.getContextPath()%>/barcode?msg=JCN090008381&type=code128&fmt=jpeg&height=1cm&hrfont=Arial&res=100"/></td>
</tr>
</table>
</body>
</html>
关于<%=request.getContextPath()%>/barcode? 后面的参数我确实要好好将解释一下
type=code128, 类型 (标准) 还有很多其它的例如 code39
msg=12345, 内容
height=2.5cm, 条形码高度
mw=0.5mm, 条形码斑马线的距离
wf=2, 还没知道
qz=10mw, 边空白的距离
hrp=bottom JCN090008381 在下面
hrp=top JCN090008381 在上面
fmt=jpeg 图片格式 一般最好使用这个格式,其它格式可能某些浏览器不支持
hrpattern
hrfont 字体类型
hrsize
gray
res 默认100 改变图片的整体大小