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

生成条形码显示到界面

2018年05月21日 ⁄ 综合 ⁄ 共 6248字 ⁄ 字号 评论关闭
<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">1、生成条形码的java后台代码如下,需要下载</span><a target=_blank target="_blank" href="http://download.csdn.net/detail/fzdswz/7985773" style="font-family: Arial; font-size: 14px; line-height: 26px; color: rgb(51, 102, 153); text-decoration: none; background-color: rgb(255, 255, 255);">jbarcode-0.2.8.jar</a>

 

[java] view
plain
copy

  1. package com.xxx;  
  2.   
  3. import java.awt.image.BufferedImage;  
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.IOException;  
  6.   
  7. import javax.imageio.ImageIO;  
  8. import javax.servlet.ServletConfig;  
  9. import javax.servlet.ServletException;  
  10. import javax.servlet.http.HttpServlet;  
  11. import javax.servlet.http.HttpServletRequest;  
  12. import javax.servlet.http.HttpServletResponse;  
  13.   
  14. import org.jbarcode.JBarcode;  
  15. import org.jbarcode.encode.Code128Encoder;  
  16. import org.jbarcode.encode.Code39Encoder;  
  17. import org.jbarcode.encode.EAN13Encoder;  
  18. import org.jbarcode.encode.InvalidAtributeException;  
  19. import org.jbarcode.paint.BaseLineTextPainter;  
  20. import org.jbarcode.paint.EAN13TextPainter;  
  21. import org.jbarcode.paint.WideRatioCodedPainter;  
  22. import org.jbarcode.paint.WidthCodedPainter;  
  23.   
  24. public class BarCodeServlet extends HttpServlet {  
  25.     private static final long serialVersionUID = 1L;  
  26.     private JBarcode jBarcode;  
  27.   
  28.     /** 
  29.      * 初始化条形码对象 
  30.      */  
  31.     public void init(ServletConfig conf) throws ServletException {  
  32.         super.init(conf);  
  33.         // 默认生成code39类型条形码  
  34.         jBarcode = new JBarcode(Code39Encoder.getInstance(),  
  35.                 WideRatioCodedPainter.getInstance(),  
  36.                 BaseLineTextPainter.getInstance());  
  37.         jBarcode.setShowText(true);// 显示图片下字符串内容  
  38.         jBarcode.setShowCheckDigit(true);// 显示字符串内容中是否显示检查码内容  
  39.         jBarcode.setCheckDigit(false);// 不生成检查码  
  40.     }  
  41.   
  42.     public void doGet(HttpServletRequest req, HttpServletResponse resp)  
  43.             throws ServletException, IOException {  
  44.         resp.setContentType("image/gif");  
  45.         resp.setHeader("Pragma""no-cache");  
  46.         resp.setHeader("Cache-Control""no-cache");  
  47.         // 传入的参数,  
  48.         // 如果是判断条件,则用0,1表示,‘0’即无效或者不现实,‘1’即有效或者显示  
  49.         // 字符串参数  
  50.         String msg = req.getParameter("msg");  
  51.         // 是否显示图片下字符串内容  
  52.         String showText = req.getParameter("st");  
  53.         // 是否显示检查码内容  
  54.         String showCheckDigit = req.getParameter("scd");  
  55.         // 是否生成检查码  
  56.         String checkDigit = req.getParameter("cd");  
  57.         // 生成条形码类型  
  58.         String codeType = req.getParameter("codeType");  
  59.         //设置条形码barHeight  
  60.         String barH = req.getParameter("barH");  
  61.         //设置XDimension  
  62.         String barXD = req.getParameter("barXD");  
  63.         // 判断字符串内容  
  64.         if (msg == null)  
  65.             //msg = "788515004012";  
  66.         // 判断显示图片下字符串内容  
  67.         if ("0".equals(showText))  
  68.             jBarcode.setShowText(false);  
  69.         //判断barHeight  
  70.         if(barH != null && !barH.equals("")){  
  71.             double x = Double.valueOf(barH);  
  72.             jBarcode.setBarHeight(x);  
  73.         }  
  74.         if(barXD != null && !barXD.equals("")){  
  75.             try {  
  76.                 //jBarcode.setWideRatio(10);  
  77.                 jBarcode.setXDimension(Double.valueOf(barXD));  
  78.             } catch (InvalidAtributeException e) {  
  79.                 // TODO Auto-generated catch block  
  80.                 e.printStackTrace();  
  81.             }  
  82.         }else{  
  83.             try {  
  84.                 //jBarcode.setWideRatio(10);  
  85.                 jBarcode.setXDimension(Double.valueOf(0.5));  
  86.             } catch (InvalidAtributeException e) {  
  87.                 // TODO Auto-generated catch block  
  88.                 e.printStackTrace();  
  89.             }  
  90.         }  
  91.           
  92.         // 判断显示检查码内容  
  93.         if ("1".equals(showCheckDigit))  
  94.             jBarcode.setShowText(true);  
  95.         // 判断生成检查码  
  96.         if ("1".equals(checkDigit))  
  97.             jBarcode.setShowText(true);  
  98.         // 判断生成条形码类型  
  99.         if(codeType != null && !"".equals(codeType)){  
  100.             if (codeType.equalsIgnoreCase("EAN13")) {  
  101.                 // EAN13条形码基本属性,必要添加条件  
  102.                 jBarcode.setEncoder(EAN13Encoder.getInstance());  
  103.                 jBarcode.setPainter(WidthCodedPainter.getInstance());  
  104.                 jBarcode.setTextPainter(EAN13TextPainter.getInstance());  
  105.                 //必须需要以下属性  
  106.                 jBarcode.setCheckDigit(true);  
  107.             } else if(codeType.equalsIgnoreCase("code128")){  
  108.                 jBarcode.setEncoder(Code128Encoder.getInstance());  
  109.                 jBarcode.setPainter(WidthCodedPainter.getInstance());  
  110.                 jBarcode.setTextPainter(BaseLineTextPainter.getInstance());  
  111.                 jBarcode.setCheckDigit(false);  
  112.                 jBarcode.setShowCheckDigit(false);  
  113.             }else{  
  114.                 // 这里设置 根据不同的条件设定生成的条形码基本属性,如EAN13。  
  115.                 // 通过if...else...来判断即可  
  116.                 // 默认生成的是Code39  
  117.                 // 默认生成code39类型条形码  
  118.             }  
  119.         }  
  120.         // 生成条形码,并通过输出来展示在页面上  
  121.         try {  
  122.             ByteArrayOutputStream out = new ByteArrayOutputStream();  
  123.             BufferedImage localBufferedImage = jBarcode.createBarcode(msg);  
  124.             ImageIO.write(localBufferedImage, "png", out);  
  125.             byte[] b = out.toByteArray();  
  126.             resp.getOutputStream().write(b);  
  127.         } catch (InvalidAtributeException e) {  
  128.             e.printStackTrace();  
  129.         }  
  130.     }  
  131.   
  132.     public void doPost(HttpServletRequest req, HttpServletResponse resp)  
  133.             throws ServletException, IOException {  
  134.         try {  
  135.             doGet(req, resp);  
  136.         } catch (Exception e) {  
  137.             e.printStackTrace();  
  138.         }  
  139.     }  
  140.   
  141. }  

2、前台jsp显示代码:传递的参数可以自己设置,参考java后台代码jBarcode对象。

[html] view
plain
copy

  1. <img style="vertical-align:middle;" src="<%=request.getContextPath()%>/BarCodeServlet?msg=${placeOrderNo}&barH=12.2&barXD=0.4&codeType=code128"/>  
  2.               

3、在web.xml配置java类:

[java] view
plain
copy

  1. <servlet>  
  2.      <servlet-name>BarCodeServlet</servlet-name>  
  3.      <servlet-class>com.xxx.BarCodeServlet</servlet-class>  
  4.   </servlet>  
  5.    
  6.   <servlet-mapping>  
  7.      <servlet-name>BarCodeServlet</servlet-name>  
  8.      <url-pattern>/BarCodeServlet</url-pattern>  
  9.   </servlet-mapping>  

 

注:对于jBarcode的参数设置可以看引用的包里面的org.jbarcode.demo的代码。支持多种格式的条形码生成。

抱歉!评论已关闭.