一、准备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.该案例生成的条形码扫描反应慢

Jbarcode 条形码生成工具的更多相关文章

  1. Java条形码生成技术-Barcode4j

    背景 目前二维码的应用场景已经遍布各类互联网平台,通常是将产品/商品的唯一编号存储于二维码中以做扫码识别. 而用于生产环境的条形码技术仍然存在,如硬件设备制造.供应.物流运输等等. 在常见的产品信息管 ...

  2. Java 条形码生成(一维条形码)

    utl:http://mianhuaman.iteye.com/blog/1013945 在这里给大家介绍一个java 生成条形码 jbarcode.jar 生成条形码 支持EAN13, EAN8, ...

  3. 【C#附源码】数据库文档生成工具支持(Excel+Html)

    [2015] 很多时候,我们在生成数据库文档时,使用某些工具,可效果总不理想,不是内容不详细,就是表现效果一般般.很多还是word.html的.看着真是别扭.本人习惯用Excel,所以闲暇时,就简单的 ...

  4. 微软开源全新的文档生成工具DocFX

    微软放弃Sandcastle有些年头了,微软最近开源了全新的文档生成工具DocFX,目前支持C#和VB,类似JSDoc或Sphinx,可以从源代码中提取注释生成文档之外,而且还有语法支持你加入其他的文 ...

  5. css sprite,css雪碧图生成工具V3.0更新

    V3.0主要改进 1.增加了单独添加单张图片以及删除单张图片的功能 2.增加了生成.sprite文件用以保存雪碧图信息 3.增加了打开.sprite文件功能 什么是css sprite CSS spr ...

  6. DBImport v3.44 中文版发布:数据库数据互导及文档生成工具(IT人员必备)

    前言: 距离上一个版本V3.3版本的文章发布,已经是1年10个月前的事了. 其实版本一直在更新,但也没什么大的功能更新,总体比较稳定,所以也不怎么写文介绍了. 至于工作上的事,之前有半年时间跑去学英语 ...

  7. C/C++ makefile自动生成工具(comake2,autotools,linux),希望能为开源做点微薄的贡献!

      序     在linux下C或C++项目开发,Makefile是必备的力气,但是发现手写很麻烦. 在百度有个comake2工具,用于自动生成Makefile工具,而在外边本想找一个同类工具,但发现 ...

  8. .NET平台开源项目速览(4).NET文档生成工具ADB及使用

    很久以前就使用ADB这个工具来生成项目的帮助文档.功能强大,在学习一些开源项目的过程中,官方没有提供CHM帮助文档,所以为了快速的了解项目结构和注释.就生成文档来自己看,非常好用.这也是一个学习方法吧 ...

  9. Linux下三个密码生成工具

    http://code.csdn.net/news/2820879 想出一个难破解且容易记的密码对不是一件简单的事情.在我为电脑设定一个新密码,或者在线注册了一个新的账号,需要输入密码的时候,脑袋就一 ...

随机推荐

  1. C++ new运算符

    new 分配的数据类型:内置数据类型.自定义数据类型. 如果不成功,则 new 将返回零或引发异常:编写自定义异常处理例程并调用 _set_new_handler运行库函数(以您的函数名称作为其参数) ...

  2. JavaScript原型继承的实例

    // 创建构造函数实例(获取DOM节点) <div id="app">测试字符</div>

  3. html常用文本标签(转)

    内容一<br />内容二 让文本强制换行 内容一内容二 <p>段落一</p><p>段落二</p> 段落标签 段落一 段落二 <b> ...

  4. springboot之session、cookie

    1-  获取session的方案 session:  https://blog.csdn.net/yiifaa/article/details/77542208 2-  session什么时候创建? ...

  5. DeepNetwork---tensorflow实现

    https://github.com/zle1992/Reinforcement_Learning_Game DeepQNetwork.py import numpy as np import ten ...

  6. Block 循环引用(中)

    不会造成循环引用的block 大部分GCD方法 dispatch_async(dispatch_get_main_queue(), ^{ [self doSomething]; }); 因为self并 ...

  7. 解决 samba 服务器 windows 多重连接

    samba连接,用户名密码均正确.失败提示:不允许一个用户使用一个以上用户名与一个服务器或共享资源的多重连接. 事实上,这个不是samba的限制.是Windows的限制. 在打开存在public = ...

  8. python os.path.dirname()

    ----返回文件所在的路径 ----如果path变量直接是文件名则返回空

  9. 设计模式之Chain of Responsibility(职责链)(转)

    Chain of Responsibility定义 Chain of Responsibility(CoR) 是用一系列类(classes)试图处理一个请求request,这些类之间是一个松散的耦合, ...

  10. WIN7系统怎样增加C盘空间

    具体操作参考:https://jingyan.baidu.com/article/215817f78e05c01eda142385.html