1.添加依赖
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.0.0</version>
</dependency>
2.代码
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.springframework.stereotype.Service;
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;
@Service
public class TestService {
private static final String CHARSET = "utf-8";
private static final String FORMAT_NAME = "JPG";
/**
* content:二维码内容(根据自己的运用情况生成)
* qrCodeSize:二维码尺寸
* imgPath:logo访问地址
* needCompress:是否压缩logo
* logoSize:logo尺寸
*/
public static void qrCode(String content,Integer qrCodeSize,String imgPath,Integer logoSize,boolean needCompress) {
//文件存储地址
String destPath = "/user/test/";
String pictureName = "1234567890" + "." + "jpg";
try {
encode(content,qrCodeSize,destPath,pictureName,imgPath,needCompress,logoSize);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 生成二维码(内嵌LOGO)
* content:二维码内容(根据自己的运用情况生成)
* qrCodeSize:二维码尺寸
* imgPath:logo访问地址
* needCompress:是否压缩logo
* logoSize:logo尺寸
*/
public static void encode(String content, Integer qrCodeSize, String destPath,String pictureName,
String imgPath, boolean needCompress,Integer logoSize) throws Exception {
BufferedImage image = createImage(content,qrCodeSize,imgPath, needCompress,logoSize);
mkdirs(destPath);
ImageIO.write(image, FORMAT_NAME, new File(destPath + pictureName));
}
private static BufferedImage createImage(String content ,Integer qrCodeSize,String imgPath,
boolean needCompress,Integer logoSize) throws Exception {
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
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, qrCodeSize, qrCodeSize, 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 (imgPath == null || "".equals(imgPath)) {
return image;
}
//插入图片
insertImage(image, imgPath, needCompress,qrCodeSize,logoSize);
return image;
}
/**
* 插入LOGO
* source 二维码图片
* imgPath LOGO图片地址
* needCompress 是否压缩
*/
private static void insertImage(BufferedImage source, String imgPath,
boolean needCompress,Integer qrCodeSize,Integer logoSize) throws Exception {
File file = new File(imgPath);
if (!file.exists()) {
System.err.println("" + imgPath + " 该文件不存在!");
return;
}
Image src = ImageIO.read(new File(imgPath));
int width = src.getWidth(null);
int height = src.getHeight(null);
if (needCompress) { // 压缩LOGO
if (width > logoSize) {
width = logoSize;
}
if (height > logoSize) {
height = logoSize;
}
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 = (qrCodeSize - width) / 2;
int y = (qrCodeSize - 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();
}
/**
* 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
* destPath 存放目录
*/
public static void mkdirs(String destPath) {
File file = new File(destPath);
// 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
if (!file.exists() && !file.isDirectory()) {
file.mkdirs();
}
}
//main方法测试
public static void main(String[] args) {
qrCode("https://www.baidu.com/",300,null,null,false);
}
}
3.测试
//main方法测试
public static void main(String[] args) {
qrCode("https://www.baidu.com/",300,null,null,false);
}
4.结果