ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口。Zxing可以实现使用手机的内置的摄像头完成条形码的扫描及解码。
导入对应的jar 包
<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>
对应的 Utils 实现生成或者解析对应的二维码信息
package com.ruoyi.common.utils;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeWriter;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* 生成二维码 填充信息
*/
package com.ruoyi.common.utils;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeWriter;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* 生成二维码 填充信息
*/
public class QrCodeUtils {
/**
*
* @param text 对应的信息
* @param width 图片宽度
* @param height 图片高度
* @param filePath 存放位置
* @throws WriterException
* @throws IOException
*/
public static void generateQRCodeImage(String text, int width, int height, String filePath) // Generate QR Code image
throws WriterException, IOException {
QRCodeWriter qrCodeWriter = new QRCodeWriter(); // Create QR Code writer
Map<EncodeHintType, Object> hints = new HashMap<>(); // Set QR Code encoding hints
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // Set character set to UTF-8
BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height, hints);
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String number = format.format(date);
File qrCodeFile = new File(filePath + "/" + number+ ".png");
MatrixToImageWriter.writeToPath(bitMatrix, "PNG", qrCodeFile.toPath());
}
public static void analysisQRCodeImage() throws IOException, NotFoundException {
File file = new File("D://path/20240717143149228.png");
BufferedImage bufferedImage = ImageIO.read(file);
LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result = new MultiFormatReader().decode(bitmap);
System.out.println("QR Code content: " + result.getText());
}
}
具体调用
QrCodeUtils.generateQRCodeImage("12321", 350, 350, "D://path");
QrCodeUtils.analysisQRCodeImage();
标签:google,java,zxing,二维码,new,import,生成,com
From: https://blog.csdn.net/ling_zhi_xin/article/details/141025730