package com.util;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.jbarcode.JBarcode;
import org.jbarcode.JBarcodeFactory;
import org.jbarcode.paint.TextPainter;
import org.jbarcode.util.ImageUtil;
public class BarCode {
private static final int BARCODE_HEIGHT = 24;
private static final int BARCODE_DPI = ImageUtil.DEFAULT_DPI;
private static final String FONT_FAMILY = "consola";
private static final int FONT_SIZE = 12;
private static JBarcode jbc = null;
static JBarcode getJBarcode() {
if (jbc == null) {
jbc = JBarcodeFactory.getInstance().createCode128();
jbc.setTextPainter(CustomTextPainter.getInstance());
jbc.setBarHeight(BARCODE_HEIGHT);
}
return jbc;
}
public static void createBarcode(String message, File file) {
try {
FileOutputStream fos = new FileOutputStream(file);
createBarcode(message, fos);
fos.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static void createBarcode(String message, OutputStream os) {
try {
BufferedImage image = getJBarcode().createBarcode(message);
ImageUtil.encodeAndWrite(image, ImageUtil.PNG, os, BARCODE_DPI, BARCODE_DPI);
os.flush();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 自定义的 TextPainter, 允许定义字体,大小等等。
*/
static class CustomTextPainter implements TextPainter {
public static CustomTextPainter instance = new CustomTextPainter();
public static CustomTextPainter getInstance() {
return instance;
}
@Override
public void paintText(BufferedImage barCodeImage, String text, int width) {
Graphics g2d = barCodeImage.getGraphics();
Font font = new Font(FONT_FAMILY, Font.PLAIN, FONT_SIZE * width);
g2d.setFont(font);
FontMetrics fm = g2d.getFontMetrics();
int height = fm.getHeight();
int center = (barCodeImage.getWidth() - fm.stringWidth(text)) / 2;
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, barCodeImage.getWidth(), barCodeImage.getHeight() * 1 / 20);
g2d.fillRect(0, barCodeImage.getHeight() - (height * 9 / 10), barCodeImage.getWidth(), (height * 9 / 10));
g2d.setColor(Color.BLACK);
g2d.drawString(text, center, barCodeImage.getHeight() - (height / 10) - 2);
}
}
public static void main(String[] args) {
String imgRootPath="C:/";
String imgRootURL="D:/";
SimpleDateFormat format=new SimpleDateFormat("yyyyMMddHHmmssSSS");
String timenow=format.format(new Date());
String imgFolder=imgRootPath+"/upload/img/barcode/";
String imgPath=imgRootPath+"/upload/img/barcode/"+timenow+".png";
String imgURL=imgRootURL+"/upload/img/barcode/"+timenow+".png";
System.out.println(imgPath);
System.out.println(imgURL);
new File(imgFolder).mkdirs();
BarCode.createBarcode(timenow, new File(imgPath));
}
}
依赖jar包: jbarcode-0.2.8.jar
条形码(一维码)工具类
最新推荐文章于 2024-03-08 14:12:42 发布