添加依赖
<!-- 二维码生成工具-->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.4.1</version>
</dependency>
@RequestMapping(value = "/QRcode", method = RequestMethod.GET)
public void QRcode(HttpServletResponse response) {
try {
BufferedImage bufferedImage = QRcode.getQRCode("DQ101000012303230002");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "PNG", baos);
OutputStream out = response.getOutputStream();
out.write(baos.toByteArray());
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
package com.zwfexer.until;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.apache.cxf.common.util.StringUtils;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
/**
* @Classname QRcode
* @Description
* @Date
* @auther
*/
public class QRcode {
/**
* @Description: (二维码)
*/
private static final int QRCOLOR = 0xFF000000; //默认是黑色
private static final int BGWHITE = 0xFFFFFFFF; //背景颜色
//二维码长宽
private static final int CODEWH = 120;
//文字的 y 轴坐标
private static final int TEXT_Y = 60;
//编码
private static final String CHARSET = "utf-8";
// 二维码绘制高度偏移量,留出空间写文字描述二维码信息
private static final int OFFSET_HEIGHT = 20;
//二维码标题字体
private static final String TITLE_FONT = "黑体";
//标题前缀
private static final String TITLE_PREFIX = "编号:";
/**
* 生成二维码图片
*
* @param content 二维码内容
*/
public static BufferedImage getQRCode(String content) {
QRcode qRcode = new QRcode();
// 二维码
BufferedImage bim = qRcode.getBufferedImage(content, BarcodeFormat.QR_CODE, CODEWH, CODEWH, qRcode.getDecodeHintType());
return bim;
}
/**
* 生成二维码bufferedImage图片
*
* @param content 编码内容
* @param barcodeFormat 编码类型
* @param width 图片宽度
* @param height 图片高度
* @param hints 设置参数
* @return
*/
public BufferedImage getBufferedImage(String content, BarcodeFormat barcodeFormat, int width, int height, Map<EncodeHintType, ?> hints) {
MultiFormatWriter multiFormatWriter = null;
BitMatrix bm = null;
BufferedImage image = null;
try {
multiFormatWriter = new MultiFormatWriter();
// 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数
bm = multiFormatWriter.encode(content, barcodeFormat, width, height, hints);
int w = bm.getWidth();
int h = bm.getHeight();
image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
// 开始利用二维码数据创建Bitmap图片,分别设为黑(0xFFFFFFFF)白(0xFF000000)两色
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGWHITE);
}
}
} catch (WriterException e) {
e.printStackTrace();
}
//有标题,合成带标题的二维码
String title = content;
if (!StringUtils.isEmpty(title)) {
return drawDetailForQR(image, title);
}
return image;
}
/**
* 绘制二维码描述信息.
*
* @param source 源二维码图片
* @param title 二维码标题
* @return 合成后的图片
*/
private BufferedImage drawDetailForQR(BufferedImage source, String title) {
//新建模板图片
BufferedImage bufferedImage = new BufferedImage(CODEWH, CODEWH + OFFSET_HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = bufferedImage.createGraphics();
//绘制矩形背景
graphics.setColor(Color.white);
graphics.fillRect(0, 0, CODEWH, OFFSET_HEIGHT);
//绘制描述信息
//标题字体大小
Font font = new Font(TITLE_FONT, Font.PLAIN, 10);
graphics.setColor(Color.green);
graphics.setFont(font);
graphics.drawString(TITLE_PREFIX + title, 5, OFFSET_HEIGHT - 2);
//绘制二维码
graphics.drawImage(source, 0, OFFSET_HEIGHT, CODEWH, CODEWH, null);
graphics.dispose();
return bufferedImage;
}
/**
* 设置二维码的格式参数
*
* @return
*/
public Map<EncodeHintType, Object> getDecodeHintType() {
// 用于设置QR二维码参数
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
// 设置QR二维码的纠错级别(H为最高级别)具体级别信息
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q);
// 设置编码方式
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.MARGIN, 0);
return hints;
}
}
效果