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扫下试试,自动扫描出链接,并且前往页面,好了本文到这里就结束了


相关推荐
怪兽201432 分钟前
fastjson在kotlin不使用kotlin-reflect库怎么使用?
android·开发语言·kotlin
ClearLiang32 分钟前
Kotlin-协程的挂起与恢复
开发语言·kotlin
彭同学学习日志38 分钟前
Kotlin Fragment 按钮跳转报错解决:Unresolved reference ‘floatingActionButton‘
android·开发语言·kotlin
海域云赵从友43 分钟前
破解跨境数据传输瓶颈:中国德国高速跨境组网专线与本地化 IP 的协同策略
开发语言·php
咚咚王者1 小时前
人工智能之编程进阶 Python高级:第九章 爬虫类模块
开发语言·python
会编程的林俊杰1 小时前
SpringBoot项目启动时的依赖处理
java·spring boot·后端
一叶飘零_sweeeet1 小时前
深度拆解汽车制造系统设计:用 Java + 设计模式打造高扩展性品牌 - 车型动态生成架构
java·设计模式·工厂设计模式
深蓝海拓1 小时前
使matplot显示支持中文和负号
开发语言·python
王家羽翼-王羽2 小时前
nacos 3.1.0 运行主类报错 com.alibaba.cloud.nacos.logging.NacosLoggingAppRunListener
java
syt_biancheng2 小时前
Day3算法训练(简写单词,dd爱框框,3-除2!)
开发语言·c++·算法·贪心算法