报销单需要根据单号生成条形码
先看效果图
直接上代码,复制即可使用
public class BarCodeUtils {
private static final int DEFAULT_PICTURE_WIDTH = 300;
private static final int DEFAULT_PICTURE_HEIGHT = 100;
private static final int DEFAULT_BAR_CODE_WIDTH = 280;
private static final int DEFAULT_BAR_CODE_HEIGHT = 60;
private static final int DEFAULT_FONT_SIZE = 15;
private static final Map<EncodeHintType, Object> hints = new HashMap<>();
static {
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
}
public static BufferedImage getBarCodeImage(String codeValue) {
return getBarCodeImage(codeValue, DEFAULT_BAR_CODE_WIDTH, DEFAULT_BAR_CODE_HEIGHT);
}
public static BufferedImage getBarCodeImage(String codeValue, int width, int height) {
return getBarCodeImage(codeValue, width, height, BarcodeFormat.CODE_128);
}
public static BufferedImage getBarCodeImage(String codeValue, int width, int height, BarcodeFormat barcodeFormat) {
Writer writer;
switch (barcodeFormat) {
case CODE_128:
writer = new Code128Writer();
break;
case PDF_417:
writer = new PDF417Writer();
break;
default:
writer = new Code128Writer();
}
BitMatrix bitMatrix;
try {
bitMatrix = writer.encode(codeValue, barcodeFormat, width, height, hints);
} catch (WriterException e) {
throw new RuntimeException("条形码内容写入失败");
}
return MatrixToImageWriter.toBufferedImage(bitMatrix);
}
public static BufferedImage getBarCodeWithWords(String codeValue, String bottomStr) {
return getBarCodeWithWords(codeValue, bottomStr, "", "");
}
public static BufferedImage getBarCodeWithWords(String codeValue,
String bottomStr,
String topLeftStr,
String topRightStr) {
return getCodeWithWords(getBarCodeImage(codeValue),
bottomStr,
topLeftStr,
topRightStr,
DEFAULT_PICTURE_WIDTH,
DEFAULT_PICTURE_HEIGHT,
0,
0,
0,
0,
0,
0,
DEFAULT_FONT_SIZE);
}
public static BufferedImage getCodeWithWords(BufferedImage codeImage,
String bottomStr,
String topLeftStr,
String topRightStr,
int pictureWidth,
int pictureHeight,
int codeOffsetX,
int codeOffsetY,
int topLeftOffsetX,
int topLeftOffsetY,
int topRightOffsetX,
int topRightOffsetY,
int fontSize) {
BufferedImage picImage = new BufferedImage(pictureWidth, pictureHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = picImage.createGraphics();
setGraphics2D(g2d);
setColorWhite(g2d, picImage.getWidth(), picImage.getHeight());
int codeStartX = (pictureWidth - codeImage.getWidth()) / 2 + codeOffsetX;
int codeStartY = (pictureHeight - codeImage.getHeight()) / 2 + codeOffsetY;
g2d.drawImage(codeImage, codeStartX, codeStartY, codeImage.getWidth(), codeImage.getHeight(), null);
g2d.setColor(Color.BLACK);
g2d.setFont(new Font("微软雅黑", Font.PLAIN, fontSize));
int wordAndCodeSpacing = 3;
if (StringUtils.isNotEmpty(bottomStr)) {
int strWidth = g2d.getFontMetrics().stringWidth(bottomStr);
int strStartX = codeStartX + (codeImage.getWidth() - strWidth) / 2;
int strStartY = codeStartY + codeImage.getHeight() + fontSize + wordAndCodeSpacing;
g2d.drawString(bottomStr, strStartX, strStartY);
}
if (StringUtils.isNotEmpty(topLeftStr)) {
int strWidth = g2d.getFontMetrics().stringWidth(topLeftStr);
int strStartX = codeStartX + topLeftOffsetX;
int strStartY = codeStartY + topLeftOffsetY - wordAndCodeSpacing;
g2d.drawString(topLeftStr, strStartX, strStartY);
}
if (StringUtils.isNotEmpty(topRightStr)) {
int strWidth = g2d.getFontMetrics().stringWidth(topRightStr);
int strStartX = codeStartX + codeImage.getWidth() - strWidth + topRightOffsetX;
int strStartY = codeStartY + topRightOffsetY - wordAndCodeSpacing;
g2d.drawString(topRightStr, strStartX, strStartY);
}
g2d.dispose();
picImage.flush();
return picImage;
}
private static void setGraphics2D(Graphics2D g2d) {
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
Stroke s = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER);
g2d.setStroke(s);
}
private static void setColorWhite(Graphics2D g2d, int width, int height) {
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, width, height);
g2d.setColor(Color.BLACK);
}
public static String bufferedImageToBase64(BufferedImage bufferedImage) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
ImageIO.write(bufferedImage, "jpg", stream);
} catch (Exception e) {
e.printStackTrace();
}
byte[] bytes = Base64.encodeBase64(stream.toByteArray());
String base64 = new String(bytes);
return "data:image/jpeg;base64," + base64;
}
public static String generateBarCode(String content) {
return bufferedImageToBase64(BarCodeUtils.getBarCodeWithWords(content, content, "", ""));
}
public static void main(String[] args) {
String s = generateBarCode("0123456789");
System.out.println("条形码:" + s);
}
}