SpringBoot轻松实现二维码条形码含源码案例

简介: SpringBoot轻松实现二维码条形码含源码案例

1.介绍

QR码(Quick Response Code)是一种二维码(2D barcode)的类型,最早由日本公司Denso Wave于1994年开发。它是一种能够存储各种数据类型的矩阵二维条码,通常以黑色模块和白色背景的方式呈现。QR码可以存储文本、URL、联系信息、地理位置等多种信息,因此在移动设备、广告传播、商品标识等领域广泛使用。

ZXing是一个开源的条形码/二维码生成和解析库,支持多种格式的1D/2D条形码。它包含了QR码的生成和解析功能。ZXing库提供了多种语言的实现,包括Java、C++、Python等。

ZXing,全名为"Zebra Crossing",是一个开源的Java库,用于二维码的生成和解析。它是一个强大的工具,可以用于生成QR码以及解析包括QR码在内的多种二维码格式。ZXing提供了多种编程语言的API,使开发者能够轻松集成二维码功能到他们的应用中。它支持多种平台,包括Android、iOS、Java等。除了QR码,ZXing还支持解析其他一维码和二维码,例如EAN、UPC、DataMatrix等。

2.代码

package com.lp.util;
 
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.io.FileInputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
 
/**
 * @author liu pei
 * @date 2023年12月20日 下午7:04
 * @Description: 二维码,条形码 工具类
 */
public class QRCodeGeneratorUtils {
    public static void main(String[] args) {
        generateQRCode("www.baidu.com",200,200,"/home/2132.png");
    }
 
    /**
     * 生成QR码的方法
     * @param data 要存储在QR码中的数据,可以是文本、URL等。
     * @param width QR码的宽度(像素)。
     * @param height QR码的高度(像素)。
     * @param filePath 生成的QR码文件的保存路径。
     */
    public static String 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);
            return filePath;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * 二维码转 Base64
     * @param data
     * @param width
     * @param height
     * @param filePath
     * @return
     */
    public static String generateQRCodeStr(String data, int width, int height, String filePath) {
        String s = generateQRCode(data, width, height, filePath);
        return convertImageToBase64(s);
    }
    /**
     * 生成条形码的方法
     * @param data   要存储在条形码中的数据,可以是商品条形码等。
     * @param width  条形码的宽度(像素)。
     * @param height 条形码的高度(像素)。
     * @param filePath 生成的条形码文件的保存路径。
     */
    public static String generateBarcode(String data, int width, int height, String filePath) {
        try {
            Map<EncodeHintType, Object> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 设置字符编码
 
            MultiFormatWriter writer = new MultiFormatWriter();
            BitMatrix bitMatrix = writer.encode(data, BarcodeFormat.CODE_128, width, height, hints);
 
            // 创建BufferedImage对象来表示条形码
            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) ? 0 : 0xFFFFFF); // 生成黑色条和白色背景的条形码
                }
            }
 
            // 将条形码保存到文件
            File barcodeFile = new File(filePath);
            ImageIO.write(image, "png", barcodeFile);
 
            System.out.println("条形码已生成并保存到: " + filePath);
            return filePath;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * 图片转换为base64 前端可以直接展示
     * @param filePath
     * @return
     */
    public static String convertImageToBase64(String filePath){
        try {
            File file = new File(filePath);
            byte[] imageBytes = new byte[(int) file.length()];
            FileInputStream fis = new FileInputStream(file);
            fis.read(imageBytes);
            fis.close();
            String encodedImage = Base64.getEncoder().encodeToString(imageBytes);
            System.out.println(encodedImage);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

3.生成结果

相关文章
|
1天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的漫画阅读系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的漫画阅读系统的详细设计和实现(源码+lw+部署文档+讲解等)
|
1天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的快递信息管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的快递信息管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
|
1天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的高校听课评价系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的高校听课评价系统的详细设计和实现(源码+lw+部署文档+讲解等)
|
1天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的通讯录管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的通讯录管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
|
1天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的影院管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的影院管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
|
1天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的学习系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的学习系统的详细设计和实现(源码+lw+部署文档+讲解等)
|
1天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的钢铁集团公司安全管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的钢铁集团公司安全管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
|
1天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的普通话培训信息管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的普通话培训信息管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
|
1天前
|
JavaScript 前端开发 Java
基于SpringBoot+Vue+uniapp的在线开放课程的Web前端的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的在线开放课程的Web前端的详细设计和实现(源码+lw+部署文档+讲解等)
|
1天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的校园二手物品交易平台的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的校园二手物品交易平台的详细设计和实现(源码+lw+部署文档+讲解等)