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

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