依赖
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.4.0</version>
</dependency>
utils
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.oned.Code128Writer;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;
public class BarCodeUtil{
private static final int TOP = 10;
private static final int WIDTH = 300;
private static final int HEIGHT = 75;
private static Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>() {
private static final long serialVersionUID = 1L;
{
put(EncodeHintType.CHARACTER_SET, "utf-8");
}
};
public static BufferedImage createBarCode(String code) throws WriterException {
return createBarCode(code, WIDTH, HEIGHT,false);
}
public static BufferedImage createBarCode(String code,boolean showCode) throws WriterException {
return createBarCode(code, WIDTH, HEIGHT,showCode);
}
public static BufferedImage createBarCode(String code,int width,int height) throws WriterException {
return createBarCode(code, width, height,false);
}
public static BufferedImage createBarCode(String code,int width,int height,boolean showCode) throws WriterException {
Code128Writer writer = new Code128Writer();
BitMatrix bitMatrix = writer.encode(code, BarcodeFormat.CODE_128, width, height, hints);
BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);
if (showCode) {
BufferedImage outImage = new BufferedImage(width, height+35, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = outImage.createGraphics();
setGraphics2D(g2d);
setColorWhite(g2d);
g2d.drawImage(image, 0, TOP, image.getWidth(), image.getHeight(), null);
Color color=new Color(0, 0, 0);
g2d.setColor(color);
g2d.setFont(new Font("微软雅黑", Font.PLAIN, 18));
FontMetrics fontMetrics = g2d.getFontMetrics();
int strWidth = fontMetrics.stringWidth(code);
int wordStartX=(width - strWidth) / 2;
int wordStartY=height+TOP+20;
g2d.drawString(code, wordStartX, wordStartY);
g2d.dispose();
outImage.flush();
return outImage;
}
return image;
}
public static void createBarCodeStream(String code,OutputStream outputStream) throws IOException, WriterException {
createBarCodeStream(code,false,outputStream);
}
public static void createBarCodeStream(String code,boolean showCode,OutputStream outputStream) throws IOException, WriterException {
createBarCodeStream(code,WIDTH,HEIGHT,showCode,outputStream);
}
public static void createBarCodeStream(String code, int width, int height,OutputStream outputStream) throws IOException, WriterException {
createBarCodeStream(code,width,height,false,outputStream);
}
public static void createBarCodeStream(String code, int width, int height, boolean showCode,OutputStream outputStream) throws IOException, WriterException {
BufferedImage image = createBarCode(code,width,height,showCode);
ImageIO.write(image, "png", outputStream);
}
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){
g2d.setColor(Color.WHITE);
g2d.fillRect(0,0,600,600);
g2d.setColor(Color.BLACK);
}
public static void main(String[] args) throws IOException, WriterException {
File file = new File("E:\\test.png");
FileOutputStream fileOutputStream = new FileOutputStream(file);
createBarCodeStream("abd123123",true,fileOutputStream);
}
}