<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.4.1</version> <!-- 请使用最新版本 -->
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.4.1</version>
</dependency>
import cn.hutool.extra.qrcode.BufferedImageLuminanceSource;
import com.google.zxing.*;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import org.springframework.stereotype.Component;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
@Component
public class QRCodeGeneratorService {
// 生成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 void 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);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 解析二维码
* @param path 二维码图片路径
* @return String 二维码内容
* @throws Exception
*/
public static String decode(String path) throws Exception {
File file = new File(path);
BufferedImage image = ImageIO.read(file);
if (image == null) {
return null;
}
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result;
Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
result = new MultiFormatReader().decode(bitmap, hints);
return result.getText();
}
public static void main(String[] args) throws Exception {
// 生成二维码并保存到文件系统中(路径为/tmp/qrcode.png)
QRCodeGeneratorService qrCodeGenerator = new QRCodeGeneratorService();
String data = "https://todoitbo.fun"; // 要存储在QR码中的数据
int width = 300; // QR码的宽度
int height = 300; // QR码的高度
String filePath = "/data/qrcode.png"; // 生成的QR码文件的路径
qrCodeGenerator.generateQRCode(data, width, height, filePath);
//条形码
String datas = "123456789"; // 要存储在条形码中的数据
int widths = 600; // 条形码的宽度
int heights = 100; // 条形码的高度
String filePaths = "/data/barcode.png"; // 生成的条形码文件的路径
qrCodeGenerator.generateBarcode(datas, widths, heights, filePaths);
String decodeqrcode = decode("/data/qrcode.png");
System.out.println("二维码内容:"+decodeqrcode);
String decodebarcode = decode("/data/barcode.png");
System.out.println("条形码内容:"+decodebarcode);
}
}