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


相关推荐
先鱼鲨生2 分钟前
gtest框架的安装与使用
开发语言·apache
梦幻通灵18 分钟前
IDEA查看源码利器XCodeMap插件
java·intellij-idea
Ashlee_code30 分钟前
南太平洋金融基建革命:斐济-巴新交易所联盟的技术破局之路 ——从关税动荡到离岸红利,跨境科技如何重塑太平洋资本生态
java·开发语言·科技·金融·重构·web3·php
隐-梵31 分钟前
2025年测绘程序设计比赛--基于统计滤波的点云去噪(已获国特)
java·开发语言·windows·c#·.net
没有梦想的咸鱼185-1037-166333 分钟前
MATLAB科研数据可视化技术
开发语言·机器学习·matlab·信息可视化·数据分析
叉烧钵钵鸡1 小时前
Java ++i 与 i++ 底层原理
java·开发语言·后端
御水流红叶1 小时前
安卓加固脱壳
android·开发语言·python
hqxstudying1 小时前
SpringAI的使用
java·开发语言·人工智能·springai
狐小粟同学1 小时前
JAVAEE--4.多线程案例
java·开发语言
the beard2 小时前
RabbitMQ:基于SpringAMQP声明队列与交换机并配置消息转换器(三)
java·开发语言·rabbitmq·intellij idea