Zxing库的使用⭐️实现给自己的博客主页生成一张二维码链接,有源码可以直接复制到本地执行

目录

前言

一、简介

二、本地实现

[2.1 引入依赖(根据自己springboot项目来)](#2.1 引入依赖(根据自己springboot项目来))

[2.2 实现类](#2.2 实现类)

三、运行一次


前言

小伙伴们大家好,自从地铁上刷到Zxing库的使用后,一直想本地部署玩一玩

一、简介

ZXing(全称为 Zebra Crossing)是一个开源的条码/二维码处理库,提供了识别、生成和解码条码/二维码的功能。它由 Google 开发并维护,采用 Java 语言编写,并支持多种编程语言的接口。(看到底层使用java编写的,突然很亲切有莫有)

二、本地实现

2.1 引入依赖(根据自己springboot项目来)
XML 复制代码
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.4.1</version>
        </dependency>
2.2 实现类
java 复制代码
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;

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

public class ZxingTest {
    // 生成QR码的方法
    public void generateQRCode(String data, int width, int height, String filePath) {
        try {
            Map<EncodeHintType, Object> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 设置字符编码
            hints.put(EncodeHintType.ERROR_CORRECTION, com.google.zxing.qrcode.decoder.ErrorCorrectionLevel.H); // 错误纠正级别
            hints.put(EncodeHintType.MARGIN, 1); // 二维码边距

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

            // 创建BufferedImage对象来表示QR码
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
                    image.setRGB(x, y, bitMatrix.get(x, y) ? Color.BLACK.getRGB() : Color.WHITE.getRGB());
                }
            }

            // 将QR码保存到文件
            File qrCodeFile = new File(filePath);
            ImageIO.write(image, "png", qrCodeFile);

            System.out.println("QR码已生成并保存到: " + filePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        ZxingTest qrCodeGenerator = new ZxingTest();
        String dataNew = "https://blog.csdn.net/TM007_?spm=1000.2115.3001.5343";
        int width = 300; // QR码的宽度
        int height = 300; // QR码的高度
        String filePath = "C:\\Users\\xxx\\Desktop\\testZxing\\002.png"; // 生成的QR码文件的路径

        qrCodeGenerator.generateQRCode(dataNew, width, height, filePath);
    }

}

2.3 涉及到的具体方法原理不做介绍,看下main方法内,data为二维码包含的内容 ,filepath为图片的生成地址

三、运行一次

执行成功,去瞅瞅,可以看到大小格式是根据赋值的值来的(不贴了,过不了审)

用phone扫下试试,自动扫描出链接,并且前往页面,好了本文到这里就结束了


相关推荐
蛋蛋的就会顺顺的4 小时前
hot100 p0——栈,贪心算法,动态规划
java·前端·javascript·算法·leetcode·力扣
需要8265 小时前
Spring Bean 生命周期:从实例化到销毁的完整钩子
java·后端·spring
小 黄 鸡5 小时前
HashMap 面试必会知识点详解(JDK 8)
java·面试·职场和发展
死磕音视频6 小时前
让小模型做选择题:限定候选项并读取 logits 概率的入门实践(附 Python 代码)
开发语言·python·大模型·文本分类·transformers
伯恩bourne7 小时前
Spring Boot Bean 注册的两种方式:@Component 家族 vs @Configuration + @Bean
java·spring boot·后端
silvamux_7 小时前
curl 测试 OpenAI 兼容 API 的完整流程
java·人工智能·缓存·ai·oneapi
省钱兄--zs8 小时前
深圳24小时自助健身房软硬件解决方案实战指南与开发流程详解
java·数据仓库·spring boot·小程序·架构
顽疲8 小时前
Java vue 养老系统源码详解:月账单 care_month_bill 与 Quartz 扣费 Job 实践
java·开发语言·vue.js·vue养老·springboot养老·java养老
Wang's Blog8 小时前
Java 项目实战: 交友项目 - 安装开发工具与环境准备
java·开发语言·交友
用户21296835261688 小时前
开源了一个 Java 多智能体 LLM 框架 Baton:一个依赖,把 Agent 循环、工具调用、记忆、编排全配齐
java·llm