Barbecue 条形码

来源:互联网 发布:剃光头软件 编辑:程序博客网 时间:2024/05/04 23:01

文前声明:我是从这个地址转载过来的

http://ejb-wawa.iteye.com/blog/188550
文章的名字我自己加的!他发表的日期是2008-04-30!现在的Barbecue版本至少是1.5了!


(转别人的)条形码
转自www.alibubu.com 
本周主题 -- 条形码制作- barbecue 及 krysalis-barcode 
SECTION 00 简介 

记得我在大学的时候, 去大众计算机实习工读, 那 时候在工业管理部门, 那时候我仅是呆呆笨笨的学生, 他们跟我说要我学 习 clipper 去印制条形码, 朦胧无知的我就乖乖地学习 clipper 艰深难懂完全 没有对象导向的语法, 不过, 当我出了社会, 协助一家 ec 网站建立 7-11 的整套系统, 那时候, 我采用了 asp 通过 activx 的组件产生 barcode. 最近刚好有人在 jsptw 问起 barcode 的产生, 我又回想当时制作的方式, 不由得好奇 java 是否 有相关的 opensource 可以使用, 在 sourceforge 上有存在着 barbecue 这个项目, Jiayun 大侠也提供了 krysalis barcode 这个 opensource 给我知道. 因此, 本周就介绍如何制作 Barcode . 

SECTION 01 Barbecue 



#BSD License# , 目前版本 1.0.6 RC1, 由此下载 binary/source 文件 

Barbecue 这个 opensource 项目使用 java 制作了 一些 barcode 的格式, 只需要通过简单又具有弹性的 java api 就可以使 用了. 他输出的格式为图片文件, 也可以使用在 Swing 之 中, 更可以输出成为 SVG 的格式. 

使用 Barbecue 这个项目非常简单, 当你下载他的 binary 文件解压缩后, 你会找到一个 barbecue.war文件, 你把它放到 %TOMCAT_HOME%/webapps/ 之下, 接着你可以直接执行 http://localhost:8080/barbecue/barcode?data=123545&height=50, 你就可以看到 


这样的 barcode 图片了 ( 此图片为1.0.5输出, 1.0.6 RC1有点 bug, 无法输出文字部分 ). 如果你要在其它的 jsp 使用的话, 你可以用 <img src="http://localhost:8080/barbecue/barcode?data=123545&height=50" >, 他就会内嵌在你的网页之中. 

SECTION 02 barbecue 参数解释 

我们可以 trace 一下 barbecue.war 的 web.xml 是怎么设置的 
<?xml version="1.0" 

encoding="UTF-8"?> 
<!DOCTYPE web-app 
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" 
    "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"> 

<web-app> 
    <servlet> 
        <servlet-name>barbecue</servlet-name> 
                <display-name>Barbecue</display-name> 
                <deSCRIPTion>Barcode 

generating servlet (serves images)</deSCRIPTion> 
        <servlet-class>net.sourceforge.barbecue.BarcodeServlet</servlet-class> 
                <load-on-startup>1</load-on-startup> 
    </servlet> 
        <servlet-mapping> 
                <servlet-name>barbecue</servlet-name> 
                <url-pattern>*</url-pattern> 
        </servlet-mapping> 
</web-app> 

他使用一个叫做 net.sourceforge.barbecue.BarcodeServlet 的程序接收传入的参数, 可以接收以下这几种参数 
data *: 这是必要项, 就是你 要对什么数值做 barcode 编码 
type: 以下是支持的格式列表 , 请注意大小写, 默认为 Code128B 
Code128 
Code128A 
Code128B 
Code128C 
EAN128 
USPS 
ShipmentIdentificationNumber 
SSCC18 
SCCC14ShippingCode 
GlobalTradeItemNumber 
UCC128 
PDF417 
Code39 
3of9 
USD3 
Codabar 
USD4 
NW7 
Monarch 
2of7 
appId: 只有 UCC128 格式需 要. 
width: 最小的 bar 宽度 pixels. 
height: 输出的图片高度 pixels. 
resolution: 输出的图片分辨 率 dpi. 
drawText: 默认为 true, 当 设为 false 时将不会同时绘出文字, 但是有些 type 即使设为 true 也不会绘出文字. (1.0.6RC1 此处有bug) 
drawText: 默认为 false, 当 设为 true 时将增加一个检查码, 只有 Code39 格式有用. 

SECTION 03 在 swing 中使用 barbecue 

我们如果要在 swing 中使用, 范例程序如下, 因为 barcode 本身就 是 JComponent, 所以可以直接 add(); 
import javax.swing.*; 
import net.sourceforge.barbecue.BarcodeFactory; 
import net.sourceforge.barbecue.Barcode; 
import 

net.sourceforge.barbecue.BarcodeException; 
import 

net.sourceforge.barbecue.BarcodeImageHandler; 

import java.awt.image.BufferedImage; 
import java.awt.*; 
import java.awt.event.*; 
import java.io.FileOutputStream; 
import java.io.IOException; 

public class 

SwingApplication { 

    public static 

void main(String[] 

args) { 
        //Create the top-level container and add contents to it. 
        JFrame frame = new JFrame("SwingApplication"); 
        SwingApplication app = new SwingApplication(); 

                Component contents = app.usingBarbecueAsSwingComponent(); 

        frame.getContentPane().add(contents, BorderLayout.CENTER); 

        //Finish setting up the frame, and show it. 
        frame.addWindowListener(new WindowAdapter() { 
            public void windowClosing(WindowEvent e) { 
                System.exit(0); 
            } 
        }); 
        frame.pack(); 
        frame.setVisible(true); 
    } 

        public Component usingBarbecueAsSwingComponent() { 

                // 

Always get a Barcode from the BarcodeFactory 
        Barcode barcode = null; 
        try { 
            barcode = BarcodeFactory.createCode128B("12345"); 
        } catch 

(BarcodeException e) { 
            // 

Error handling 
        } 

                return barcode; 
        } 


执行的结果是. 



SECTION 04 krysalis 的 barcode 



#ASF License# , 目前版本 0.9, 由此下载 binary/source 文件 

相同的, krysalis barcode 也有提供一个 krysalis-barcode.war, 你可以直接 deploy 到 %Tomcat_HOME%/webapps 之 下, 直接 http://localhost:8080/krysalis-barcode 就可以看到执行的页面了, krysalis barcode 主要输出 SVG 图片文件格式为 主, 如果没有安装过 SVG 的使用者, 可以到 adobe 的 网站下载 SVG Viewer 3.0.1 . 当我输入, type=code128, msg=12345, height=2.5cm, mw=0.5mm, wf=2, qz=10mw, hrp=bottom 的画面如下 




SECTION 05 krysalis barcode 的使用 

也是相同的观念, 使用了一个 org.krysalis.barcode.servlet.BarcodeServlet 作为接值及呈现的处理. 
<?xml version="1.0" 

encoding="ISO-8859-1"?> 
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" 
    "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"> 
<web-app> 
  <servlet> 
    <servlet-name>BarcodeServlet</servlet-name> 
    <servlet-class>org.krysalis.barcode.servlet.BarcodeServlet</servlet-class> 
  </servlet> 
  <servlet> 
    <servlet-name>BarcodeErrorServlet</servlet-name> 
    <servlet-class>org.krysalis.barcode.servlet.BarcodeErrorServlet< 

SPAN CLASS="syntax17"></servlet-class> 
  </servlet> 
  <servlet-mapping> 
    <servlet-name>BarcodeServlet</servlet-name> 
    <url-pattern>/gensvg</url-pattern> 
  </servlet-mapping> 
  <servlet-mapping> 
    <servlet-name>BarcodeErrorServlet</servlet-name> 
    <url-pattern>/errsvg</url-pattern> 
  </servlet-mapping> 
  <mime-mapping> 
    <extension>xhtml</extension> 
    <!--mime-type>application/xhtml+xml</mime-type--> 
    <!--the above is 

not supported by IE6--> 
    <mime-type>text/html</mime-type> 
  </mime-mapping> 
  <mime-mapping> 
    <extension>svg</extension> 
    <mime-type>image/svg+xml</mime-type> 
  </mime-mapping> 
  <mime-mapping> 
    <extension>svgz</extension> 
    <mime-type>image/svg+xml</mime-type> 
  </mime-mapping> 
  <error-page> 
    <exception-type>org.krysalis.barcode.BarcodeException</exception-type> 
    <location>/errsvg</location> 
  </error-page> 
</web-app> 

目前支持的 type 有以下几种 
Interleaved 2 of 5 
Code 39 
Codabar 
Code 128 
UPC-A 
UPC-E 
EAN-13 
EAN-8 

SECTION 06 krysalis barcode 输出的 SVG 文件 

当我们执行 gensvg?type=code128&msg=12345&height=2.5cm&mw=0.5mm&wf=2&qz=10mw&hrp=bottom 他会产生一个 svg 文件. 文件内容就是 XML 文件. 内容如下.. 
<?xml version="1.0" 

standalone="no"?> 
<!DOCTYPE svg> 
<svg 
        height="25mm" 
        width="49.5mm" 
        preserveAspectRatio="xMidYMid meet" 
        xmlns:xmlns="http://www.w3.org/2000/svg" 
zoomAndPan="magnify"> 
<g style="fill:black; stroke:none"> 
        <rect height="22.1781mm" width="1mm" x="5mm" y="0mm"/> 
        <rect height="22.1781mm" width="0.5mm" x="6.5mm" y="0mm"/> 
        <rect height="22.1781mm" width="1.5mm" x="8mm" y="0mm"/> 
        <rect height="22.1781mm" width="0.5mm" x="10.5mm" y="0mm"/> 
        <rect height="22.1781mm" width="1mm" x="11.5mm" y="0mm"/> 
        <rect height="22.1781mm" width="1.5mm" x="13.5mm" y="0mm"/> 
        <rect height="22.1781mm" width="0.5mm" x="16mm" y="0mm"/> 
        <rect height="22.1781mm" width="0.5mm" x="18mm" y="0mm"/> 
        <rect height="22.1781mm" width="1mm" x="19mm" y="0mm"/> 
        <rect height="22.1781mm" width="0.5mm" x="21.5mm" y="0mm"/> 
        <rect height="22.1781mm" width="2mm" x="22.5mm" y="0mm"/> 
        <rect height="22.1781mm" width="1.5mm" x="25mm" y="0mm"/> 
        <rect height="22.1781mm" width="1mm" x="27mm" y="0mm"/> 
        <rect height="22.1781mm" width="1.5mm" x="28.5mm" y="0mm"/> 
        <rect height="22.1781mm" width="0.5mm" x="31mm" y="0mm"/> 
        <rect height="22.1781mm" width="1.5mm" x="32.5mm" y="0mm"/> 
        <rect height="22.1781mm" width="0.5mm" x="34.5mm" y="0mm"/> 
        <rect height="22.1781mm" width="1mm" x="35.5mm" y="0mm"/> 
        <rect height="22.1781mm" width="1mm" x="38mm" y="0mm"/> 
        <rect height="22.1781mm" width="1.5mm" x="40.5mm" y="0mm"/> 
        <rect height="22.1781mm" width="0.5mm" x="42.5mm" y="0mm"/> 
        <rect height="22.1781mm" width="1mm" x="43.5mm" y="0mm"/> 
        <text 
          style="font-family:Helvetica; 

font-size:8.0pt; 

text-anchor:middle" 
          x="24.75mm" 
          y="24.4356mm" 
          startOffset="0" > 
                12345 
        </text> 
</g> 
</svg> 

SECTION 07 结论 

目前我们所见到的都是属于一维条形码, 未来希望能够看到二维甚至多维条形码的制作或是 其它 opensource 能够出现. 当然, 如果你们有特别制作特殊的条形码格式, 或 是有一些比较冷僻的条形码标准不在这两个项目中, 希望你能够加入他们的开发工程.. 感谢这么多愿意默默付出的工程师们, 相较之下, 自己非常的渺小