jsp打印条形码

条形码,用的比较多的是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:
--------------------

type the barcode type - see below
width the width of the smallest bar in the barcode in pixels
height the height of the barcode in pixels
resolution the output resolution in DPI
checksum true to include a calculated checksum, false to omit (only used by Code39 and derivatives)
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


Types:
------

Valid barcode types are (case insensitive):

Code128
Code128A
Code128B
Code128C
EAN128
USPS
ShipmentIdentificationNumber
SSCC18
SCCC14ShippingCode

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章