主要使用 hutool 的二维码工具类及图片工具类,实现给二维码添加背景图,增加自定义文字的效果。
并实现了二维码的展示与打包下载。
Controller层
@GetMapping("testOutput")
@ApiOperation(value = "测试输出单张图片")
public void testOutput(HttpServletResponse response)
{
//背景图地址,resources 目录下
String fileSource = "static/img/bg.png";
//二维码配置
QrConfig config = new QrConfig(300, 300);
//二维码内容
String content = "我是二维码内容";
ByteArrayInputStream input = cultureEnrollService.getQrCodeUrl(fileSource, config, content,0, 170,
"文本一", 24, 0, -280,
"文本二", 24, 0, 350);
try {
response.setContentType("image/png");
response.setCharacterEncoding("utf-8");
// 下载文件
// response.setHeader("Content-disposition", "attachment;filename*=utf-8''img.png");
ServletOutputStream out = response.getOutputStream();
byte[] buffer = new byte[1024];
int r = 0;
while ((r = input.read(buffer)) != -1) {
out.write(buffer, 0, r);
}
out.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@GetMapping("testOutput2")
@ApiOperation(value = "测试输出压缩文件")
public void testOutput2(HttpServletResponse response)
{
try {
cultureEnrollService.batchDownload(response);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Service 层
/**
* 两个图片合并(加文字) 文件格式
*
* @param sourceUrl 底部背景图
* @param qrContent 二维码内容
* @param qrX 二维码横坐标
* @param qrY 二维码纵坐标
* @param name1 文字1(一般为标题)
* @param fontSize1 文字1大小
* @param fontX1 文字1的横坐标
* @param fontY1 文字1的纵坐标
* @param name2 文字2(一般为标题)
* @param fontSize2 文字2大小
* @param fontX2 文字2的横坐标
* @param fontY2 文字2的纵坐标
* @return
*/
@Override
public ByteArrayInputStream getQrCodeUrl(String sourceUrl,
QrConfig config,
String qrContent,
int qrX,
int qrY,
String name1,
int fontSize1,
int fontX1,
int fontY1,
String name2,
int fontSize2,
int fontX2,
int fontY2) {
//二维码设置
// QrConfig config = new QrConfig(300, 300);
//1,生成二维码
BufferedImage qrImage = QrCodeUtil.generate(qrContent, config);
// 2,将图片合成在一起
Image image = ImgUtil.pressImage(
//底图
ImgUtil.read(sourceUrl),
//二维码图(水印图)
qrImage,
// ImgUtil.read(codeFile),
qrX,
//y坐标修正值。 默认在中间,偏移量相对于中间偏移
qrY,
//透明度
1.0f
);
//3,加企业文字
Image image1 = ImgUtil.pressText(
image,
//title
name1,
//颜色
Color.YELLOW,
//字体 42
new Font("黑体", Font.BOLD, fontSize1),
//x坐标修正值。 默认在中间,偏移量相对于中间偏移
fontX1,
//y坐标修正值。 默认在中间,偏移量相对于中间偏移
fontY1,
//透明度:alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字
1.0f
);
//4,文字2
Image image2 = ImgUtil.pressText(
image1,
//title
name2,
//颜色
Color.YELLOW,
//字体 42
new Font("黑体", Font.BOLD, fontSize2),
//x坐标修正值。 默认在中间,偏移量相对于中间偏移
fontX2,
//y坐标修正值。 默认在中间,偏移量相对于中间偏移
fontY2,
//透明度:alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字
1.0f
);
//转为流
ByteArrayInputStream inputStream = ImgUtil.toStream(image2, "png");
return inputStream;
}
@Override
public void batchDownload(HttpServletResponse response) throws IOException {
//背景图地址,resources 目录下
String fileSource = "image/bg.png";
//文件的名称
String downloadFilename = "二维码.zip";
//转换中文否则可能会产生乱码
downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");
// 指明response的返回对象是文件流
response.setContentType("application/octet-stream");
// 设置在下载框默认显示的文件名
response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename);
ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
//二维码配置
QrConfig config = new QrConfig(300, 300);
//查询业务信息
List<String> list = new ArrayList<>();
list.add("这是业务对象1");
list.add("这是业务对象2");
// 对企业进行遍历,填充对应信息
for (String s : list) {
ByteArrayInputStream input = this.getQrCodeUrl(fileSource, config, s,0, 170,
"读取文本一", 24, 0, -280,
"读取文本二", 24, 0, 350);
zos.putNextEntry(new ZipEntry("图片文件名.png"));
byte[] buffer = new byte[1024];
int r = 0;
while ((r = input.read(buffer)) != -1) {
zos.write(buffer, 0, r);
}
input.close();
}
zos.flush();
zos.close();
}
具体引入包
import cn.hutool.core.img.ImgUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
hutool本身是不引入 zxing 的,但是 qrcode 工具类依赖了 zxing,还需要额外引入 zxing。
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.5.2</version>
</dependency>
5383

被折叠的 条评论
为什么被折叠?



