调用方法:
QrcodeUtil.encode("2020042312251445");
下面是工具类QrcodeUtil.java
package com.fc.test.common.code;
import cn.hutool.core.lang.UUID;
import com.fc.test.common.file.FileUploadUtils;
import com.fc.test.common.file.FileUtils;
import com.google.zxing.*;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Hashtable;
/**
* 二维码生成
* @author zlxls
* @date 2020-03-26
**/
public class QrcodeUtil {
public static void main(String[] args) {
String imgName = "111.png";
// 生成的二维码的路径
String imgPath = FileUploadUtils.getRoot_dir()+"/qrcode/";
String content = "123456";
//生成二维码
try {
QrcodeUtil.encode(content,imgPath, imgName);
} catch (Exception e) {
e.printStackTrace();
}
}
private static final String CHARSET = "utf-8";
private static final String FORMAT_NAME = "PNG";
// 二维码尺寸
private static final int QRCODE_SIZE = 240;
// LOGO宽度
private static final int WIDTH = 60;
// LOGO高度
private static final int HEIGHT = 60;
/**
* 生成图片
* @param content 内容
* @return
* @throws Exception
*/
public static String encode(String content) throws Exception {
String imgName = UUID.randomUUID()+".png";
// 生成的二维码的路径
String imgPath = "qrcode/";
if(QrcodeUtil.encode(content,FileUploadUtils.getRoot_dir()+imgPath, imgName)){
return imgPath+imgName;
}
return "";
}
/**
* 生成图片
* @param content 内容
* @param imgPath 图片路径
* @param imgName 图片名称
* @param bgimgPath logo地址
* @param needCompress 是否压缩LOGO
* @return
* @throws Exception
*/
public static boolean encode(String content, String imgPath, String imgName, String bgimgPath, boolean needCompress) throws Exception {
BufferedImage image = QrcodeUtil.createImage(content, bgimgPath, needCompress);
FileUtils.mkdirs(imgPath);
return ImageIO.write(image, FORMAT_NAME, new File(imgPath+imgName));
}
/**
* 生成图片
* @param content 内容
* @param imgPath 图片路径
* @param imgName 图片名称
* @return
* @throws Exception
*/
public static boolean encode(String content, String imgPath, String imgName) throws Exception {
BufferedImage image = QrcodeUtil.createImage(content,"",false);
FileUtils.mkdirs(imgPath);
return ImageIO.write(image, FORMAT_NAME, new File(imgPath+imgName));
}
/**
* 生成图片对象
* @param content
* @param bgimgPath
* @param needCompress
* @return
* @throws Exception
*/
public static BufferedImage encode(String content, String bgimgPath, boolean needCompress) throws Exception {
BufferedImage image = QrcodeUtil.createImage(content, bgimgPath, needCompress);
return image;
}
/**
* 生成 图片对象
* @param content 二维码内容
* @param bgimgPath logo地址
* @param needCompress 是否压缩LOGO
* @return
* @throws Exception
*/
private static BufferedImage createImage(String content, String bgimgPath,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);
}
}
if (bgimgPath == null || "".equals(bgimgPath)) {
return image;
}
// 插入图片
QrcodeUtil.insertImage(image, bgimgPath, needCompress);
return image;
}
/**
* 插入logo
* @param source 二维码图片
* @param bgimgPath logo地址
* @param needCompress 是否压缩LOGO
* @throws Exception
*/
private static void insertImage(BufferedImage source, String bgimgPath,boolean needCompress) throws Exception {
File file = new File(bgimgPath);
if (!file.exists()) {
System.err.println("" + bgimgPath + " 该文件不存在!");
return;
}
Image src = ImageIO.read(new File(bgimgPath));
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();
}
}