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

推荐订阅源

D
Darknet – Hacking Tools, Hacker News & Cyber Security
爱范儿
爱范儿
GbyAI
GbyAI
A
About on SuperTechFans
阮一峰的网络日志
阮一峰的网络日志
U
Unit 42
博客园_首页
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
S
Secure Thoughts
Security Latest
Security Latest
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
PCI Perspectives
PCI Perspectives
MyScale Blog
MyScale Blog
罗磊的独立博客
Y
Y Combinator Blog
Know Your Adversary
Know Your Adversary
月光博客
月光博客
C
CXSECURITY Database RSS Feed - CXSecurity.com
Recorded Future
Recorded Future
S
Securelist
T
Tor Project blog
Apple Machine Learning Research
Apple Machine Learning Research
人人都是产品经理
人人都是产品经理
WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
P
Privacy & Cybersecurity Law Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
雷峰网
雷峰网
Microsoft Security Blog
Microsoft Security Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Project Zero
Project Zero
T
Tailwind CSS Blog
腾讯CDC
C
Cisco Blogs
T
The Exploit Database - CXSecurity.com
The Hacker News
The Hacker News
F
Full Disclosure
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
美团技术团队
N
Netflix TechBlog - Medium
云风的 BLOG
云风的 BLOG
N
News and Events Feed by Topic
C
Cybersecurity and Infrastructure Security Agency CISA
D
Docker
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
O
OpenAI News
Cloudbric
Cloudbric

博客园 - 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 的二维码已生成!");
    }
}