Java 条形码生成

依赖

<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");
        }
    };
    /**
     * 生成条形码图片
     * @param code 条形码值
     * @return 返回BufferedImage
     */
    public static BufferedImage createBarCode(String code) throws WriterException {
        return createBarCode(code, WIDTH, HEIGHT,false);
    }

    /**
     * 生成条形码图片
     * @param code 条形码值
     * @param showCode  是否显示条形码的值
     * @return 返回BufferedImage
     */
    public static BufferedImage createBarCode(String code,boolean showCode) throws WriterException {
        return createBarCode(code, WIDTH, HEIGHT,showCode);
    }

    /**
     * 生成条形码图片
     * @param code 条形码值
     * @param width  条形码宽度
     * @param width  条形码高度
     * @return 返回BufferedImage
     */
    public static BufferedImage createBarCode(String code,int width,int height) throws WriterException {
        return createBarCode(code, width, height,false);
    }

    /**
     * 生成条形码图片
     * @param code  条形码值
     * @param width  条形码宽度
     * @param width  条形码高度
     * @param showCode  是否显示条形码的值
     * @return 返回BufferedImage
     */
    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;
    }
    /**
     * 生成条形码图片
     * @param code  条形码值
     * @return 返回BufferedImage
     */
    public static void createBarCodeStream(String code,OutputStream outputStream) throws IOException, WriterException {
        createBarCodeStream(code,false,outputStream);
    }
    /**
     * 生成条形码图片
     * @param code  条形码值
     * @param showCode  是否显示条形码的值
     * @return 返回BufferedImage
     */
    public static void createBarCodeStream(String code,boolean showCode,OutputStream outputStream) throws IOException, WriterException {
        createBarCodeStream(code,WIDTH,HEIGHT,showCode,outputStream);
    }

    /**
     * 生成条形码图片
     * @param code  条形码值
     * @param width  条形码宽度
     * @param height  条形码高度
     * @return 返回BufferedImage
     */
    public static void createBarCodeStream(String code, int width, int height,OutputStream outputStream) throws IOException, WriterException {
        createBarCodeStream(code,width,height,false,outputStream);
    }


    /**
     * 生成条形码图片
     * @param code  条形码值
     * @param width  条形码宽度
     * @param height  条形码高度
     * @param showCode  是否显示条形码的值
     * @return 返回BufferedImage
     */
    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);
    }


    /**
     * 设置 Graphics2D 属性  (抗锯齿)
     * @param g2d  Graphics2D提供对几何形状、坐标转换、颜色管理和文本布局更为复杂的控制
     */
    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);
    }

    /**
     * 设置背景为白色
     * @param g2d Graphics2D提供对几何形状、坐标转换、颜色管理和文本布局更为复杂的控制
     */
    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 {
        //BufferedImage image = getBarCode("abc123123",300,100,true);
        //输出流
        File file = new File("E:\\test.png");
        FileOutputStream fileOutputStream = new FileOutputStream(file);
//        ImageIO.write(image, "png", fileOutputStream);

        createBarCodeStream("abd123123",true,fileOutputStream);

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值