背景
目前二维码的应用场景已经遍布各类互联网平台,通常是将产品/商品的唯一编号存储于二维码中以做扫码识别。

而用于生产环境的条形码技术仍然存在,如硬件设备制造、供应、物流运输等等。

在常见的产品信息管理、物料订单系统中,存在多个生成及打印条形码(一维码)的需求场景。

解决方案
Java生成条形码的方案 – barcode4j、zxing

barcode4j

barcode4j开源Java条形码生成库。支持多种编码格式,比如:code-39,code-128等

Welcome to Barcode4J

是由google开源的1D/2D编解码类库。目标是能够对QR编码、Data Matrix、UPC的1D条形码进行解码。 其提供了多种平台下的客户端包括:J2ME、J2SE和Android

本次采用了barcode4j作为解决方案

1.引入pom依赖包

 <dependency><groupId>net.sf.barcode4j</groupId><artifactId>barcode4j-light</artifactId><version>2.0</version></dependency>

2.代码块

import lombok.extern.slf4j.Slf4j;
import org.krysalis.barcode4j.HumanReadablePlacement;
import org.krysalis.barcode4j.impl.AbstractBarcodeBean;
import org.krysalis.barcode4j.impl.codabar.CodabarBean;
import org.krysalis.barcode4j.impl.code128.Code128Bean;
import org.krysalis.barcode4j.impl.code39.Code39Bean;
import org.krysalis.barcode4j.impl.datamatrix.DataMatrixBean;
import org.krysalis.barcode4j.impl.fourstate.RoyalMailCBCBean;
import org.krysalis.barcode4j.impl.fourstate.USPSIntelligentMailBean;
import org.krysalis.barcode4j.impl.int2of5.Interleaved2Of5Bean;
import org.krysalis.barcode4j.impl.pdf417.PDF417Bean;
import org.krysalis.barcode4j.impl.postnet.POSTNETBean;
import org.krysalis.barcode4j.impl.upcean.EAN13Bean;
import org.krysalis.barcode4j.impl.upcean.EAN8Bean;
import org.krysalis.barcode4j.impl.upcean.UPCABean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.krysalis.barcode4j.tools.UnitConv;
import org.springframework.util.StringUtils;import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;/*** @version 1.0.0* @description: 条形码工具类* @date 2022/11/24 11:15*/
@Slf4j
public class BarcodeUtil {/*                <dependency><groupId>net.sf.barcode4j</groupId><artifactId>barcode4j-light</artifactId><version>2.0</version></dependency>*//*** 生成code39条形码** @param message       要生成的文本* @param fontSize      条形码字体大小* @param height        条形码的高度* @param width         条形码的宽度* @param withQuietZone 是否两边留白* @param quietZone     withQuietZone为true可用 留白宽度* @return*/public static byte[] generateBarCode39(String message, Double fontSize, Double height, Double width, boolean withQuietZone, Double quietZone) {if (StringUtils.isEmpty(message)) {return null;}return generateBar(new Code39Bean(), message, fontSize, height, width, withQuietZone, quietZone);}/*** 生成code128条形码** @param message       要生成的文本* @param fontSize      条形码字体大小* @param height        条形码的高度* @param width         条形码的宽度* @param withQuietZone 是否两边留白* @param quietZone     withQuietZone为true可用 留白宽度* @return*/public static byte[] generateBarCode128(String message, Double fontSize, Double height, Double width, boolean withQuietZone, Double quietZone) {if (StringUtils.isEmpty(message)) {return null;}return generateBar(new Code128Bean(), message, fontSize, height, width, withQuietZone, quietZone);}/*** 生成EAN13条形码** @param message       要生成的文本  Valid are 0-9 only length: 11-12* @param fontSize      条形码字体大小* @param height        条形码的高度* @param width         条形码的宽度* @param withQuietZone 是否两边留白* @param quietZone     withQuietZone为true可用 留白宽度* @return*/public static byte[] generateBarEAN13(String message, Double fontSize, Double height, Double width, boolean withQuietZone, Double quietZone) {if (StringUtils.isEmpty(message)) {return null;}return generateBar(new EAN13Bean(), message, fontSize, height, width, withQuietZone, quietZone);}/*** 生成EAN8条形码** @param message       要生成的文本 Valid are 0-9 only length: 7-8* @param fontSize      条形码字体大小* @param height        条形码的高度* @param width         条形码的宽度* @param withQuietZone 是否两边留白* @param quietZone     withQuietZone为true可用 留白宽度* @return*/public static byte[] generateBarEAN8(String message, Double fontSize, Double height, Double width, boolean withQuietZone, Double quietZone) {if (StringUtils.isEmpty(message)) {return null;}return generateBar(new EAN8Bean(), message, fontSize, height, width, withQuietZone, quietZone);}/*** 生成Codabar条形码** @param message       要生成的文本 Valid are 0-9 and a-e* @param fontSize      条形码字体大小* @param height        条形码的高度* @param width         条形码的宽度* @param withQuietZone 是否两边留白* @param quietZone     withQuietZone为true可用 留白宽度* @return*/public static byte[] generateBarCodabar(String message, Double fontSize, Double height, Double width, boolean withQuietZone, Double quietZone) {if (StringUtils.isEmpty(message)) {return null;}return generateBar(new CodabarBean(), message, fontSize, height, width, withQuietZone, quietZone);}/*** 生成UPCA条形码** @param message       要生成的文本 Valid are 0-9 lenth:11-12* @param fontSize      条形码字体大小* @param height        条形码的高度* @param width         条形码的宽度* @param withQuietZone 是否两边留白* @param quietZone     withQuietZone为true可用 留白宽度* @return*/public static byte[] generateBarUPCA(String message, Double fontSize, Double height, Double width, boolean withQuietZone, Double quietZone) {if (StringUtils.isEmpty(message)) {return null;}return generateBar(new UPCABean(), message, fontSize, height, width, withQuietZone, quietZone);}/*** 生成POSTNET条形码** @param message       要生成的文本 Valid are 0-9* @param fontSize      条形码字体大小* @param height        条形码的高度* @param width         条形码的宽度* @param withQuietZone 是否两边留白* @param quietZone     withQuietZone为true可用 留白宽度* @return*/public static byte[] generateBarPOSTNET(String message, Double fontSize, Double height, Double width, boolean withQuietZone, Double quietZone) {if (StringUtils.isEmpty(message)) {return null;}return generateBar(new POSTNETBean(), message, fontSize, height, width, withQuietZone, quietZone);}/*** 生成RoyalMailCBC条形码** @param message       要生成的文本 Valid are 0-9* @param fontSize      条形码字体大小* @param height        条形码的高度* @param width         条形码的宽度* @param withQuietZone 是否两边留白* @param quietZone     withQuietZone为true可用 留白宽度* @return*/public static byte[] generateBarRoyalMailCBC(String message, Double fontSize, Double height, Double width, boolean withQuietZone, Double quietZone) {if (StringUtils.isEmpty(message)) {return null;}return generateBar(new RoyalMailCBCBean(), message, fontSize, height, width, withQuietZone, quietZone);}/*** 生成USPSIntelligentMail条形码** @param message       要生成的文本 Valid are 0-9 length:20* @param fontSize      条形码字体大小* @param height        条形码的高度* @param width         条形码的宽度* @param withQuietZone 是否两边留白* @param quietZone     withQuietZone为true可用 留白宽度* @return*/public static byte[] generateBarUSPSIntelligentMail(String message, Double fontSize, Double height, Double width, boolean withQuietZone, Double quietZone) {if (StringUtils.isEmpty(message)) {return null;}return generateBar(new USPSIntelligentMailBean(), message, fontSize, height, width, withQuietZone, quietZone);}/*** 生成PDF417条形码** @param message       要生成的文本 Valid are 0-9 length:20* @param fontSize      条形码字体大小* @param height        条形码的高度* @param width         条形码的宽度* @param withQuietZone 是否两边留白* @param quietZone     withQuietZone为true可用 留白宽度* @return*/public static byte[] generateBarPDF417(String message, Double fontSize, Double height, Double width, boolean withQuietZone, Double quietZone) {if (StringUtils.isEmpty(message)) {return null;}return generateBar(new PDF417Bean(), message, fontSize, height, width, withQuietZone, quietZone);}/*** 生成DataMatrix条形码** @param message       要生成的文本 Valid are 0-9 length:20* @param fontSize      条形码字体大小* @param height        条形码的高度* @param width         条形码的宽度* @param withQuietZone 是否两边留白* @param quietZone     withQuietZone为true可用 留白宽度* @return*/public static byte[] generateBarDataMatrix(String message, Double fontSize, Double height, Double width, boolean withQuietZone, Double quietZone) {if (StringUtils.isEmpty(message)) {return null;}return generateBar(new DataMatrixBean(), message, fontSize, height, width, withQuietZone, quietZone);}/*** 生成Interleaved2Of5条形码** @param message       要生成的文本 Valid are 0-9* @param fontSize      条形码字体大小* @param height        条形码的高度* @param width         条形码的宽度* @param withQuietZone 是否两边留白* @param quietZone     withQuietZone为true可用 留白宽度* @return*/public static byte[] generateBarInterleaved2Of5(String message, Double fontSize, Double height, Double width, boolean withQuietZone, Double quietZone) {if (StringUtils.isEmpty(message)) {return null;}return generateBar(new Interleaved2Of5Bean(), message, fontSize, height, width, withQuietZone, quietZone);}/*** 生成条形码** @param bean          条形码类型* @param message       要生成的文本* @param fontSize      条形码字体大小* @param height        条形码的高度* @param width         条形码的宽度* @param withQuietZone 是否两边留白* @param quietZone     withQuietZone为true可用 留白宽度* @return*/private static byte[] generateBar(AbstractBarcodeBean bean, String message, Double fontSize, Double height, Double width, boolean withQuietZone, Double quietZone) {// 条码数据显示位置bean.setMsgPosition(HumanReadablePlacement.HRP_BOTTOM);if (fontSize != null) {bean.setFontSize(fontSize);} else {bean.setFontSize(5);}// 两端留白宽度if (quietZone != null) {bean.setQuietZone(quietZone);}// 精细度final int dpi = 256;// 设置条码每一条的宽度// UnitConv 是barcode4j 提供的单位转换的实体类,用于毫米mm,像素px,英寸in,点pt之间的转换// 设置条形码高度和宽度if (height != null) {bean.setBarHeight(height);} else {bean.setBarHeight(20);}if (width != null) {bean.setModuleWidth(UnitConv.in2mm(width / dpi));} else {bean.setModuleWidth(UnitConv.in2mm(5.0f / dpi));}// 设置两侧是否留白bean.doQuietZone(withQuietZone);String format = "image/png";ByteArrayOutputStream ous = null;byte[] imageByte;try {ous = new ByteArrayOutputStream();// 输出到流BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format, dpi,BufferedImage.TYPE_BYTE_BINARY, false, 0);// 生成条形码bean.generateBarcode(canvas, message);// 结束绘制canvas.finish();imageByte = ous.toByteArray();} catch (IOException e) {throw new RuntimeException(e);} finally {try {if (null != ous) {ous.close();}} catch (Exception e) {log.error("ByteArrayOutputStream流关闭失败,失败原因为:{{}}", e.getMessage());}}return imageByte;}public static void main(String[] args) {byte[] bytes = generateBarDataMatrix("123312", null, null, null, true, null);// byte[]转base64String base64Str = Base64.getEncoder().encodeToString(bytes);System.out.println(base64Str);}
}

参考地址
条形码的各种编码格式

Java条形码生成-Barcode4j相关推荐

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

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

  2. Java条形码生成(128c)

    需要jbarcode的jar包支持. 下载地址:https://sourceforge.net/projects/jbcode 注:部分同学使用eclipse时无法直接使用BASE64Encoder, ...

  3. Java 生成条形 二维码 Java 生成条形码 订单条形码 Java生成各种条形码 java条形码生成示例 java 生成条形 二维码

    1.加入Maven.或者Jar依赖 maven依赖, 如果不是Maven项目,则需要去 Maven中央仓库下载Jar <!-- 二维码工具--><dependency>< ...

  4. pdf文件生成及条形码生成

    1.导入依赖 <!-- pdf--><dependency><groupId>com.lowagie</groupId><artifactId&g ...

  5. 【java】生成13位条形码(Ean-13码)

    [java]生成13位条形码(Ean-13码) 题目: 生成13位条形码 Ean-13码规则:第十三位数字是前十二位数字经过计算得到的校验码. 例如:690123456789 计算其校验码的过程为: ...

  6. java条形码和二维码解析

    java条形码和二维码解析 条形码(一维码) 条形码(barcode) – 将宽度不等的多个黑条和空白,按照一定的编码规则排列,用以表达一组信息的图形标识符 – 通常代表一串数字/字母,每一位有特殊含 ...

  7. vb/java/c# 生成code128 条码/QR Code二维码 导出excel

    java/c#生成条形码/二维码图片,导出excel 会用到POI/NPOI,BarcodeLib.ZXing插件. code128 如果不使用插件,也可使用下面vb/c#代码生成含有校验的条码文本, ...

  8. 条形码生成工具类实现

    1.pom引用: <!-- 条形码工具类 --> <dependency><groupId>com.google.zxing</groupId>< ...

  9. 只需3个步骤,轻松解决程序员在Java中生成、扫描二维码难题

    条形码包含有关产品或公司的信息,以机器可读的形式直观地表示.条码广泛用于跟踪货物和库存管理.我们可以在 WPF 应用程序中轻松生成各种类型的条码.二维码广泛用于分享重要信息.对于不同的要求,您可能希望 ...

最新文章

  1. hdu1247 Hat’s Words
  2. spring cloud微服务分布式云架构 - 整合企业架构的技术点
  3. 【PC工具】微软OneNote使用笔记,onenote无法连接网络无法同步解决方法
  4. C#中的深复制和浅复制(在C#中克隆对象)
  5. pandas describe函数_SQL和Pandas同时掉到河里,你先救谁?
  6. centos7.5安装influxdb-1.7.8
  7. linux网络编程之posix 线程(三):posix 匿名信号量与互斥锁 示例生产者--消费者问题
  8. mysql数据库表的导入导出
  9. xp系统的WINS服务器设置,WindowsXP系统设置
  10. C++socket编程(七):7.1 http协议讲解,通过抓包和telnet分析
  11. Web开发中,使用表格来展示每个角色对应的权限
  12. 虚拟机开启Linux时出现“我以复制虚拟机”、“我已移动虚拟机”
  13. 将 varchar 值转换为数据类型为 int 的列时发生语法错误
  14. python--简易员工信息系统编写
  15. [贪心][区间dp]Zero-One Codeforces1733D1D2
  16. 区块链开发区块链架构与应用PPT
  17. 软件工程——软科中国大学专业排名
  18. x265-1.7版本-common/quant.cpp注释
  19. 初窥门径:认识C语言
  20. 程序人生丨如何体现测试工程师的价值

热门文章

  1. 音视频从入门到精通——FFmpeg之swr_convert音频重采样函数分析
  2. IDC机房网络系列视频
  3. html移动小图标,html5 实现可拖拽移动的悬浮图标
  4. 通过深度学习评估公共开放空间的利用率:以底特律河岸开放空间研究为例
  5. 百度APP“看听模式”:“AI主播”借道信息流全面落地?
  6. linux挂载windows共享目录报错,linux通过cifs挂载windows共享目录
  7. 计算机专业选修课怎么选比较好,你知道怎么选AP课程吗?附AP不同专业方向的选课建议...
  8. 网站服务器拥挤如何进去,教你一招:有效解决网络拥挤的办法!
  9. 盘点国有银行发行数字人民币现状:正全面加速落地
  10. 《ROS2机器人建模URDF》8.4控制移动机器人轮子运动