Jbarcode 条形码生成工具

一、准备jar包

https://sourceforge.net/projects/jbcode/?source=typ_redirect

二、编写工具类

  1 package com.example.demo.utils;
  2 
  3 import org.jbarcode.JBarcode;
  4 import org.jbarcode.JBarcodeFactory;
  5 import org.jbarcode.encode.Code128Encoder;
  6 import org.jbarcode.encode.InvalidAtributeException;
  7 import org.jbarcode.paint.TextPainter;
  8 import org.jbarcode.util.ImageUtil;
  9 
 10 import java.awt.*;
 11 import java.awt.image.BufferedImage;
 12 import java.io.*;
 13 import java.util.ArrayList;
 14 import java.util.List;
 15 
 16 /**
 17  * @author zsh
 18  * @company wlgzs
 19  * @create 2019-03-10 10:37
 20  * @Describe Jbarcode 条形码生成工具
 21  * 备注:
 22  * 1.静态代码块的作用:当类被载入时,静态代码块被执行,且只被执行一次,静态块常用来执行类属性的初始化。
 23  * 2.常量条形码的高度和字体大小设置很重要,若是设置小了会看不到设置的文件
 24  */
 25 public class JbarcodeUtil {
 26 
 27     //设置条形码高度
 28     private static final int BARCODE_HEIGHT = 40;
 29     //设置条形码默认分辨率
 30     private static final int BARCODE_DPI = ImageUtil.DEFAULT_DPI;
 31     //设置条形码字体样式
 32     private static final String FONT_FAMILY = "console";
 33     //设置条形码字体大小
 34     private static final int FONT_SIZE = 15;
 35     //设置条形码文本
 36     public static String TEXT = "";
 37     //创建jbarcode
 38     private static JBarcode jbc = null;
 39 
 40     static JBarcode getJBarcode() throws InvalidAtributeException {
 41         /**
 42          * 参考设置样式:
 43          *barcode.setEncoder(Code128Encoder.getInstance()); //设置编码
 44          *barcode.setPainter(WidthCodedPainter.getInstance());// 设置Painter
 45          *barcode.setTextPainter(BaseLineTextPainter.getInstance()); //设置TextPainter
 46          *barcode.setBarHeight(17); //设置高度
 47          *barcode.setWideRatio(Double.valueOf(30).doubleValue());// 设置宽度比率
 48          *barcode.setXDimension(Double.valueOf(2).doubleValue()); // 设置尺寸,大小 密集程度
 49          *barcode.setShowText(true); //是否显示文本
 50          *barcode.setCheckDigit(true); //是否检查数字
 51          *barcode.setShowCheckDigit(false); //是否显示检查数字
 52          */
 53         if (jbc == null) {
 54             //生成code128
 55             jbc = JBarcodeFactory.getInstance().createCode128();
 56             jbc.setEncoder(Code128Encoder.getInstance());
 57             jbc.setTextPainter(CustomTextPainter.getInstance());
 58             jbc.setBarHeight(BARCODE_HEIGHT);
 59             jbc.setXDimension(Double.valueOf(0.8).doubleValue());
 60             jbc.setShowText(true);
 61         }
 62         return jbc;
 63     }
 64 
 65     /**
 66      * @descript:生成条形码文件
 67      * @param message  条形码内容
 68      * @param file   生成文件
 69      */
 70     public static void createBarcode(String message, File file, String text) {
 71         try {
 72             FileOutputStream fos = new FileOutputStream(file);
 73             createBarcode(message, fos,text);
 74             fos.close();
 75         } catch (IOException e) {
 76             throw new RuntimeException(e);
 77         }
 78     }
 79 
 80     /**
 81      * @descript:生成条形码并写入指定输出流
 82      * @param message   条形码内容
 83      * @param os   输出流
 84      */
 85     public static void createBarcode(String message, OutputStream os, String text) {
 86         try {
 87             //设置条形码文本
 88             TEXT=text;
 89             //创建条形码的BufferedImage图像
 90             BufferedImage image = getJBarcode().createBarcode(message);
 91             ImageUtil.encodeAndWrite(image, ImageUtil.PNG, os, BARCODE_DPI, BARCODE_DPI);
 92             os.flush();
 93         } catch (Exception e) {
 94             throw new RuntimeException(e);
 95         }
 96     }
 97 
 98     /**
 99      * 静态内部类
100      * 自定义的 TextPainter, 允许定义字体,大小,文本等
101      * 参考底层实现:BaseLineTextPainter.getInstance()
102      */
103     protected static class CustomTextPainter implements TextPainter {
104         private static CustomTextPainter instance =new CustomTextPainter();
105         public static CustomTextPainter getInstance() {
106             return instance;
107         }
108         public void paintText(BufferedImage barCodeImage, String text, int width) {
109             //绘图
110             Graphics g2d = barCodeImage.getGraphics();
111             //创建字体
112             Font font = new Font(FONT_FAMILY, Font.PLAIN, FONT_SIZE * width);
113             g2d.setFont(font);
114             FontMetrics fm = g2d.getFontMetrics();
115             int height = fm.getHeight();
116             int center = (barCodeImage.getWidth() - fm.stringWidth(text)) / 2;
117             g2d.setColor(Color.WHITE);
118             g2d.fillRect(0, 0, barCodeImage.getWidth(), barCodeImage.getHeight() * 1 / 20);
119             g2d.fillRect(0, barCodeImage.getHeight() - (height * 9 / 10), barCodeImage.getWidth(), (height * 9 / 10));
120             g2d.setColor(Color.BLACK);
121             //绘制文本
122             g2d.drawString(TEXT, 0, 145);
123             //绘制条形码
124             g2d.drawString(text, center, barCodeImage.getHeight() - (height / 10) - 2);
125         }
126     }
127 
128     //测试
129     public static void main(String[] args) throws FileNotFoundException, IOException {
130         List<String> list=new ArrayList<String>();
131         list.add("KJ4.1-0127-0001");
132         list.add("KJ4.1-0128-0001");
133         list.add("KJ4.1-0129-0001");
134         list.add("KJ4.1-0130-0001");
135         if(list!=null && list.size()>0){
136             for(String message:list){
137                 JbarcodeUtil.createBarcode(message, new File("D:\\"+message+".png"),"测试");
138             }
139         }
140 
141     }
142 }

注意事项:

1.//设置条形码高度
    private static final int BARCODE_HEIGHT = 20;

  //设置条形码字体大小
  private static final int FONT_SIZE = 15;

这2个设置大小很重要,若是设置值小了则看不到文件如“苏薇”,自己可以把值修改为12运行下会发现文本"苏微"看不到,这是由于高度太小,字体无法显示

2.生成的条形码用扫码枪可以扫描,但是有时候扫描不了,原因是生成的条形码密度太厚,故"jbc.setXDimension(Double.valueOf(0.8).doubleValue());"设置很重要,值越小密度越细,条形码宽度越宽。

3.案例中message="KJ4.1-0130-0001",若message="KJ4.1-0130-0001(001)"则扫描不了,原因识别不了括号

4.该案例生成的条形码扫描反应慢

猜你喜欢

转载自www.cnblogs.com/zsh-blogs/p/10504577.html