生成二维码需要的jar包
<!-- 二维码 -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
生成二维码代码
package com.cloud.sys.util;
import com.google.zxing.*;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.apache.axis.encoding.Base64;
import org.apache.axis.utils.ByteArrayOutputStream;
import org.apache.commons.lang3.StringUtils;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Hashtable;
/**
* 生成二维码工具类
*/
public class QRCodeUtil {
private static final String CHARSET = "utf-8";
private static final String FORMAT_NAME = "PNG";
// 二维码尺寸
private static final int QRCODE_SIZE = 420;
// LOGO宽度
private static final int WIDTH = 60;
// LOGO高度
private static final int HEIGHT = 60;
/**
* 生成二维码
*
* @param content 二维码内容
* @return 返回新image
*/
private static BufferedImage createImage(String content, String imgPath, boolean needCompress) throws Exception {
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE,
hints);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
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) ? 0xFF000000 : 0xFFFFFFFF);
}
}
// 若没有需要二维码嵌套的logo,直接返回二维码
if (StringUtils.isBlank(imgPath)) {
return image;
}
// 插入图片
QRCodeUtil.insertImage(image, imgPath, needCompress);
return image;
}
/**
* 二维码嵌套logo
*
* @param source
* @param imgPath
* @param needCompress
* @throws Exception
*/
private static void insertImage(BufferedImage source, String imgPath, boolean needCompress) throws Exception {
if(StringUtils.isBlank(imgPath)){
System.err.println("" + imgPath + " 该文件不存在!");
}
URL url = new URL(imgPath);
Image src = ImageIO.read(url);
int width = src.getWidth(null);
int height = src.getHeight(null);
if (needCompress) { // 压缩LOGO
if (width > WIDTH) {
width = WIDTH;
}
if (height > HEIGHT) {
height = HEIGHT;
}
Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null); // 绘制缩小后的图
g.dispose();
src = image;
}
// 插入LOGO
Graphics2D graph = source.createGraphics();
int x = (QRCODE_SIZE - width) / 2;
int y = (QRCODE_SIZE - height) / 2;
graph.drawImage(src, x, y, width, height, null);
Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
graph.setStroke(new BasicStroke(3f));
graph.draw(shape);
graph.dispose();
}
/**
* 根据背景图片和logo生成二维码
*
* @param imgPath 背景图片
* @param logoPath logo
* @param content 二维码图片地址
* @return
* @author st
* @date 2022-05-19 17:06
*/
public static String encodeImgAndLogo(String content, String imgPath, String logoPath, String destPath, boolean needCompress) throws Exception {
BufferedImage code;
if (StringUtils.isNotBlank(imgPath)) {
// -----------------------------------------------------背景图片--------------------------------------------------------------
InputStream biginputStream = getImageFromNetByUrl(imgPath);
code = ImageIO.read(biginputStream);
Graphics2D g = code.createGraphics();
// 消除文字锯齿
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
// 消除画图锯齿
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// -----------------------------------------------------二维码------------------------------------------------------------
if (StringUtils.isNotBlank(content)) {
// 生成二维码
BufferedImage image = QRCodeUtil.createImage(content, logoPath, needCompress);
// 合成图片,若不显示二维码,修改x\y位置
g.drawImage(image, 290, 380, image.getWidth(), image.getHeight(), null);
}
// 关闭资源
g.dispose();
} else {
code = QRCodeUtil.createImage(content, logoPath, needCompress);
}
//修复图片失真问题
BufferedImage newBufferedImage = new BufferedImage(code.getWidth(), code.getHeight(), BufferedImage.TYPE_INT_RGB);
newBufferedImage.createGraphics().drawImage(code, 0, 0, Color.WHITE, null);
// 二维码输出位置
if (!StringUtils.isBlank(destPath)) {
ImageIO.write(newBufferedImage, FORMAT_NAME, new File(destPath));
}
// 写入流
ByteArrayOutputStream stream = new ByteArrayOutputStream();
ImageIO.write(newBufferedImage, FORMAT_NAME, stream);
String base64 = Base64.encode(stream.toByteArray());
stream.flush();
stream.close();
return base64;
}
/**
* 根据网络地址获得数据的字节流
*
* @param strUrl
* @return
*/
private static InputStream getImageFromNetByUrl(String strUrl) {
try {
URL url = new URL(strUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
conn.setDoInput(true);
conn.connect();
// 得到网络返回的输入流
return conn.getInputStream();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}