最近在工作中遇到生成条形码的需求,于是在一番搜索之后找到ZXing这个工具。(生成的结果如下图)

首先需要引入依赖:

        <!--zxing工具-->
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.4.0</version>
        </dependency>
        <!--zxing工具之javase-->
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.4.0</version>
        </dependency>

下面是工具类:

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 com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.yunchang.springboot.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author super man
 * @date 2020/9/16 0016  16:10
 * @description: 条形码生成工具
 */
public class GoogleBarCodeUtils {

    /**
     * 条形码宽度
     */
    private static final int WIDTH = 300;

    /**
     * 条形码高度
     */
    private static final int HEIGHT = 52;

    /**
     * 加文字 条形码
     */
    private static final int WORD_HEIGHT = 110;

    /**
     * 条形码右下角第一段数据
     */
    private static final String RIGHT_FIRST_WORDS = "superMan";

    private static final String RIGHT_SECOND_WORDS = "Made in China";

    /**
     * 设置 条形码参数
     */
    private static Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>() {
        private static final long serialVersionUID = 1L;

        {
            // 设置编码方式
            put(EncodeHintType.CHARACTER_SET, "utf-8");
            // 容错级别 这里选择最高H级别
            put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            put(EncodeHintType.MARGIN, 1);
        }
    };

    /**
     * 生成 图片缓冲
     *
     * @param vaNumber VA 码
     * @return 返回BufferedImage
     * @author fxbin
     */
    public static BufferedImage getBarCode(String vaNumber) {
        try {
            Code128Writer writer = new Code128Writer();
            // 编码内容, 编码类型, 宽度, 高度, 设置参数
            BitMatrix bitMatrix = writer.encode(vaNumber, BarcodeFormat.CODE_128, WIDTH, HEIGHT, hints);
            return MatrixToImageWriter.toBufferedImage(bitMatrix);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 把带logo的二维码下面加上文字
     *
     * @param image 条形码图片
     * @param words 文字
     * @return 返回BufferedImage
     * @author fxbin
     */
    public static BufferedImage insertWords(BufferedImage image, String words, String time) {
        // 新的图片,把带logo的二维码下面加上文字
        if (StringUtils.isNotEmpty(words)) {
            BufferedImage outImage = new BufferedImage(WIDTH, WORD_HEIGHT, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = outImage.createGraphics();
            // 抗锯齿
            setGraphics2D(g2d);
            // 设置白色
            setColorWhite(g2d);
            // 画条形码到新的面板
            g2d.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
            // 画文字到新的面板
            Color color = new Color(0, 0, 0);
            g2d.setColor(color);
            // 字体、字型、字号
            g2d.setFont(new Font("微软雅黑", Font.PLAIN, 16));
            //文字长度
            int strWidth = g2d.getFontMetrics().stringWidth(words);
            //总长度减去文字长度的一半  (居中显示)
            int wordStartX = (WIDTH - strWidth) / 2;
            //height + (outImage.getHeight() - height) / 2 + 12
            int wordStartY = HEIGHT + 20;
            // time 文字长度
            int timeWidth = 1;
            // rightFirstWords 文字长度
            int rightFirstWordsWidth = WIDTH - g2d.getFontMetrics().stringWidth(RIGHT_FIRST_WORDS);
            // rightSecondWords 文字长度
            int rightSecondWordsWidth = WIDTH - g2d.getFontMetrics().stringWidth(RIGHT_SECOND_WORDS);
            // 画文字
            g2d.drawString(words, wordStartX, wordStartY);
            g2d.drawString(time, timeWidth, wordStartY + 18);
            g2d.drawString(RIGHT_FIRST_WORDS, rightFirstWordsWidth, wordStartY + 18);
            g2d.drawString(RIGHT_SECOND_WORDS, rightSecondWordsWidth, wordStartY + 36);
            g2d.dispose();
            outImage.flush();
            return outImage;
        }
        return null;
    }

    /**
     * 设置 Graphics2D 属性  (抗锯齿)
     *
     * @param g2d Graphics2D提供对几何形状、坐标转换、颜色管理和文本布局更为复杂的控制
     */
    private static void setGraphics2D(Graphics2D g2d) {
        // 消除画图锯齿
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        // 消除文字锯齿
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_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, 302, 113);
        //设置笔刷
        g2d.setColor(Color.BLACK);
    }

    public static void main(String[] args) throws IOException {
        BufferedImage image = insertWords(getBarCode("superMan"), "superMan", "2020-09-16");
        ImageIO.write(image, "jpg", new File("D:/superMan.png"));
    }

}

web接口生成代码如下:

    @ApiOperation(value = "条形码图片")
    @PostMapping("generateBarCode")
    public void generateBarCodeByFnSku(
            @RequestParam(value = "superMan") String superMan,
            HttpServletResponse response) throws IOException {
        BufferedImage image = insertWords(getBarCode(superMan), superMan, DateUtil.getTodayDate("yyyy-MM-dd"));
        ServletOutputStream os = response.getOutputStream();
        ImageIO.write(image, "PNG", os);
        os.flush();
        os.close();
    }

若还有疑问,欢迎留言讨论!

----------------------------------------------------------------------------2020-11-19--------------------------------------------------------------------------------------

回复留言中将文字放在条形码上方问题:

其实画这个条形码你可以想象你有一个画板,条形码和文字都是你要画的内容。

现在如果要把文字放在条形码上方就只需要将条形码和文字的位置调整一下就行。代码如下:

    public static BufferedImage insertWords(BufferedImage image, String words, String time, Integer type) {
        // 新的图片,把带logo的二维码下面加上文字
        if (StringUtils.isNotEmpty(words)) {
            BufferedImage outImage = new BufferedImage(WIDTH, WORD_HEIGHT, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = outImage.createGraphics();
            // 抗锯齿
            setGraphics2D(g2d);
            // 设置白色
            setColorWhite(g2d, type);
            // 画条形码到新的面板
            g2d.drawImage(image, 0, 20, image.getWidth(), image.getHeight(), null);
            // 画文字到新的面板
            Color color = new Color(0, 0, 0);
            g2d.setColor(color);
            // 字体、字型、字号
            g2d.setFont(new Font("微软雅黑", Font.PLAIN, 16));
            //文字长度
            int strWidth = g2d.getFontMetrics().stringWidth(words);
            //总长度减去文字长度的一半  (居中显示)
            int wordStartX = (WIDTH - strWidth) / 2;
            //height + (outImage.getHeight() - height) / 2 + 12
            int wordStartY = HEIGHT + 20;
            // time 文字长度
            int timeWidth = 1;
            // rightFirstWords 文字长度
            int rightFirstWordsWidth = WIDTH - g2d.getFontMetrics().stringWidth(RIGHT_FIRST_WORDS);
            // rightSecondWords 文字长度
            int rightSecondWordsWidth = WIDTH - g2d.getFontMetrics().stringWidth(RIGHT_SECOND_WORDS);
            // 画文字
            g2d.drawString(words, wordStartX, 15);
            g2d.drawString(time, timeWidth, wordStartY + 18);
            g2d.drawString(RIGHT_FIRST_WORDS, rightFirstWordsWidth, wordStartY + 18);
            g2d.drawString(RIGHT_SECOND_WORDS, rightSecondWordsWidth, wordStartY + 36);
            g2d.dispose();
            outImage.flush();
            return outImage;
        }
        return null;
    }

结果如图:

GitHub 加速计划 / zx / zxing
32.53 K
9.31 K
下载
ZXing ("Zebra Crossing") barcode scanning library for Java, Android
最近提交(Master分支:2 个月前 )
8944e607 3 个月前
6ea3726b Bumps `spring.version` from 6.1.5 to 6.1.8. Updates `org.springframework:spring-test` from 6.1.5 to 6.1.8 - [Release notes](https://github.com/spring-projects/spring-framework/releases) - [Commits](https://github.com/spring-projects/spring-framework/compare/v6.1.5...v6.1.8) Updates `org.springframework:spring-web` from 6.1.5 to 6.1.8 - [Release notes](https://github.com/spring-projects/spring-framework/releases) - [Commits](https://github.com/spring-projects/spring-framework/compare/v6.1.5...v6.1.8) --- updated-dependencies: - dependency-name: org.springframework:spring-test dependency-type: direct:development update-type: version-update:semver-patch - dependency-name: org.springframework:spring-web dependency-type: direct:development update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Sean Owen <srowen@gmail.com> 3 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐