前言
本文将基于Spring Boot介绍两种生成二维码的实现方式,一种是基于Google开发工具包,另一种是基于Hutool来实现;
下面我们将基于Spring Boot,并采用两种方式实现二维码的生成,对于每一种方式还提供两种类型的二维码返回形式,即:物理文件 和 图片响应流
一、基于Google开发工具包ZXing生成二维码
(1)在pom.xml添加依赖
<!-- zxing生成二维码 --> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.3.3</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.3.3</version> </dependency>
(2)二维码处理工具类QRCodeUtil
/** * 二维码工具 * @Author:debug (SteadyJack) * @Link: weixin-> debug0868 qq-> 1948831260 * @Date: 2020/11/16 22:38 **/ public class QRCodeUtil { private static final Logger log= LoggerFactory.getLogger(QRCodeUtil.class); //CODE_WIDTH:二维码宽度,单位像素 private static final int CODE_WIDTH = 400; //CODE_HEIGHT:二维码高度,单位像素 private static final int CODE_HEIGHT = 400; //FRONT_COLOR:二维码前景色,0x000000 表示黑色 private static final int FRONT_COLOR = 0x000000; //BACKGROUND_COLOR:二维码背景色,0xFFFFFF 表示白色 //演示用 16 进制表示,和前端页面 CSS 的取色是一样的,注意前后景颜色应该对比明显,如常见的黑白 private static final int BACKGROUND_COLOR = 0xFFFFFF; // 中间图片的路径 private static final String imagePath = "resources/images/**.png"; public static void createCodeToFile(String content, File codeImgFileSaveDir, String fileName) { try { if (StringUtils.isBlank(content) || StringUtils.isBlank(fileName)) { return; } content = content.trim(); if (codeImgFileSaveDir==null || codeImgFileSaveDir.isFile()) { //二维码图片存在目录为空,默认放在桌面... codeImgFileSaveDir = FileSystemView.getFileSystemView().getHomeDirectory(); } if (!codeImgFileSaveDir.exists()) { //二维码图片存在目录不存在,开始创建... codeImgFileSaveDir.mkdirs(); } //核心代码-生成二维码 BufferedImage bufferedImage = getBufferedImage(content); File codeImgFile = new File(codeImgFileSaveDir, fileName); ImageIO.write(bufferedImage, "png", codeImgFile); log.info("二维码图片生成成功:" + codeImgFile.getPath()); } catch (Exception e) { e.printStackTrace(); } } /** * 生成二维码并输出到输出流, 通常用于输出到网页上进行显示,输出到网页与输出到磁盘上的文件中,区别在于最后一句 ImageIO.write * write(RenderedImage im,String formatName,File output):写到文件中 * write(RenderedImage im,String formatName,OutputStream output):输出到输出流中 * @param content :二维码内容 * @param outputStream :输出流,比如 HttpServletResponse 的 getOutputStream */ public static void createCodeToOutputStream(String content, OutputStream outputStream) { try { if (StringUtils.isBlank(content)) { return; } content = content.trim(); //核心代码-生成二维码 BufferedImage bufferedImage = getBufferedImage(content); //区别就是这一句,输出到输出流中,如果第三个参数是 File,则输出到文件中 ImageIO.write(bufferedImage, "png", outputStream); log.info("二维码图片生成到输出流成功..."); } catch (Exception e) { e.printStackTrace(); } } //这里是发到前端一个base64的字符串, //前端 this.base64Data = "data:img/jpg;base64," + get_to_url.data.fxORCode 这样接受即可 public static String createCodeToOutputStream(String content) { String encode = null; try { if (StringUtils.isBlank(content)) { return null; } content = content.trim(); //核心代码-生成二维码 BufferedImage bufferedImage = getBufferedImage(content); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); //区别就是这一句,输出到输出流中,如果第三个参数是 File,则输出到文件中 ImageIO.write(bufferedImage, "png", byteArrayOutputStream); BASE64Encoder base64Encoder = new BASE64Encoder(); encode = base64Encoder.encode(byteArrayOutputStream.toByteArray()); log.info("二维码图片生成到输出流成功..."); } catch (Exception e) { e.printStackTrace(); } return encode; } //核心代码-生成二维码 private static BufferedImage getBufferedImage(String content) throws WriterException { //com.google.zxing.EncodeHintType:编码提示类型,枚举类型 Map<EncodeHintType, Object> hints = new HashMap(); //EncodeHintType.CHARACTER_SET:设置字符编码类型 hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); //EncodeHintType.ERROR_CORRECTION:设置误差校正 //ErrorCorrectionLevel:误差校正等级,L = ~7% correction、M = ~15% correction、Q = ~25% correction、H = ~30% correction //不设置时,默认为 L 等级,等级不一样,生成的图案不同,但扫描的结果是一样的 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); //EncodeHintType.MARGIN:设置二维码边距,单位像素,值越小,二维码距离四周越近 hints.put(EncodeHintType.MARGIN, 1); MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, CODE_WIDTH, CODE_HEIGHT, hints); BufferedImage bufferedImage = new BufferedImage(CODE_WIDTH, CODE_HEIGHT, BufferedImage.TYPE_INT_BGR); for (int x = 0; x < CODE_WIDTH; x++) { for (int y = 0; y < CODE_HEIGHT; y++) { bufferedImage.setRGB(x, y, bitMatrix.get(x, y) ? FRONT_COLOR : BACKGROUND_COLOR); } } //QRImgCenter(bufferedImage);//如果需要添加中间小图标,这里放开即可 return bufferedImage; } //创建中间图片 private static BufferedImage QRImgCenter(BufferedImage bufferedImage) throws IOException { ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attr.getRequest(); // 得到一个Graphics2D对象,并将其设置为输出图像 Graphics2D graphics2D = bufferedImage.createGraphics(); graphics2D.drawImage(bufferedImage, 0, 0, null); // 设置中间图片 BufferedImage watermarkImage = ImageIO.read(new File(request.getServletContext().getRealPath("/") + imagePath)); int watermarkWidth = watermarkImage.getWidth(); int watermarkHeight = watermarkImage.getHeight(); int x = (bufferedImage.getWidth() - watermarkWidth) / 2; int y = (bufferedImage.getHeight() - watermarkHeight) / 2; graphics2D.drawImage(watermarkImage, x, y, null); graphics2D.drawString("", x, y); return bufferedImage; } }
总的来说,上面代码主要包含了两个部分,一部分是将实现如何将信息塞入二维码并将其生成图片存储至物理文件目录下;另一部分是实现如何直接将信息塞入二维码并生成图片最终以图片流的形式将其返回给前端调用端;
(3)测试Google ZXing工具包
@RequestMapping("qr/code") public class QrCodeController extends BaseController{ private static final String RootPath="E:\\shFiles\\QRCode"; private static final String FileFormat=".png"; private static final ThreadLocal<SimpleDateFormat> LOCALDATEFORMAT=ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMddHHmmss")); //生成二维码并将其存放于本地目录 @PostMapping("generate/v1") public BaseResponse generateV1(String content){ BaseResponse response=new BaseResponse(StatusCode.Success); try { final String fileName=LOCALDATEFORMAT.get().format(new Date()); QRCodeUtil.createCodeToFile(content,new File(RootPath),fileName+FileFormat); }catch (Exception e){ response=new BaseResponse(StatusCode.Fail.getCode(),e.getMessage()); } return response; } //生成二维码并将其返回给前端调用者 @PostMapping("generate/v2") public BaseResponse generateV2(String content,HttpServletResponse servletResponse){ BaseResponse response=new BaseResponse(StatusCode.Success); try { QRCodeUtil.createCodeToOutputStream(content,servletResponse.getOutputStream()); }catch (Exception e){ response=new BaseResponse(StatusCode.Fail.getCode(),e.getMessage()); } return response; } }
最后是将该项目运行起来并采用Postman对该接口进行测试,首先是控制器第一个方法接口的测试,其测试结果如下图所示(生成的二维码图片是存放在 E:\shFiles\QRCode 中的):
最后是控制器第二个方法接口的测试,其测试结果如下图所示:
PS:如果不想存储二维码图片到实际的文件目录,则可以采用“图片流”的形式将其返回即可;反之,则可以将生成的二维码图片存储起来并返回该图片的访问链接给到前端(这个就稍微有点麻烦了,既要存储、又要赋予图片的访问域名和链接);具体取舍可以根据实际业务情况来做抉择吧!
二、基于开源的Hutool工具生成二维码
(1)在pom.xml添加依赖
下面,debug换一种实现方式,采用目前比较知名、流行的开源工具Hutool加以实现
<!--二维码开发工具集--> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>4.6.10</version> </dependency> <!-- zxing生成二维码 --> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.3.3</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.3.3</version> </dependency>
(2)自定义一Java Config配置文件
以Bean的形式显示配置并注入QrConfig,如下代码所示:
package com.example.qrcode.Config; import cn.hutool.extra.qrcode.QrConfig; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.awt.*; @Configuration public class QRCode { @Bean public QrConfig qrConfig(){ QrConfig qrConfig=new QrConfig(); qrConfig.setBackColor(Color.white.getRGB()); qrConfig.setForeColor(Color.black.getRGB()); return qrConfig; } }
(3)建立一QRService
用于处理真正的生成二维码的业务逻辑,其核心代码如下所示:
package com.example.qrcode.Service; import cn.hutool.extra.qrcode.QrCodeUtil; import cn.hutool.extra.qrcode.QrConfig; import com.example.qrcode.Config.QRCode; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.RequestMapping; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; @Service public class QRService { @Resource QrConfig qrconig; public void generateFile(String content, File file){ //生成到本地文件 QrCodeUtil.generate(content, qrconig, file); } //输出到流 public void generateStream(String content, HttpServletResponse response) throws IOException { QrCodeUtil.generate(content,qrconig,"png",response.getOutputStream()); } }
(4)QRController控制器类中进行调用
package com.example.qrcode.Controller; import com.example.qrcode.Service.QRService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @RestController public class QRController { @Autowired QRService qrService; @RequestMapping("123") public void generateV3(String content, HttpServletResponse servletResponse) throws IOException { qrService.generateStream(content,servletResponse); } }