jsp打印条形码

来源:互联网 发布:vb出错弹出对话框 编辑:程序博客网 时间:2024/06/10 17:32

条形码,用的比较多的是barbecue和barcode4j。

 

一、barcode4j

1、 google或者摆渡一下barcode4j要用的东东,下载压缩包barcode4j-2.0alpha2-bin.zip和barcode4j-2.0alpha2-src.zip。


2、 解压barcode4j-2.0alpha2-bin.zip这个包,在build目录下有barcode4j.jar,在lib目录下有avalon-framework-4.2.0.jar, 将barcode4j.jar和avalon-framework-4.2.0.jar添加到项目的lib中,刷新工程,然后在项目配置中将这两个jar包添加到classpath里面去。

3、 解压将barcode4j-2.0alpha2-src.zip,将src/java/org/krysalis/barcode4j/servlet目录下的BarcodeServlet.java类的代码拷出来,修改默认的图片显示方式,BarcodeServlet.java第168行的默认图片显示格式改成jpeg(因为不是所有的浏览器都支持svg格式,也不是所有的浏览者都愿意给自己的浏览器加一个这样的插件)

4、 将以下这段servlet配置在web.xml中
<servlet>
    <servlet-name>BarcodeServlet</servlet-name>
    <servlet-class>com.yourname.BarcodeServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>BarcodeServlet</servlet-name>
    <url-pattern>/barcode</url-pattern>
  </servlet-mapping>

4、 在页面中添加<img src="<%=request.getContextPath() %>/barcode?msg=12345678" height="50px" width=130px/>

5、 查看该页面就可以发现条形码为“12345678”的图片了,当然了图片大小就自己设置了。

 

 

二、barbecue

将 barbecue-1.5-beta1.jar 放入工程下的 WEB_INFO/lib目录里
在工程的web.xml里增加 servlet映射文件

<img src="<%=request.getContextPath()%>/barcode?data=123456789&type=Code39&width=1&height=20">

参数解释
data=123456789 要生成条形码的字符
type=Code39   表示是code39码 ,条形码类型
width 表示生成的条形码的宽度
height=20 表示生成的条形码的高度

使用稍微需要注意一点的是 条形码下面是否同时显示条形码内容.下面这几句话是设置方法
headless true for headless mode (default value), false to force non-headless mode
drawText (only takes effect if headless = false) true to draw text of barcode, false to omit
"无头" true 为无头模式(默认值为true),false 为强制非无头模式
显示文本(只有headless=false时才有效) 为true 时 显示条形码的内容,false 不显示

简单的说就是默认为条形码下方不显示条形码内容,
如果 headless=false&drawText=true 则条形码下方显示条形码的内容

Just so easy!

官方帮助文档内容如下
Installation:
-------------

Barbecue comes with a pre-compiled war file in the distribution that you can drop straight
into a servlet compatible application server (e.g. Tomcat).

The Barbecue servlet is configured to be available from the following URL by default:

http://host:port/barbecue/

Any requests with this prefix will be routed to the Barbecue servlet.



Usage:
------

The recommended way to use Barbecue as a servlet is to reference it as the src for an
img tag. For example:

<img src="http://localhost:8080/barbecue/barcode?data=123545&height=50"/>



Notes:
------

If you are running the servlet on a headless server (i.e. one with no monitor or graphics
environment) then you must ensure that you use the servlet headless parameter, and also
set the following JVM property:

-Djava.awt.headless=true
-------------------------------------------------------------------


Servlet class: net.sourceforge.barbecue.BarcodeServlet

Required parameters:
--------------------

data the data to encode


Optional parameters:
--------------------

typethe barcode type - see below
widththe width of the smallest bar in the barcode in pixels
heightthe height of the barcode in pixels
resolutionthe output resolution in DPI
checksumtrue to include a calculated checksum, false to omit (only used by Code39 and derivatives)
headlesstrue for headless mode (default value), false to force non-headless mode
drawText(only takes effect if headless = false) true to draw text of barcode, false to omit


Types:
------

Valid barcode types are (case insensitive):

Code128
Code128A
Code128B
Code128C
EAN128
USPS
ShipmentIdentificationNumber
SSCC18
SCCC14ShippingCode

原创粉丝点击