java用JBarcode组件生成条形码(支持自定义字体及颜色)

前言:

JBarcode入门教程我就不写了,可以参考:点击打开链接

我的这篇教程和上篇博客的不同之处:

1 上篇博客直接生成二维码图片放到d盘的某个文件夹下,我的二维码生成二维码后直接用Base64编码然后返回到前台页面。

2 上篇博客只介绍了生成商品条形码,其他二维如Code93码、ISBN码、ISSN码、Code128码等等都没有介绍。我的博客里会介绍这些条码怎么生成。我文末会介绍常用条码及其如何选择适合自己项目的条码。

博客正文:

1 什么是JBarcode工具,这个工具支持哪些条码



2 如何用 JBarcode生成条形码,我这里提供生成商品条形码和128条形码

  1. package barcode;  
  2. import java.awt.image.BufferedImage;  
  3. import java.io.ByteArrayOutputStream;  
  4.   
  5. import javax.imageio.ImageIO;  
  6.   
  7. import org.apache.commons.lang.StringUtils;  
  8. import org.jbarcode.JBarcode;  
  9. import org.jbarcode.encode.Code128Encoder;  
  10. import org.jbarcode.encode.EAN13Encoder;  
  11. import org.jbarcode.encode.InvalidAtributeException;  
  12. import org.jbarcode.paint.BaseLineTextPainter;  
  13. import org.jbarcode.paint.EAN13TextPainter;  
  14. import org.jbarcode.paint.WidthCodedPainter;  
  15.   
  16. import sun.misc.BASE64Encoder;  
  17.    
  18. public class BarcodeUtil {  
  19.       
  20.     /**  
  21.      * 128条形码  
  22.      *  
  23.      * @param strBarCode  
  24.      *            条形码:0-100位  
  25.      * @param dimension  
  26.      *            商品条形码:尺寸  
  27.      * @param barheight  
  28.      *            商品条形码:高度  
  29.      * @return 图片(Base64编码)  
  30.      */  
  31.       public static String generateBarCode128(String strBarCode,String dimension,String barheight) {  
  32.               
  33.        
  34.             try {  
  35.                 ByteArrayOutputStream outputStream = null;  
  36.                 BufferedImage bi = null;  
  37.                 int len = strBarCode.length();  
  38.                 JBarcode productBarcode = new JBarcode(Code128Encoder.getInstance(),  
  39.                         WidthCodedPainter.getInstance(),  
  40.                         EAN13TextPainter.getInstance());  
  41.        
  42.                 // 尺寸,面积,大小 密集程度  
  43.                 productBarcode.setXDimension(Double.valueOf(dimension).doubleValue());  
  44.                 // 高度 10.0 = 1cm 默认1.5cm  
  45.                 productBarcode.setBarHeight(Double.valueOf(barheight).doubleValue());  
  46.                 // 宽度  
  47.                 productBarcode.setWideRatio(Double.valueOf(30).doubleValue());  
  48. //                  是否显示字体  
  49.                 productBarcode.setShowText(true);  
  50. //                 显示字体样式  
  51.                 productBarcode.setTextPainter(BaseLineTextPainter.getInstance());   
  52.        
  53.                 // 生成二维码  
  54.                 bi = productBarcode.createBarcode(strBarCode);  
  55.                   
  56.                 outputStream = new ByteArrayOutputStream();  
  57.                 ImageIO.write(bi, “jpg”, outputStream);  
  58.                 BASE64Encoder encoder = new BASE64Encoder();  
  59. //            System.err.println(encoder.encode(outputStream.toByteArray()));  
  60.   
  61.                 return encoder.encode(outputStream.toByteArray());  
  62.             } catch (Exception e) {  
  63.                 e.printStackTrace();  
  64.                 return “encodeError”;  
  65.             }  
  66.         }  
  67.       
  68.       
  69.       
  70.       
  71.    
  72.     /**  
  73.      * 商品条形码  
  74.      * @param strBarCode  
  75.      *            商品条形码:13位  
  76.      * @param dimension  
  77.      *            商品条形码:尺寸  
  78.      * @param barheight  
  79.      *            商品条形码:高度  
  80.      * @return 图片(Base64编码)  
  81.      */  
  82.     public static String generateBarCode(String strBarCode,String dimension,String barheight) {  
  83. //      isNumeric 是否是数值  
  84. //      校验。。。。。  
  85.           
  86.    
  87.         try {  
  88.             ByteArrayOutputStream outputStream = null;  
  89.             BufferedImage bi = null;  
  90.             int len = strBarCode.length();  
  91.             JBarcode productBarcode = new JBarcode(EAN13Encoder.getInstance(),  
  92.                     WidthCodedPainter.getInstance(),  
  93.                     EAN13TextPainter.getInstance());  
  94.               
  95.             String barCode = strBarCode.substring(0, len - 1);  
  96.             String code = strBarCode.substring(len - 1, len);  
  97.               
  98.             //校验13位  
  99.             String checkCode = productBarcode.calcCheckSum(barCode);  
  100.             if (!code.equals(checkCode)) {  
  101.                 return “checkCodeError”;  
  102.             }  
  103.    
  104.    
  105.             // 尺寸,面积,大小  
  106.             productBarcode.setXDimension(Double.valueOf(dimension).doubleValue());  
  107.             // 高度 10.0 = 1cm 默认1.5cm  
  108.             productBarcode.setBarHeight(Double.valueOf(barheight).doubleValue());  
  109.             // 宽度  
  110.             productBarcode.setWideRatio(Double.valueOf(25).doubleValue());  
  111.               
  112.             // 是否校验13位,默认false  
  113.             productBarcode.setShowCheckDigit(true);  
  114.               
  115.           //显示字符串内容中是否显示检查码内容  
  116. //          productBarcode.setShowCheckDigit(true);  
  117.    
  118.             // 生成二维码  
  119.             bi = productBarcode.createBarcode(barCode);  
  120.               
  121.             outputStream = new ByteArrayOutputStream();  
  122.             ImageIO.write(bi, “jpg”, outputStream);  
  123.             BASE64Encoder encoder = new BASE64Encoder();  
  124. //          System.err.println(encoder.encode(outputStream.toByteArray()));  
  125.   
  126.             return encoder.encode(outputStream.toByteArray());  
  127.         } catch (Exception e) {  
  128.             e.printStackTrace();  
  129.             return “encodeError”;  
  130.         }  
  131.     }  
  132.    
  133.     /**  
  134.      * @param args  
  135.      * @throws InvalidAtributeException  
  136.      */  
  137.     public static void main(String[] args) throws InvalidAtributeException {  
  138.    
  139.         String encode = BarcodeUtil.generateBarCode(“6936983800013”,”0.5”,”30”);  
  140.         String encode2 = BarcodeUtil.generateBarCode128(“69369833450938430579753045230800013”,”0.5”,”30”);  
  141.    
  142.         System.out.println(encode);  
  143.    
  144.     }  
  145.    
  146. }  
package barcode;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;

import javax.imageio.ImageIO;

import org.apache.commons.lang.StringUtils;
import org.jbarcode.JBarcode;
import org.jbarcode.encode.Code128Encoder;
import org.jbarcode.encode.EAN13Encoder;
import org.jbarcode.encode.InvalidAtributeException;
import org.jbarcode.paint.BaseLineTextPainter;
import org.jbarcode.paint.EAN13TextPainter;
import org.jbarcode.paint.WidthCodedPainter;

import sun.misc.BASE64Encoder;

public class BarcodeUtil {

    /**
     * 128条形码
     *
     * @param strBarCode
     *            条形码:0-100位
     * @param dimension
     *            商品条形码:尺寸
     * @param barheight
     *            商品条形码:高度
     * @return 图片(Base64编码)
     */
      public static String generateBarCode128(String strBarCode,String dimension,String barheight) {


            try {
                ByteArrayOutputStream outputStream = null;
                BufferedImage bi = null;
                int len = strBarCode.length();
                JBarcode productBarcode = new JBarcode(Code128Encoder.getInstance(),
                        WidthCodedPainter.getInstance(),
                        EAN13TextPainter.getInstance());

                // 尺寸,面积,大小 密集程度
                productBarcode.setXDimension(Double.valueOf(dimension).doubleValue());
                // 高度 10.0 = 1cm 默认1.5cm
                productBarcode.setBarHeight(Double.valueOf(barheight).doubleValue());
                // 宽度
                productBarcode.setWideRatio(Double.valueOf(30).doubleValue());
//                  是否显示字体
                productBarcode.setShowText(true);
//                 显示字体样式
                productBarcode.setTextPainter(BaseLineTextPainter.getInstance()); 

                // 生成二维码
                bi = productBarcode.createBarcode(strBarCode);

                outputStream = new ByteArrayOutputStream();
                ImageIO.write(bi, "jpg", outputStream);
                BASE64Encoder encoder = new BASE64Encoder();
//            System.err.println(encoder.encode(outputStream.toByteArray()));

                return encoder.encode(outputStream.toByteArray());
            } catch (Exception e) {
                e.printStackTrace();
                return "encodeError";
            }
        }





    /**
     * 商品条形码
     * @param strBarCode
     *            商品条形码:13位
     * 13位条形码是有一定规则的。13位商品条码数的含义
     *   1、商品条码前3位是“国别码”,“690-695”开头的,该商品一般产自中国。
     *   2、厂商代码“第4-8位”,由国际物品编码协会统一分配。
     *   3、条码“9-12位”数,代表厂内商品代码,由厂商自行确定。
     *   4、条码最后一位数为校验码,由前面的数字通过特殊公式计算得出。
     *   5、商品条码我们常见上面标注有“EAN”,这个EAN码是国际物品编码协会制定第一种商品用条码,通用语全世界。有13位数的标准版(EAN-13)和8位数的(EAN-8)两种版本。
     * @param dimension
     *            商品条形码:尺寸
     * @param barheight
     *            商品条形码:高度
     * @return 图片(Base64编码)
     */
    public static String generateBarCode(String strBarCode,String dimension,String barheight) {
//      isNumeric 是否是数值
//      校验。。。。。


        try {
            ByteArrayOutputStream outputStream = null;
            BufferedImage bi = null;
            int len = strBarCode.length();
            JBarcode productBarcode = new JBarcode(EAN13Encoder.getInstance(),
                    WidthCodedPainter.getInstance(),
                    EAN13TextPainter.getInstance());

            String barCode = strBarCode.substring(0, len - 1);
            String code = strBarCode.substring(len - 1, len);

            //校验13位
            String checkCode = productBarcode.calcCheckSum(barCode);
            if (!code.equals(checkCode)) {
                return "checkCodeError";
            }


            // 尺寸,面积,大小
            productBarcode.setXDimension(Double.valueOf(dimension).doubleValue());
            // 高度 10.0 = 1cm 默认1.5cm
            productBarcode.setBarHeight(Double.valueOf(barheight).doubleValue());
            // 宽度
            productBarcode.setWideRatio(Double.valueOf(25).doubleValue());

            // 是否校验13位,默认false
            productBarcode.setShowCheckDigit(true);

          //显示字符串内容中是否显示检查码内容
//          productBarcode.setShowCheckDigit(true);

            // 生成二维码
            bi = productBarcode.createBarcode(barCode);

            outputStream = new ByteArrayOutputStream();
            ImageIO.write(bi, "jpg", outputStream);
            BASE64Encoder encoder = new BASE64Encoder();
//          System.err.println(encoder.encode(outputStream.toByteArray()));

            return encoder.encode(outputStream.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
            return "encodeError";
        }
    }

    /**
     * @param args
     * @throws InvalidAtributeException
     */
    public static void main(String[] args) throws InvalidAtributeException {

        String encode = BarcodeUtil.generateBarCode("6936983800013","0.5","30");
        String encode2 = BarcodeUtil.generateBarCode128("69369833450938430579753045230800013","0.5","30");

        System.out.println(encode);

    }

}

有的读者想生成Code 11, Code 93类型的条码怎么生成呢?很简单

答案就在创建Jbarcode对象代码里,如果你想生成128条码

  1. JBarcode productBarcode = new JBarcode(Code128Encoder.getInstance(),  
  2.                     WidthCodedPainter.getInstance(),  
  3.                     EAN13TextPainter.getInstance());  
 JBarcode productBarcode = new JBarcode(Code128Encoder.getInstance(),
                        WidthCodedPainter.getInstance(),
                        EAN13TextPainter.getInstance());
如果你想生成EAN13Ender条码

  1. JBarcode productBarcode = new JBarcode(EAN13Encoder.getInstance(),  
  2.                     WidthCodedPainter.getInstance(),  
  3.                     EAN13TextPainter.getInstance());  
JBarcode productBarcode = new JBarcode(EAN13Encoder.getInstance(),
                    WidthCodedPainter.getInstance(),
                    EAN13TextPainter.getInstance());

看到红色字体里了吧,想生成什么条码直接xxx.getInstance即可,简单吧


3 页面显示

生成图片后,用Base64编码后得到字符串,假如为:”123xyz”

页面jsp里<img src=”data:image/png;base64,123xyz”/>即可


注意-注意条形码的字体样式!!!

我当时就在这里栽了,不同的字体样式显示效果不一样,甚至可以用天上地下来形容他们的差距。最后没办法,只有看

祝你成功!

转载:http://blog.csdn.net/wabiaozia/article/details/52747772
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值