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生成与解析二维码的资料请关注脚本之家其它相关文章!

相关文章

  • 解析Spring RestTemplate必须搭配MultiValueMap的理由

    解析Spring RestTemplate必须搭配MultiValueMap的理由

    本文给大家介绍Spring RestTemplate必须搭配MultiValueMap的理由,本文通过实例图文相结合给大家介绍的非常详细,需要的朋友参考下吧
    2021-11-11
  • mybatis教程之动态sql语句_动力节点Java学院整理

    mybatis教程之动态sql语句_动力节点Java学院整理

    这篇文章主要介绍了mybatis教程之动态sql语句,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • 详解Java设计模式之桥接模式

    详解Java设计模式之桥接模式

    桥接,顾名思义,就是用来连接两个部分,使得两个部分可以互相通讯。桥接模式将系统的抽象部分与实现部分分离解耦,使他们可以独立的变化。本文通过示例详细介绍了桥接模式的原理与使用,需要的可以参考一下
    2022-10-10
  • Java中的Semaphore原理解析

    Java中的Semaphore原理解析

    这篇文章主要介绍了Java中的Semaphore原理解析,Semaphore(信号量)是用来控制同时访问特定资源的线程数量,它通过协调各个线程,以保证合理的使用公共资源,需要的朋友可以参考下
    2024-01-01
  • 关于SpringBoot中的请求映射及使用

    关于SpringBoot中的请求映射及使用

    这篇文章主要介绍了关于SpringBoot中的请求映射及使用,Spring Boot 中的授权机制,包括基于角色的授权和基于资源的授权,同时,我们也将给出相应的代码示例,帮助读者更好地理解和应用这些授权机制,需要的朋友可以参考下
    2023-07-07
  • Java使用Spire.Doc for Java实现Word自动化插入图片

    Java使用Spire.Doc for Java实现Word自动化插入图片

    在日常工作中,Word文档是不可或缺的工具,而图片作为信息传达的重要载体,其在文档中的插入与布局显得尤为关键,下面我们就来看看如何使用Spire.Doc for Java实现Word文档自动化插入图片吧
    2025-12-12
  • 如何在spring boot项目中使用Spring Security的BCryptPasswordEncoder类进行相同密码不同密文的加密和验证

    如何在spring boot项目中使用Spring Security的BCryptPasswordE

    本文介绍如何在Spring Boot项目中通过修改pom.xml引入安全依赖,添加配置类以解除默认的HTTP请求拦截,以及如何创建BCryptPasswordEncoder对象进行密码的加密和匹配,通过这些步骤,可以有效地增强应用的安全性
    2023-08-08
  • SpringBoot中Entity、DTO、VO的通俗理解与实战案例

    SpringBoot中Entity、DTO、VO的通俗理解与实战案例

    本文介绍了SpringBoot中的Entity、DTO、VO三者的作用和区别,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2025-12-12
  • SpringBoot中ConfigurationProperties使用详解

    SpringBoot中ConfigurationProperties使用详解

    ConfigurationProperties是Spring Boot中管理外部配置的核心工具,通过“配置文件→实体类→代码使用”的流程,实现了配置的集中管理和动态绑定,感兴趣的可以了解一下
    2026-01-01
  • Java中ShardingSphere分库分表实战

    Java中ShardingSphere分库分表实战

    我们做项目的时候,数据量比较大,单表千万级别的,需要分库分表,本文主要介绍了Java中ShardingSphere分库分表实战,感兴趣的可以了解一下
    2021-09-09

最新评论