引入依赖Jar
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId> javase </artifactId>
<version>3.4.0</version>
</dependency>
二维码图片生成工具类
方法drawQRCode(),根据内容直接生成,同时可以指定生成二维码图片的宽度和高度
方法draw Logo QRCode(),根据内容生成,支持在二维码中间带上logo。
public class QRCodeUtils {
private final static Logger logger = LoggerFactory.getLogger(QRCodeUtils.class);
/**
* 二维码图片格式
*/
private final static String PNG_FORMAT = "png";
private final static String UTF8 = "utf-8";
/**
* 二维码图片背景颜色,黑色
*/
public static int QR_COLOR = 0xFF000000;
/**
* 二维码图片颜色,白色
*/
public static int BG_WHITE = 0xFFFFFFFF;
/**
* 根据二维码内容生成二维码
*
* @param url 二维码内容
* @param imageWidth 图片宽度
* @param imageHeight 图片高度
* @return
*/
public static String drawQRCode(String url, int imageWidth, int imageHeight) {
if (StringUtils.isEmpty(url)) {
return null;
}
try (
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
) {
QRCodeWriter writer = new QRCodeWriter();
BitMatrix bm = writer.encode(url, Barcode Format.QR_CODE, imageWidth, imageHeight, HINTS);
MatrixToImageWriter.writeToStream(bm, PNG_FORMAT, outStream);
return new String(Base64.encodeBase64(outStream.toByteArray()), UTF8);
} catch ( Exception e) {
logger.error("生成二维码失败:" + e.getMessage());
}
return null;
}
/**
* 生成带logo的二维码图片
*
* @param logoUrl logo图片地址
* @param qrUrl 二维码地址
* @param imageWidth 图片宽度
* @param imageHeight 图片高度
* @return
*/
public static String drawLogoQRCode(String logoUrl, String qrUrl, int imageWidth, int imageHeight) {
try {
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
// 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数
BitMatrix bm = multiFormatWriter.encode(qrUrl, BarcodeFormat.QR_CODE, imageWidth, imageHeight, HINTS);
BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
// 开始利用二维码数据创建 Bitmap 图片,分别设为黑(0xFFFFFFFF)白(0xFF000000)两色
for (int x = 0; x < imageWidth; x++) {
for (int y = 0; y < imageHeight; y++) {
image.setRGB(x, y, bm.get(x, y) ? QR_COLOR : BG_WHITE);
}
}
add logo Image(logoUrl, image);
image.flush();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(image, PNG_FORMAT, outputStream);
return new String(Base64.encodeBase64(outputStream.toByteArray()), UTF8);
} catch (Exception e) {
logger.error("drawLogoQRCode error" + e.getMessage());
}
return "";
}
private static void addLogoImage(String logoUrl, final BufferedImage image) throws IOException {
BufferedImage logoBuff = null;
if (logoUrl.startsWith("http")) {
// 构建绘图对象
InputStream logoFileInputStream = new URL(logoUrl).openStream();
logoBuff = ImageIO.read(logoFileInputStream);
} else {
File logoFile = new File(logoUrl);
if (logoFile. exists ()) {
logoBuff = ImageIO.read(logoFile);
}
}
Graphics2D g = image.createGraphics();
int width = image.getWidth();
int height = image.getHeight();
g.drawImage(logoBuff, width * 2 / 5, height * 2 / 5, width * 2 / 10, height * 2 / 10, null);
g.dispose();
logoBuff.flush();
}
/**
* 用于设置QR二维码参数
*/
private static Map<EncodeHintType, Object> HINTS = new HashMap<EncodeHintType, Object>() {
{
// 设置QR二维码的纠错级别(H为最高级别)具体级别信息
put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
// 设置编码方式
put(EncodeHintType.CHARACTER_SET, UTF8);
put(EncodeHintType.MARGIN, 0);
}
};
}
使用及效果
String logo = "#34;;
String url = "#34;;
//String urlPath = QRCodeUtils.drawLogoQRCode(logo, url, 400, 400);
String urlPath = QRCodeUtils.drawQRCode(url, 400, 400);
String bas464 = "data:image/png;base64," + urlPath;
return bas464;
<img src=“{base64内容}” />