简述:最近有接到一个需求,生成二维码,然后打包成zip ,最后下载,且不用文件服务器!
1、spring boot 搭建项目我就不多写了,网上一大堆
2、maven 引入jar包
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.4.0</version>
</dependency>
3、controller 编写
/**
* @author lucksheep
*/
@Controller
@RequestMapping("/download")
public class DownloadController {
/**
* 自定义生成N张二维码,且打包下载
* @param request
* @param response
* @param number
* @throws IOException
* @throws WriterException
*/
@GetMapping("/qrcode/zip/{number}")
public void downloadZip(HttpServletRequest request, HttpServletResponse response,@PathVariable Integer number) throws IOException, WriterException {
String zipName=new String("二维码".getBytes(),"ISO-8859-1") + ".zip";
response.setContentType("application/zip");
response.setHeader("Content-disposition","attachment; filename=" +zipName );
OutputStream outputStream = response.getOutputStream();
ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
for (int i=0;i<number;i++) {
String url = "https://www.baidu.com/";
BufferedImage buffImg = QRcodeUntil.getBufferedImage(null, url);
ZipEntry entry = new ZipEntry("二维码"+i+".png");
zipOutputStream.putNextEntry(entry);
ImageIO.write(buffImg, "png", zipOutputStream);
}
zipOutputStream.flush();
zipOutputStream.close();
outputStream.flush();
outputStream.close();
}
@GetMapping("/qrcode")
public void downloadQrcode(HttpServletRequest request, HttpServletResponse response) throws IOException, WriterException {
String imgName=new String("我是谁".getBytes(),"ISO-8859-1") + ".png";
response.setContentType("application/force-download");
response.setHeader("Content-disposition","attachment; filename=" +imgName );
OutputStream outputStream = response.getOutputStream();
String url = "https://www.baidu.com/";
BufferedImage buffImg = QRcodeUntil.getBufferedImage(null, url);
ImageIO.write(buffImg, "png", outputStream);
outputStream.flush();
outputStream.close();
}
}
4、二维码生成帮助类 QRcodeUntil .java
package com.mvc.smus.common;
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.springframework.util.StringUtils;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
* 画制定logo和制定描述的二维码
* @author zy
*/
public class QRcodeUntil {
/**
* 默认是黑色
*/
private static final int QRCOLOR = 0xFF000000;
/**
* 背景颜色
*/
private static final int BGWHITE = 0xFFFFFFFF;
/**
* 二维码宽
*/
private static final int WIDTH = 400;
/**
* 二维码高
*/
private static final int HEIGHT = 400;
/**
* 用于设置QR二维码参数
*/
public static Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>() {
private static final long serialVersionUID = 1L;
{
// 设置QR二维码的纠错级别(H为最高级别)具体级别信息
put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
// 设置编码方式
put(EncodeHintType.CHARACTER_SET, "utf-8");
put(EncodeHintType.MARGIN, 0);
}
};
public static void main(String[] args) {
HttpURLConnection httpUrl = null;
try {
String imgUrl="http://img2.imgtn.bdimg.com/it/u=2874714129,2818353273&fm=26&gp=0.jpg";
URL url = new URL(imgUrl);
httpUrl = (HttpURLConnection) url.openConnection();
httpUrl.connect();
InputStream in=httpUrl.getInputStream();
BufferedImage img=ImageIO.read(in);
File QrCodeFile = new File("F:/down/textsss.png");
ImageIO.write(img, "png", QrCodeFile);
} catch (IOException e) {
} catch (ClassCastException e) {
} finally {
try {
httpUrl.disconnect();
} catch (NullPointerException e) {
}
}
}
/**
* 生成带logo的二维码图片
* @param logoFile logo图片地址
* @param qrUrl
*/
public static BufferedImage getBufferedImage(File logoFile, String qrUrl) {
try {
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
// 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数
BitMatrix bm = multiFormatWriter.encode(qrUrl, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
// 开始利用二维码数据创建Bitmap图片,分别设为黑(0xFFFFFFFF)白(0xFF000000)两色
for (int x = 0; x < WIDTH; x++) {
for (int y = 0; y < HEIGHT; y++) {
image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGWHITE);
}
}
int width = image.getWidth();
int height = image.getHeight();
if (Objects.nonNull(logoFile) && logoFile.exists()) {
// 读取Logo图片
BufferedImage logo = ImageIO.read(logoFile);
logo=setRadius(logo,width / 2);
// 构建绘图对象
Graphics2D g = image.createGraphics();
// 开始绘制logo图片
g.drawImage(logo, width * 2 / 5, height * 2 / 5, width * 2 / 10, height * 2 / 10, null);
g.dispose();
logo.flush();
}
image.flush();
return image;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 图片设置圆角
* @param image 图片流
* @return radius 半径
* @throws IOException
*/
public static BufferedImage setRadius(BufferedImage image,int radius){
int w = image.getWidth();
int h = image.getHeight();
BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = output.createGraphics();
g2.setComposite(AlphaComposite.Src);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Color.WHITE);
g2.fill(new RoundRectangle2D.Float(0, 0, w, h, radius, radius));
g2.setComposite(AlphaComposite.SrcAtop);
g2.drawImage(image, 0, 0, null);
g2.dispose();
return output;
}
/**
* 设置图片文本
* @param image
* @param height
* @param note
* @return
*/
public static BufferedImage setImgText(BufferedImage image,int height,String note){
// 自定义文本描述
if (!StringUtils.isEmpty(note)) {
// 新的图片,把带logo的二维码下面加上文字
BufferedImage outImage = new BufferedImage(400, 445, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D outg = outImage.createGraphics();
// 画二维码到新的面板
outg.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
// 画文字到新的面板
outg.setColor(Color.BLACK);
// 字体、字型、字号
outg.setFont(new Font("宋体", Font.BOLD, 20));
int strWidth = outg.getFontMetrics().stringWidth(note);
if (strWidth > 399) {
// 长度过长就换行
String note1 = note.substring(0, note.length() / 2);
String note2 = note.substring(note.length() / 2, note.length());
int strWidth1 = outg.getFontMetrics().stringWidth(note1);
int strWidth2 = outg.getFontMetrics().stringWidth(note2);
outg.drawString(note1, 200 - strWidth1 / 2, height + (outImage.getHeight() - height) / 2 + 12);
BufferedImage outImage2 = new BufferedImage(400, 485, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D outg2 = outImage2.createGraphics();
outg2.drawImage(outImage, 0, 0, outImage.getWidth(), outImage.getHeight(), null);
outg2.setColor(Color.BLACK);
// 字体、字型、字号
outg2.setFont(new Font("宋体", Font.BOLD, 30));
outg2.drawString(note2, 200 - strWidth2 / 2,outImage.getHeight() + (outImage2.getHeight() - outImage.getHeight()) / 2 + 5);
outg2.dispose();
outImage2.flush();
outImage = outImage2;
} else {
// 画文字
outg.drawString(note, 200 - strWidth / 2, height + (outImage.getHeight() - height) / 2 + 12);
}
outg.dispose();
outImage.flush();
image = outImage;
}
return image;
}
}
注释:如果要在二维码图片中显示文字,则加入下面的方法调用