Java后端如何生成二维码

为节约服务器资源,一般情况下,不要直接生成海量二维码。

可以考虑,前缀字符,自定义规则生成二维码。

支持自定义二维码大小、二维码logo、颜色等等

前端生成二维码

详见前端开发手册

附件:代码文件下载

​https://caiyun.139.com/m/i?135CmpZ2OwqB7​

演示

代码

复制代码
package com.pusdn.test;

import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.pusdn.PgzApplication;
import com.pusdn.common.config.PgzConfig;
import com.pusdn.common.utils.file.FileUploadUtils;
import com.pusdn.common.utils.spring.SpringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.awt.*;

@SpringBootTest(classes = PgzApplication.class)
public class QRCodeTest {

    @Test
    public void testQr() {
        String content = "https://pusdn-dev.feishu.cn/wiki/Q3ZpwiZXWiXLpwkv8JZcF0Nwngf?from=from_copylink";
        String logoPath = "/Users/janeyork/PUSDN/Work/XCN/pgz--gf-op-xcn/pgz-sbv-gf-admin/src/test/java/com/pusdn/test/temp.png";// 绝对路经
        // 推荐路经
        String qrPath = FileUploadUtils.getDefaultBaseDir() + "/qrcode/";
        // 或者  String qrPath = System.getProperty("user.home") + "/pusdn/" + "你的路经/";

        // 生成指定url对应的二维码到文件,宽和高都是300像素
        QrCodeUtil.generate(content, 300, 300, FileUtil.file(qrPath + "xxx.jpg"));


        QrCodeUtil.generate(
            content,
            QrConfig.create().setImg(logoPath), //附带logo
            FileUtil.file(qrPath + "xxx2.jpg")
        );



        QrConfig qrConfig = QrConfig.create()
            .setForeColor(Color.BLUE)
            .setBackColor(Color.MAGENTA)
            .setWidth(0)
            .setHeight(0).setMargin(1);
        String asciiArt = QrCodeUtil.generateAsAsciiArt("https://baidu.cn/",qrConfig);
        System.out.println(asciiArt);
    }
}
相关推荐
折哥的程序人生 · 物流技术专研6 小时前
第4篇:Lambda 简化策略模式(Java 8+)
java·设计模式·策略模式·函数式编程·lambda·代码简化·扩充系列
researcher-Jiang6 小时前
高性能计算之OpenMP——超算习堂学习1
android·java·学习
西门吹-禅8 小时前
java springboot N+1问题
java·开发语言·spring boot
DLYSB_8 小时前
生命通道:如何用 HIS 医疗系统直连网络声光终端,打造“零延误”的危急值响应网关?
java·网络·数据库·报警灯
weixin_BYSJ19878 小时前
SpringBoot + MySQL 乒乓球运动员信息管理系统项目实战--附源码04954
java·javascript·spring boot·python·django·flask·php
AI小码8 小时前
LLM 应用的缓存工程:当每次 API 调用都在燃烧成本
java·人工智能·spring·计算机·llm·编程·api
Hui Baby9 小时前
Spring Security
java·后端·spring
leoZ2319 小时前
Claude 驱动的全栈开发:Spring Boot + Vue 的 BS 架构实践
vue.js·spring boot·架构
IT笔记9 小时前
【Rust】Rust Match 模式匹配详解
java·开发语言·rust
我才是银古10 小时前
构建 Java GIS 工具库:从碎片化 API 到统一抽象的设计实践
java·gis·geotools·gdal·esri