package com.springmvc.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLDecoder;
import org.jbarcode.JBarcode;
import org.jbarcode.JBarcodeFactory;
import org.jbarcode.encode.Code128Encoder;
import org.jbarcode.encode.InvalidAtributeException;
import org.jbarcode.paint.TextPainter;
import org.jbarcode.util.ImageUtil;
public class JbarcodeUtil {
// 设置条形码高度
private static final int BARCODE_HEIGHT = 20;
// 设置条形码默认分辨率
private static final int BARCODE_DPI = ImageUtil.DEFAULT_DPI;
// 设置条形码字体样式
private static final String FONT_FAMILY = "新宋体";
// 设置条形码字体大小
private static final int FONT_SIZE = 15;
// 设置条形码文本
public static String TEXT = "";
// 创建jbarcode
private static JBarcode jbc = null;
// 生成条形码最下面一行;
private static String name = "";
static JBarcode getJBarcode() throws InvalidAtributeException {
/**
* 参考设置样式: barcode.setEncoder(Code128Encoder.getInstance()); //设置编码
* barcode.setPainter(WidthCodedPainter.getInstance());// 设置Painter
* barcode.setTextPainter(BaseLineTextPainter.getInstance()); //设置TextPainter
* barcode.setBarHeight(17); //设置高度
* barcode.setWideRatio(Double.valueOf(30).doubleValue());// 设置宽度比率
* barcode.setXDimension(Double.valueOf(2).doubleValue()); // 设置尺寸,大小 密集程度
* barcode.setShowText(true); //是否显示文本 barcode.setCheckDigit(true); //是否检查数字
* barcode.setShowCheckDigit(false); //是否显示检查数字
*/
if (jbc == null) {
// 生成code128
jbc = JBarcodeFactory.getInstance().createCode128();
jbc.setEncoder(Code128Encoder.getInstance());
// jbc.setTextPainter(CustomTextPainter.getInstance());
jbc.setTextPainter(CustomTextPainter.getInstance());
// jbc.setWideRatio(Double.valueOf(1).doubleValue());
// jbc.setXDimension(Double.valueOf(1).doubleValue());
jbc.setBarHeight(BARCODE_HEIGHT);
// jbc.setXDimension(1);
jbc.setShowText(true);
}
return jbc;
}
/**
* @descript:生成条形码文件
* @param message 条形码内容
* @param file 生成文件
*/
public static void createBarcode(String message, File file, String text) {
try {
FileOutputStream fos = new FileOutputStream(file);
createBarcode(message, fos, text);
fos.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* @descript:生成条形码并写入指定输出流
* @param message 条形码内容
* @param os 输出流
*/
public static void createBarcode(String message, OutputStream os, String text) {
try {
String a = message;
name = a;
// 设置条形码文本
TEXT = URLDecoder.decode(text, "utf-8");
// 创建条形码的BufferedImage图像
BufferedImage image = getJBarcode().createBarcode(a);
ImageUtil.encodeAndWrite(image, ImageUtil.JPEG, os, 10, 0);
os.flush();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 静态内部类 自定义的 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) {
// 绘图
// BufferedImage bi = barCodeImage;
Graphics g2d = barCodeImage.getGraphics();
// 创建字体
Font font = new Font("防宋", Font.PLAIN, 12);
g2d.setFont(font);
FontMetrics fm = g2d.getFontMetrics();
g2d.setColor(Color.WHITE); // 143 30
g2d.fillRect(0, 0, 200, 30);
g2d.fillRect(0, 55, 200, 111);
// g2d.fillRect(0, 0, 200, barCodeImage.getHeight() * 1 / 20);
// g2d.fillRect(0, barCodeImage.getHeight() - (height * 9 / 10),
// barCodeImage.getWidth(), (height * 9 / 10));
// 55 143 111
g2d.setColor(Color.BLACK);
String a = TEXT;
// 绘制文本
g2d.drawString(a, 35, 20);
// 绘制条形码
g2d.drawString(name, 20, 70);
}
}
}