
matrix.boot.common.utils.QrCodeUtil Maven / Gradle / Ivy
package matrix.boot.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.decoder.ErrorCorrectionLevel;
import matrix.boot.common.exception.ServiceException;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
/**
* 二维码生成工具
* @author wangcheng
*/
public class QrCodeUtil {
private static final Map hints = new HashMap<>();
private static final String FORMAT = "png";
/**
* 宽度
*/
private Integer width;
/**
* 高度
*/
private Integer height;
/**
* 文件路径
*/
private String filePath;
static {
hints.put(EncodeHintType.CHARACTER_SET, StandardCharsets.UTF_8.toString());
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.MARGIN, 0);
}
private QrCodeUtil() {}
private QrCodeUtil(int width, int height, String filePath) {
this.width = width;
this.height = height;
this.filePath = filePath;
}
/**
* 实例化
* @param width 宽度
* @param height 高度
* @param filePath 文件路径
* @return 实例
*/
public static QrCodeUtil getInstance(int width, int height, String filePath) {
return new QrCodeUtil(width, height, filePath);
}
/**
* 返回一个 BufferedImage 对象
* @param content 二维码内容
*/
public BufferedImage getBufferedImage(String content) {
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
return MatrixToImageWriter.toBufferedImage(bitMatrix);
} catch (Exception e) {
throw new ServiceException(e);
}
}
/**
* 将二维码图片输出到一个流中
* @param content 二维码内容
* @param stream 输出流
*/
public void write(String content, OutputStream stream) {
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
MatrixToImageWriter.writeToStream(bitMatrix, FORMAT, stream);
} catch (Exception e) {
throw new ServiceException(e);
}
}
/**
* 获取图片的Base64编码
* @param content 内容
* @return Base64编码
*/
public String getImageBase64(String content) {
BufferedImage image = getBufferedImage(content);
return ImageUtil.getImageBase64(image);
}
/**
* 将二维码输出到文件返回文件名
* @param content 内容
* @return 文件名称
*/
public String writeToFile(String content) {
AssertUtil.notNullTip(filePath, "QrCodeHelper:filePath");
FileOutputStream fos = null;
try {
BufferedImage bufferedImage = getBufferedImage(content);
String fileName = RandomUtil.getUUID() + "." + FORMAT;
fos = new FileOutputStream(new File(filePath, fileName));
ImageIO.write(bufferedImage, FORMAT, fos);
return fileName;
} catch (Exception e) {
throw new ServiceException(e);
} finally {
BIOStreamUtil.closeStream(fos);
}
}
/**
* 解析QrCode
* @param fileName 文件名称
* @return 解析内容
*/
public String parseFile(String fileName) {
AssertUtil.notNullTip(this.filePath, "QrCodeHelper:filePath");
AssertUtil.notNullTip(fileName, "QrCodeHelper:fileName");
try {
BufferedImage image = ImageIO.read(new File(filePath, fileName));
LuminanceSource source = new BufferedImageLuminanceSource(image);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
Map hints = new HashMap<>(1);
hints.put(DecodeHintType.CHARACTER_SET, StandardCharsets.UTF_8.toString());
Result result = new MultiFormatReader().decode(binaryBitmap, hints);
return result.toString();
} catch (Exception e) {
throw new ServiceException(e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy