现在的位置: 首页 > 综合 > 正文

Java生成条形码

2018年05月28日 ⁄ 综合 ⁄ 共 5337字 ⁄ 字号 评论关闭

    用JAVA显示各种规格的条形码(18),打印后可以被扫描设备扫描.
需要的java类请到CSDN中下载barcode.zip(http://download.csdn.net/source/743185)

1.把barcode4java类引入自己的项目中;
2.
web.xml中配置一个生成条形码的servlet,如下:

Html代码

  1.   

  2. ...   

  3. <servlet>  

  4.     <servlet-name>CreateBarCode</servlet-name>  

  5.     <servlet-class>com.soft4j.barcode.BarCodeServlet</servlet-class>  

  6. </servlet>  

  7. ...   

  8. <servlet-mapping>  

  9.     <servlet-name>CreateBarCode</servlet-name>  

  10.     <url-pattern>/CreateBarCode</url-pattern>  

  11. </servlet-mapping>  

  12. ...  

 

  ...

  <servlet>

   <servlet-name>CreateBarCode</servlet-name>

   <servlet-class>com.soft4j.barcode.BarCodeServlet</servlet-class>

  </servlet>

  ...

  <servlet-mapping>

   <servlet-name>CreateBarCode</servlet-name>

   <url-pattern>/CreateBarCode</url-pattern>

  </servlet-mapping>

  ...


   
这样就可以使用CreateBarCode这个servlet来生成条形码了。
   
备注
:
    (1)com.soft4j.barcode.BarCodeServlet
,这里填写自己项目中的实际包路径

    (2)
关于CreateBarCode这个名字可以自行设定。

   
3.
前台引用例子(BarcodeExample.jsp)

Html代码

  1. <%@ page contentType="text/html;charset=UTF-8"%><%   

  2.     String code = "123456789";//条形码内容   

  3. %>  

  4. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  

  5. <html><head><title></title>  

  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  

  7. <script language="javascript">  

  8. <!--   

  9. NS4 = (document.layers) ? 1 : 0;   

  10. visble_property_prefix = (NS4) ? "document.layers." : "";   

  11. visble_property_suffix = (NS4) ? ".visibility" : ".style.display";   

  12. visble_property_true = (NS4) ? "show" : "block";   

  13. visble_property_false = (NS4) ? "hide" : "none";   

  14. visble_property_printview = visble_property_prefix + "viewpanel" + visble_property_suffix;   

  15. function nowprint() {   

  16.     window.print();   

  17. }   

  18. function window.onbeforeprint() {   

  19.     eval(visble_property_printview + " = /"" + visble_property_false + "/"");   

  20. }   

  21. function window.onafterprint() {   

  22.     eval(visble_property_printview + " = /"" + visble_property_true + "/"");   

  23. }   

  24. //-->  

  25. </script>  

  26. </head>  

  27. <body topmargin="0px" leftmargin="0px" rightmargin="0px" bottommargin="0px">  

  28. <%      

  29.     StringBuffer barCode = new StringBuffer();   

  30.     barCode.append("<img src='");   

  31.     barCode.append(request.getContextPath());   

  32.     barCode.append("/CreateBarCode?code=");   

  33.     barCode.append(code);   

  34.     barCode.append("&barType=CODE39&checkCharacter=n&checkCharacterInText=n'>");   

  35.     out.println(barCode.toString());   

  36. %>  

  37. <div id="viewpanel" align="center">  

  38. <input name="bequery" type="button" value="  " style="cursor:hand;" onclick="nowprint();">  

  39. </div>  

  40. <!--script>nowprint();</script-->  

  41. </body>  

  42. </html>    

<%@ page contentType="text/html;charset=UTF-8"%><%

String code = "123456789";//条形码内容

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html><head><title></title>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<script language="javascript">

<!--

NS4 = (document.layers) ? 1 : 0;

visble_property_prefix = (NS4) ? "document.layers." : "";

visble_property_suffix = (NS4) ? ".visibility" : ".style.display";

visble_property_true = (NS4) ? "show" : "block";

visble_property_false = (NS4) ? "hide" : "none";

visble_property_printview = visble_property_prefix + "viewpanel" + visble_property_suffix;

function nowprint() {

window.print();

}

function window.onbeforeprint() {

eval(visble_property_printview + " = /"" + visble_property_false + "/"");

}

function window.onafterprint() {

eval(visble_property_printview + " = /"" + visble_property_true + "/"");

}

//-->

</script>

</head>

<body topmargin="0px" leftmargin="0px" rightmargin="0px" bottommargin="0px">

<%

StringBuffer barCode = new StringBuffer();

barCode.append("<img src='");

barCode.append(request.getContextPath());

barCode.append("/CreateBarCode?code=");

barCode.append(code);

barCode.append("&barType=CODE39&checkCharacter=n&checkCharacterInText=n'>");

out.println(barCode.toString());

%>

<div id="viewpanel" align="center">

<input name="bequery" type="button" value="  " style="cursor:hand;" onclick="nowprint();">

</div>

<!--script>nowprint();</script-->

</body>

</html> 

   

4.关于条形码使用时可以设置的一些参数:
  ------------------------------------
  barType(
默认为CODE128):   条码类型.支持18钟类型,分别是
CODE39,CODE39EXT,INTERLEAVED25,CODE11,CODABAR,MSI,UPCA,IND25,MAT25,CODE93,EAN13,EAN8,UPCE,CODE128,CODE93EXT,POSTNET,PLANET,UCC128 .
  code:  
要打印的条码内容
.   
  width(
默认为自适应,一般不用自行设置):   图片宽度.width,height要同时都设置才有效
.    
  height(
默认为自适应,一般不用自行设置):   图片高度.width,height要同时都设置才有效
.    
  st(
默认为y显示):   是否显示条码内容(show   text).默认会在条码图片下方显示条码内容,有效值为y
n.    
  textFont(
默认为Arial|PLAIN|11):   条码文本的字体,字体有效格式为<font   name>|<style>|<size>.Style可以是PLAIN,ITALIC
BOLD.    
  fontColor(
默认为黑色):   条码文本的颜色,有效值为RED,BLUE,GREEN,BLACK,GRAY,LIGHTGRAY,WHITE,DARKGRAY,YELLOW,ORANGE,CYAN
MAGENTA.    
  barColor(
默认为黑色):   条码的颜色
.    
  backColor(
默认为白色):   图片背景颜色
.    
  rotate(
默认为0):   设置条码旋转角度.有效值为0(不旋转),90(旋转90),180(旋转180),270(旋转270
).  
  barHeightCM(
默认为1厘米):   条码的高度
.    
  x(
默认为0.03厘米,一般不用自行调整):   条码符号中窄单元的标称尺寸,最小可设置为0.0011象素,通常以0.03递增
.  
  n(
默认为2):   宽窄比,平均宽条的条宽与平均宽空的空宽之和(条码字符间隔不计在内)除以两倍窄单元尺寸.它是宽度调节编码法中的技术参数
.  
  leftMarginCM(
默认为0.3厘米):   条码与图片左右边的距离
.    
  topMarginCM(
默认为0.2厘米):   条码与图片上下边的距离
.    
  checkCharacter(
默认为y,一般不用自行设置):   是否自动计算check   character,有效值为y
n.    
  checkCharacterInText(
默认为y,一般不用自行设置):   条码内容是否自动计算check   character,有效值为y
n.    
  Code128Set(
默认为0自动选择,一般不用设置):   设置CODE128中使用的字符集.有效值为
0,A,B,C.    
  UPCESytem(
默认为0,一般不用自行设置):   UPCE中使用的编码系统.有效值为0
1.    
  ------------------------------------
 
其中关键的参数主要是 barType,code 2.另外把checkCharactercheckCharacterInText2个参数设置成n,如果是y的话,会在你的条形码后面追加一个校验码,虽然不影响使用(扫描设备扫描条形码得到数据不会出现校验码),但是看着比较别扭(如下图
).

 

 

5.还可以在applet中使用,具体使用参数请对照BarCodeApplet.java文件,WEB使用方式差不多.

抱歉!评论已关闭.