惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recent Commits to openclaw:main
Recent Commits to openclaw:main
N
News | PayPal Newsroom
TaoSecurity Blog
TaoSecurity Blog
Google Online Security Blog
Google Online Security Blog
NISL@THU
NISL@THU
T
Threatpost
C
CXSECURITY Database RSS Feed - CXSecurity.com
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Engineering at Meta
Engineering at Meta
AWS News Blog
AWS News Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Privacy International News Feed
B
Blog
PCI Perspectives
PCI Perspectives
Martin Fowler
Martin Fowler
Spread Privacy
Spread Privacy
P
Proofpoint News Feed
T
Tenable Blog
F
Fortinet All Blogs
G
GRAHAM CLULEY
V2EX - 技术
V2EX - 技术
C
Check Point Blog
Project Zero
Project Zero
P
Palo Alto Networks Blog
J
Java Code Geeks
W
WeLiveSecurity
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
The Exploit Database - CXSecurity.com
博客园 - 司徒正美
P
Privacy & Cybersecurity Law Blog
S
SegmentFault 最新的问题
Last Week in AI
Last Week in AI
Forbes - Security
Forbes - Security
C
Cybersecurity and Infrastructure Security Agency CISA
Security Latest
Security Latest
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Recent Announcements
Recent Announcements
博客园 - Franky
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Recorded Future
Recorded Future
The Last Watchdog
The Last Watchdog
MongoDB | Blog
MongoDB | Blog
人人都是产品经理
人人都是产品经理
酷 壳 – CoolShell
酷 壳 – CoolShell
Cisco Talos Blog
Cisco Talos Blog
量子位
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC

博客园 - KLAPT

Gateway 网关 CodeX =>Skills Redis 内存满了怎么处理 SpringBoot 默认配置修改 JWT 续签 Access Token + Refresh Token 双 Token claudeCode 命令 MyBatis 的 Mapper 接口 AI | CC GUI 集成 IDEA 完整教程 在IDEA中使用Claude Code IDEA中使用CodeX MyBatisPlus解决大数据量查询慢问题 idea 中的 claude code Token Dubbo 和 Spring Cloud Gateway的区别 Transactional 注解中propagation 掌握 Spring 框架这 10 个扩展点 SpringBoot 快速实现 api 加密 Spring Boot/Cloud 中 bootstrap.yml 与 application.yml SpringBoot 实现 DOCX 转 PDF 微服务Token鉴权设计的几种方案 进程、线程、协程 RSA 加密 ntp服务端和客户端 Chronyd与NTP chronyd 作为服务器时钟 chrony sudo命令和su 的区别 java -cp 和 java -jar Maven 项目打包:实现业务代码与第三方依赖分离 达梦数据库创建用户 梦数据库新增大字段报错问题 达梦数据库操作 MySQL UPDATE多表关联更新 达梦数据库 为HTTP POST请求设置请求体 在Java中调用第三方接口并返回第三方页面 Java调用第三方接口的方法 Nginx 之Rewrite 使用详解 linux 命令 Spring Boot项目中集成Spring Security OAuth2和Apache Shiro
Java二维码
KLAPT · 2026-03-09 · via 博客园 - KLAPT

二维码工具

  • ZXing (Zebra Crossing):Google 出品,经典老牌,稳定可靠。
  • QrCode (来自 com.google.zxing.qrcode):轻量级,适合快速开发。
  • QRCodeUtils (封装工具类):自己撸一点封装,方便复用。

1. 引入 Maven 依赖

  com.google.zxing
core
3.5.2


com.google.zxing
javase
3.5.2
2. 封装一个二维码工具类
package com.moyu.qr;

import com.google.zxing.*;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

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

/**
 * 天天摸鱼的 Java 工程师出品
 * 通用二维码生成工具类
 */
publicclass QrCodeUtils {

    /**
     * 生成普通二维码并保存为图片文件
     *
     * @param content  二维码内容
     * @param filePath 保存路径(绝对路径)
     * @param width    宽度
     * @param height   高度
     * @throws Exception
     */
    public static void generateSimpleQrCode(String content, String filePath, int width, int height) throws Exception {
        // 编码参数设置
        Map hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 编码类型
        hints.put(EncodeHintType.MARGIN, 1);              // 边距

        BitMatrix bitMatrix = new MultiFormatWriter().encode(
                content,
                BarcodeFormat.QR_CODE,
                width,
                height,
                hints
        );

        Path path = new File(filePath).toPath();
        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
    }

    /**
     * 生成带 Logo 的二维码
     *
     * @param content   二维码内容
     * @param logoPath  logo 图片路径
     * @param outputPath 输出二维码路径
     */
    public static void generateQrCodeWithLogo(String content, String logoPath, String outputPath) throws Exception {
        int width = 300;
        int height = 300;

        Map hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        hints.put(EncodeHintType.MARGIN, 1);
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 容错率高,避免 logo 遮挡

        BitMatrix bitMatrix = new MultiFormatWriter().encode(
                content, BarcodeFormat.QR_CODE, width, height, hints
        );

        // 生成二维码图像
        BufferedImage qrImage = MatrixToImageWriter.toBufferedImage(bitMatrix, new MatrixToImageConfig());

        // 加载 logo 图片
        BufferedImage logo = ImageIO.read(new File(logoPath));

        int logoWidth = qrImage.getWidth() / 5;
        int logoHeight = qrImage.getHeight() / 5;

        // 计算 logo 放置位置(居中)
        int x = (qrImage.getWidth() - logoWidth) / 2;
        int y = (qrImage.getHeight() - logoHeight) / 2;

        // 合并图片
        Graphics2D g = qrImage.createGraphics();
        g.drawImage(logo, x, y, logoWidth, logoHeight, null);
        g.dispose();

        ImageIO.write(qrImage, "PNG", new File(outputPath));
    }
}

3. 实战调用:生成一个带参数的推广码

public class Main {
    public static void main(String[] args) throws Exception {
        String content = "https://yourdomain.com/register?ref=userid_123456";
        String savePath = "D:/qrcode/promo.png";

        QrCodeUtils.generateSimpleQrCode(content, savePath, 300, 300);
        System.out.println("推广二维码生成成功,快去扫码看看!");
    }
}

4.  带 Logo 的品牌码

public class LogoDemo {
    public static void main(String[] args) throws Exception {
        String content = "https://yourapp.com/share?id=abc123";
        String logo = "D:/logo/logo.png";
        String output = "D:/qrcode/share_with_logo.png";

        QrCodeUtils.generateQrCodeWithLogo(content, logo, output);
        System.out.println("带 Logo 的二维码已生成!");
    }
}