/**
* 静态内部类 * 自定义的 TextPainter, 允许定义字体,大小,文本等 * 参考底层实现:BaseLineTextPainter.getInstance() */
protected static class CustomTextPainter implements TextPainter {
private static CustomTextPainter instance =new CustomTextPainter();
public static CustomTextPainter getInstance() {
return instance;
}
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, 0, 145); //绘制条形码
g2d.drawString(text, center, barCodeImage.getHeight() - (height / 10) - 2);
}
}
//测试
public static void main(String[] args) throws FileNotFoundException, IOException {
List list=new ArrayList();
list.add("KJ4.1-0127-0001");
list.add("KJ4.1-0128-0001");
list.add("KJ4.1-0129-0001");
list.add("KJ4.1-0130-0001");
if(list!=null && list.size()>0){
for(String message:list){
JbarcodeUtil.createBarcode(message, new File("D:\\codeImg\\"+message+".png"),"苏交科");
}
}
}
}