业务:后台根据前端传入的需生成的数量,生成二维码图片并统一打包成压缩包返回。
首先根据一定规则生成二维码字符串,然后返回成list(因为有多条):
为了线程安全与数据唯一,可以给雪花算法中的nextId加上synchronized static关键字,实现类对象锁。
接着将list里的每个元素转成一个个二维码图片,并打包返回。
具体实现:
/**
* 获取二维码压缩包
*
* @param response
* @param list 二维码字符串列表
*/
private void getCodeZip(HttpServletResponse response, List<SampleCode> list) {
// 生成二维码图片
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=QrCode-" + System.currentTimeMillis() + ".zip");// 压缩包名
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(response.getOutputStream());
// zos.setLevel(5);//压缩等级
for (int j = 0; j < list.size(); j++) {
String codeString = list.get(j).getCode();// 获取二维码字符串
BufferedImage qrCode = QrCodeCreateUtil.createQrCode(codeString, 900, "JPEG");// 生成二维码图片
InputStream inputStream = bufferedImageToInputStream(qrCode);// 将bufferedImage转成inputStream
zos.putNextEntry(new ZipEntry(j + ".JPEG")); // 压缩文件名称 设置ZipEntry对象
// zos.setComment("采样二维码"); // 设置注释
int temp = 0;
while ((temp = inputStream.read()) != -1) { // 读取内容
zos.write(temp); // 压缩输出
}
inputStream.close(); // 关闭输入流
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != zos) {
zos.flush();
zos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 生成包含字符串信息的二维码图片
*
* @param content 二维码携带信息
* @param qrCodeSize 二维码图片大小
* @param imageFormat 二维码的格式
* @throws WriterException
* @throws IOException
*/
public static BufferedImage createQrCode(String content, int qrCodeSize, String imageFormat)
throws WriterException, IOException {
// 设置二维码纠错级别MAP
Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 矫错级别
QRCodeWriter qrCodeWriter = new QRCodeWriter();
// 创建比特矩阵(位矩阵)的QR码编码的字符串
BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);
// 使BufferedImage勾画QRCode (matrixWidth 是行二维码像素点)
int matrixWidth = byteMatrix.getWidth();
int picWidth = matrixWidth - 200;
int picHeight = matrixWidth - 150;// 除掉二维码过长的底部留白
BufferedImage image = new BufferedImage(picWidth, picHeight, BufferedImage.TYPE_INT_RGB);
image.createGraphics();
Graphics2D graphics = (Graphics2D) image.getGraphics();
// 画二维码
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, matrixWidth, matrixWidth);
// 使用比特矩阵画并保存图像
graphics.setColor(Color.BLACK);
for (int i = 0; i < matrixWidth; i++) {
for (int j = 0; j < matrixWidth; j++) {
if (byteMatrix.get(i, j)) {
graphics.fillRect(i - 100, j - 100, 1, 1);
}
}
}
graphics.setColor(Color.BLACK);
Font font = new Font("黑体", Font.BOLD, 80); // 设置底部文字
graphics.setFont(font);
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); // 消除文字锯齿
// 计算文字长度,计算居中的x点坐标
FontMetrics fm = graphics.getFontMetrics(font);
int textWidth = fm.stringWidth(content);
int widthX = (picWidth - textWidth) / 2;
graphics.drawString(content, widthX, picHeight);
graphics.dispose();
BufferedImage bufferedImage = resize(image, 200, 200);// 设置图片大小,并在底部加上字
return bufferedImage;
}
/**
* 设置图片大小,并在底部加上字
*
* @param content
*/
public static BufferedImage resize(BufferedImage img, int newW, int newH) {
Image tmp = img.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = dimg.createGraphics();
g2d.drawImage(tmp, 0, 0, null);
g2d.dispose();
return dimg;
}