java实现条形码一维码JBarcode打印
因为公司最近得项目需要一个打印一维码的功能,百度了很多相关文章,自己重新整理了一下,生成一个工具类,可以直接实现生成一维码的功能,源代码如下:
开始前必须引入jbarcode包
org.jbarcode
jbarcode
0.2.8
工具类BarcodeUtils源代码如下:
public class BarcodeUtils {
// 设置条形码的高度
private static final int BARCODE_HEIGHT = 65;
// 设置条形码的分辨率
private static final int BARCODE_DPI = 300;
public static void createBarcode(String message, String fileName)
{
try
{
JBarcode localJBarcode = new JBarcode(EAN13Encoder.getInstance(), WidthCodedPainter.getInstance(), EAN13TextPainter.getInstance());
//生成. 欧洲商品条码(=European Article Number)
//这里我们用作图书条码
BufferedImage localBufferedImage = localJBarcode.createBarcode(message);
saveToGIF(localBufferedImage, fileName);
localJBarcode.setEncoder(Code39Encoder.getInstance());
localJBarcode.setPainter(WideRatioCodedPainter.getInstance());
localJBarcode.setTextPainter(BaseLineTextPainter.getInstance());
localJ

本文介绍了一种使用Java和jbarcode库生成一维条形码的方法。通过创建一个工具类BarcodeUtils,可以轻松地生成并保存条形码为GIF、JPEG或PNG格式的图片,适用于各种应用场景。
最低0.47元/天 解锁文章
294





