Java生成与解析二维码的完整指南

 更新时间:2026年05月17日 10:49:21   作者:RainCity  
二维码是一种能够将大量信息编码在小空间的编码器,它是一种由黑白方块构成的图片,可以将文本、链接、图像等不同类型的信息编码到其中,下面我们就来看看如何通过Java实现生成与解析二维码吧

前言

二维码是一种能够将大量信息编码在小空间的编码器。它是一种由黑白方块构成的图片,可以将文本、链接、图像等不同类型的信息编码到其中。二维码已经广泛应用于不同的领域,如电子支付、电子门票、商品销售等,这种快捷、高效的信息处理方式已成为现代生活中不可或缺的一部分。

所需依赖

<!-- 二维码生成 -->
	<dependency>
	    <groupId>com.google.zxing</groupId>
	    <artifactId>core</artifactId>
	    <version>3.5.1</version>
	</dependency>
	<dependency>
	    <groupId>com.google.zxing</groupId>
	    <artifactId>javase</artifactId>
	    <version>3.5.1</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-mock -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-mock</artifactId>
	    <version>2.0.8</version>
	    <scope>compile</scope>
	</dependency>

主要代码

需要用到的一些常量

	/** 字符编码 */
	private static final String CHARSET = "utf-8";

	/** 图片格式 */
    private static final String FORMAT_NAME = "JPG";
    
    /** 二维码尺寸 */
    private static final Integer QRCODE_SIZE = 380;
    
    /** LOGO宽度 */
    private static final Integer WIDTH = 100;
    
    /** LOGO高度 */
    private static final Integer HEIGHT = 100;

生成二维码

	/**
     * 生成二维码,得到 MultipartFile--通用
     * @param jsonString 二维码内容
     * @param logoPath logo路径
     * @param bottomText 底部文字
     * @return {@link MultipartFile}
     * @ver v1.0.0
     */
    public static MultipartFile createQrCode(String jsonString, String logoPath, String bottomText) throws Exception {

        return initMultipartFile(jsonString, logoPath, bottomText);
    }

    /**
     * 生成二维码,得到 MultipartFile-有logo、无底部文字
     * @param jsonString 二维码内容
     * @param logoPath logo路径
     * @return {@link MultipartFile}
     * @ver v1.0.0
     */
    public static MultipartFile createQrCode(String jsonString, String logoPath) throws Exception {

        return initMultipartFile(jsonString, logoPath, null);
    }

    /**
     * 生成二维码,得到 MultipartFile-无logo、无底部文字
     * @param jsonString 二维码内容
     * @return {@link MultipartFile}
     * @ver v1.0.0
     */
    public static MultipartFile createQrCode(String jsonString) throws Exception {

        return initMultipartFile(jsonString, null, null);
    }

    /**
     * 生成二维码
     * @param content 二维码内容
     * @param logoPath logo路径
     * @param bottomText 底部文字
     * @return {@link MultipartFile}
     * @ver v1.0.0
     */
    private static MultipartFile initMultipartFile(String content, String logoPath, String bottomText) throws Exception {
        //二维码名称
        String qrName = "qr" + System.currentTimeMillis() + ".jpg";

        int height = 0;
        if (StringUtils.hasText(bottomText)) {
            height = 10;
        }

        //得到BufferedImage对象
        BufferedImage bufferedImage = createImage(content, logoPath, true, height);
        // 判断是否添加底部文字
        if (StringUtils.hasText(bottomText)) {
            addBottomText(bufferedImage, bottomText);
        }

        //创建一个ByteArrayOutputStream
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        //把BufferedImage写入ByteArrayOutputStream
        ImageIO.write(bufferedImage, "jpg", os);
        //ByteArrayOutputStream转成InputStream
        InputStream input = new ByteArrayInputStream(os.toByteArray());
        //InputStream转成MultipartFile
        return new MockMultipartFile("qrFile", qrName, "text/plain", input);
    }

    /**
     * 生成 BufferedImage
     * @param content 存放在二维码中的内容
     * @param imgPath logo的路径及名称
     * @param needCompress 是否需要压缩
     * @param height 额外高度(有底部文字时)
     * @return {@link BufferedImage}
     * @ver v1.0.0
     */
    private static BufferedImage createImage(String content, String imgPath, boolean needCompress, int height) throws Exception {
        Hashtable hints = new Hashtable();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix bitMatrix = new MultiFormatWriter()
                .encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);
        int bitWidth = bitMatrix.getWidth();
        int bitHeight = bitMatrix.getHeight();
        BufferedImage image = new BufferedImage(bitWidth, bitHeight + height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < bitWidth; x++) {
            for (int y = 0; y < bitHeight; y++) {
                image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
            }
        }
        if (imgPath == null || "".equals(imgPath)) {
            return image;
        }
        // 插入logo图片
        insertImage(image, imgPath, needCompress);
        return image;
    }

    /**
     * 插入logo图片
     * @param source 二维码
     * @param logoPath logo的路径及名称
     * @param needCompress 是否需要压缩
     * @ver v1.0.0
     */
    private static void insertImage(BufferedImage source, String logoPath, boolean needCompress) throws Exception {
        File file = new File(logoPath);
        if (!file.exists()) {
            log.error(logoPath + "   该文件不存在!");
            return;
        }
        Image src = ImageIO.read(new File(logoPath));
        int width = src.getWidth(null);
        int height = src.getHeight(null);
        // 压缩LOGO
        if (needCompress) {
            if (width > WIDTH) {
                width = WIDTH;
            }
            if (height > HEIGHT) {
                height = HEIGHT;
            }
            Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
            BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics g = tag.getGraphics();
            // 绘制缩小后的图
            g.drawImage(image, 0, 0, null);
            g.dispose();
            src = image;
        }
        // 插入LOGO
        Graphics2D graph = source.createGraphics();
        int x = (QRCODE_SIZE - width) / 2;
        int y = (QRCODE_SIZE - height) / 2;
        graph.drawImage(src, x, y, width, height, null);
        Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
        graph.setStroke(new BasicStroke(3f));
        graph.draw(shape);
        graph.dispose();
    }

    /**
     * 二维码底部添加文字
     * @param source 二维码
     * @param bottomText 底部文字
     * @ver v1.0.0
     */
    private static void addBottomText(BufferedImage source, String bottomText) {
        //生成image
        int defineWidth = QRCODE_SIZE;
        int defineHeight = 20;
        BufferedImage textImage = new BufferedImage(defineWidth, defineHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = (Graphics2D) textImage.getGraphics();
        //开启文字抗锯齿
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2.setBackground(Color.WHITE);
        g2.clearRect(0, 0, defineWidth, defineHeight);
        g2.setPaint(Color.BLACK);
        FontRenderContext context = g2.getFontRenderContext();
        //部署linux需要注意 linux无此字体会显示方块
        Font font = new Font("微软雅黑", Font.BOLD, 15);
        g2.setFont(font);
        LineMetrics lineMetrics = font.getLineMetrics(bottomText, context);

        FontRenderContext frc = new FontRenderContext(new AffineTransform(), true, true);
        Rectangle rec = font.getStringBounds(bottomText, frc).getBounds();
        double fontWidth = rec.getWidth();
        double offset = (defineWidth - fontWidth) / 2;

        float y = (defineHeight + lineMetrics.getAscent() - lineMetrics.getDescent() - lineMetrics.getLeading()) / 2;
        g2.drawString(bottomText, (int) offset, (int) y);

        Graphics2D graph = source.createGraphics();
        //开启文字抗锯齿
        graph.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        //添加image
        int width = textImage.getWidth(null);
        int height = textImage.getHeight(null);

        graph.drawImage(textImage, 0, QRCODE_SIZE - 8, width, height, Color.WHITE, null);
        graph.dispose();
    }

二维码解析

	/**
     * 根据二维码路径解析二维码
     * @param path 二维码路径
     * @return {@link String}
     * @ver v1.0.0
     */
    public static String decode(String path) throws Exception {
        return decode(new File(path));
    }
    
    /**
     * 根据二维码文件解析二维码
     * @param file 二维码文件
     * @return {@link String}
     * @ver v1.0.0
     */
    public static String decode(File file) throws Exception {
        BufferedImage image = ImageIO.read(file);
        if (image == null) {
            return null;
        }
        BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Result result;
        Hashtable hints = new Hashtable();
        hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
        result = new MultiFormatReader().decode(bitmap, hints);
        return result.getText();
    }

是不是很简单0.0 好了,到这就结束啦!!!

以上就是Java生成与解析二维码的完整指南的详细内容,更多关于Java生成与解析二维码的资料请关注脚本之家其它相关文章!

相关文章

  • idea配置SVN的教程

    idea配置SVN的教程

    文章介绍了如何安装和配置TortoiseSVN以及在IntelliJ IDEA中配置SVN,首先,安装TortoiseSVN时需要选择与电脑配置相匹配的版本,并确保安装命令行客户端工具,其次,在IntelliJ IDEA中配置SVN时,需要指定SVN可执行文件的路径
    2025-01-01
  • Java泛型类型擦除

    Java泛型类型擦除

    这篇文章主要为大家详细介绍了Java泛型类型擦除,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-03-03
  • Java实现MD5加密算法方法例子

    Java实现MD5加密算法方法例子

    这篇文章主要给大家介绍了关于Java实现MD5加密算法方法的相关资料,MD5加密是一种常见的加密方式,我们经常用在保存用户密码和关键信息上,需要的朋友可以参考下
    2023-10-10
  • Java classloader类加载器的实现

    Java classloader类加载器的实现

    本文介绍了Java类加载器的继承性和隔离性,类加载器分为四个层级:Bootstrap、Extension、System和Plugin,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2025-12-12
  • 详解java操作Redis数据库的redis工具(RedisUtil,jedis工具JedisUtil,JedisPoolUtil)

    详解java操作Redis数据库的redis工具(RedisUtil,jedis工具JedisUtil,JedisPoo

    这篇文章主要介绍了java操作Redis数据库的redis工具,包括RedisUtil,jedis工具JedisUtil,JedisPoolUtil工具,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2021-08-08
  • jar中VO的探究

    jar中VO的探究

    这篇文章主要介绍了jar中VO的探究的相关资料,需要的朋友可以参考下
    2023-11-11
  • Java基础之java处理ip的工具类

    Java基础之java处理ip的工具类

    这篇文章主要介绍了Java基础应用,使用java处理ip的工具类的相关资料,需要的朋友可以参考下
    2014-10-10
  • 简单了解springboot eureka交流机制

    简单了解springboot eureka交流机制

    这篇文章主要介绍了简单了解springboot eureka交流机制,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • ​​​​​​​Spring多租户数据源管理 AbstractRoutingDataSource

    ​​​​​​​Spring多租户数据源管理 AbstractRoutingDataSource

    本文技术了​​​​​​​Spring多租户数据源管理 AbstractRoutingDataSource,下文详细内容介绍,需要的小伙伴可以参考一下
    2022-05-05
  • Spring Boot 类加载流程分析

    Spring Boot 类加载流程分析

    本文给大家介绍Spring Boot 类加载流程分析,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2026-03-03

最新评论