好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

基于SpringBoot生成二维码的几种实现方式

本文将基于Spring Boot介绍两种生成二维码的实现方式,一种是基于Google开发工具包,另一种是基于Hutool来实现;

 为了方便理解二维码的实际应用场景,举一些例子!

(1)进销存系统 想必大家都听说过,其系统中的商品二维码承载了许多重要、核心的关键信息,比如商品编码、商品名称、规格、型号、单位、作用/使用说明等信息;操作者可以借助硬件设备,如[扫码枪],通过扫描该二维码后将该商品录入到商品库中;

(2)再比如溯源系统中的产品,用户可以通过微信等APP中的扫一扫,扫描贴在产品上的二维码,不出片刻即可得到该产品的相关信息,比如产品名称、生源地、简介、价格、生产环境、经手人等信息;

 下面我们将基于Spring Boot,并采用两种方式实现二维码的生成,对于每一种方式还提供两种类型的二维码返回形式,即:物理文件 和 图片响应流

一、基于Google开发工具包ZXing生成二维码

(1)首先,需要在pom.xml依赖配置文件中加入该工具包的依赖Jar,如下所示:   

?

1

2

3

4

5

6

7

8

9

10

11

12

<!-- 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,其核心代码如下所示:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

/**

  * 二维码工具

  * @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 ;

 

     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();

         }

     }

 

     //核心代码-生成二维码

     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);

             }

         }

         return bufferedImage;

     }

}

 上述代码有点多,诸位可以在文末提供的下载地址将其下载下来,并用IDEA等开发工具将其打开,几乎每行代码debug都做了必要的注释,在这里就不赘述了!

 总的来说,上面代码主要包含了两个部分,一部分是将实现如何将信息塞入二维码并将其生成图片存储至物理文件目录下;另一部分是实现如何直接将信息塞入二维码并生成图片最终以图片流的形式将其返回给前端调用端;

(3)最后,我们需要新建一个QrCodeController控制器类,并在其中创建两个请求方法,用于测试Google ZXing工具包这种方式生成两种类型的二维码是否可行,其代码如下所示:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

@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工具生成二维码

 下面,debug换一种实现方式,采用目前比较知名、流行的开源工具Hutool加以实现,同样的道理需要在pom.xml中加入相应的Jar依赖,如下所示:   

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<!--二维码开发工具集-->

        < 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 >

然后,需要自定义一Java Config配置文件,以Bean的形式显示配置并注入QrConfig,如下代码所示:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

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;

     }

}

 紧接着我们建立一QRService,用于处理真正的生成二维码的业务逻辑,其核心代码如下所示:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

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());

     }

}

最终,是在QRController控制器类中进行调用,如下代码所示:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

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);

     }

}

通过浏览器访问测试:

到此这篇关于基于SpringBoot生成二维码的几种实现方式的文章就介绍到这了,更多相关SpringBoot生成二维码内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/weixin_42592282/article/details/121971626

查看更多关于基于SpringBoot生成二维码的几种实现方式的详细内容...

  阅读:64次